Skip to content

Commit

Permalink
Make ShingleBased.GetProfile public to match upstream, update documen…
Browse files Browse the repository at this point in the history
…tation and add unit test for Cosine similarity. #21
  • Loading branch information
paulirwin committed Feb 19, 2021
1 parent 27ee599 commit 17ce434
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ public class Program
var cosine = new Cosine(2);

// For cosine similarity I need the profile of strings
StringProfile profile1 = cosine.GetProfile(s1);
StringProfile profile2 = cosine.GetProfile(s2);
var profile1 = cosine.GetProfile(s1);
var profile2 = cosine.GetProfile(s2);

// Prints 0.516185
Console.WriteLine(profile1.CosineSimilarity(profile2));
Console.WriteLine(cosine.Similarity(profile1, profile2));
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion src/F23.StringSimilarity/ShingleBased.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected ShingleBased(int k)

protected ShingleBased() : this(DEFAULT_K) { }

protected IDictionary<string, int> GetProfile(string s)
public IDictionary<string, int> GetProfile(string s)
{
var shingles = new Dictionary<string, int>();

Expand Down
27 changes: 27 additions & 0 deletions test/F23.StringSimilarity.Tests/CosineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,31 @@
* THE SOFTWARE.
*/

using System;
using System.CodeDom;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using F23.StringSimilarity.Tests.TestUtil;
using Xunit;
using Xunit.Abstractions;

namespace F23.StringSimilarity.Tests
{
[SuppressMessage("ReSharper", "ArgumentsStyleLiteral")]
[SuppressMessage("ReSharper", "ArgumentsStyleNamedExpression")]
public class CosineTest
{
private readonly ITestOutputHelper _testOutputHelper;

public CosineTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public void TestSimilarity()
{
Expand Down Expand Up @@ -94,6 +104,23 @@ public void TestDistance()
// TODO: regular (non-null/empty) distance tests
}

[Fact]
public void DocumentationExampleTest()
{
string s1 = "My first string";
string s2 = "My other string...";

// Let's work with sequences of 2 characters...
var cosine = new Cosine(2);

// For cosine similarity I need the profile of strings
var profile1 = cosine.GetProfile(s1);
var profile2 = cosine.GetProfile(s2);

// Prints 0.516185
Assert.Equal(0.516185, cosine.Similarity(profile1, profile2), 6);
}

private static async Task<string> ReadResourceFileAsync(string file)
{
var assembly = Assembly.GetExecutingAssembly();
Expand Down

0 comments on commit 17ce434

Please sign in to comment.