Skip to content

Commit 17ce434

Browse files
committed
Make ShingleBased.GetProfile public to match upstream, update documentation and add unit test for Cosine similarity. #21
1 parent 27ee599 commit 17ce434

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,11 @@ public class Program
365365
var cosine = new Cosine(2);
366366

367367
// For cosine similarity I need the profile of strings
368-
StringProfile profile1 = cosine.GetProfile(s1);
369-
StringProfile profile2 = cosine.GetProfile(s2);
368+
var profile1 = cosine.GetProfile(s1);
369+
var profile2 = cosine.GetProfile(s2);
370370

371371
// Prints 0.516185
372-
Console.WriteLine(profile1.CosineSimilarity(profile2));
372+
Console.WriteLine(cosine.Similarity(profile1, profile2));
373373
}
374374
}
375375
```

src/F23.StringSimilarity/ShingleBased.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected ShingleBased(int k)
5959

6060
protected ShingleBased() : this(DEFAULT_K) { }
6161

62-
protected IDictionary<string, int> GetProfile(string s)
62+
public IDictionary<string, int> GetProfile(string s)
6363
{
6464
var shingles = new Dictionary<string, int>();
6565

test/F23.StringSimilarity.Tests/CosineTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,31 @@
2222
* THE SOFTWARE.
2323
*/
2424

25+
using System;
2526
using System.CodeDom;
2627
using System.Diagnostics;
2728
using System.Diagnostics.CodeAnalysis;
29+
using System.Globalization;
2830
using System.IO;
2931
using System.Reflection;
3032
using System.Threading.Tasks;
3133
using F23.StringSimilarity.Tests.TestUtil;
3234
using Xunit;
35+
using Xunit.Abstractions;
3336

3437
namespace F23.StringSimilarity.Tests
3538
{
3639
[SuppressMessage("ReSharper", "ArgumentsStyleLiteral")]
3740
[SuppressMessage("ReSharper", "ArgumentsStyleNamedExpression")]
3841
public class CosineTest
3942
{
43+
private readonly ITestOutputHelper _testOutputHelper;
44+
45+
public CosineTest(ITestOutputHelper testOutputHelper)
46+
{
47+
_testOutputHelper = testOutputHelper;
48+
}
49+
4050
[Fact]
4151
public void TestSimilarity()
4252
{
@@ -94,6 +104,23 @@ public void TestDistance()
94104
// TODO: regular (non-null/empty) distance tests
95105
}
96106

107+
[Fact]
108+
public void DocumentationExampleTest()
109+
{
110+
string s1 = "My first string";
111+
string s2 = "My other string...";
112+
113+
// Let's work with sequences of 2 characters...
114+
var cosine = new Cosine(2);
115+
116+
// For cosine similarity I need the profile of strings
117+
var profile1 = cosine.GetProfile(s1);
118+
var profile2 = cosine.GetProfile(s2);
119+
120+
// Prints 0.516185
121+
Assert.Equal(0.516185, cosine.Similarity(profile1, profile2), 6);
122+
}
123+
97124
private static async Task<string> ReadResourceFileAsync(string file)
98125
{
99126
var assembly = Assembly.GetExecutingAssembly();

0 commit comments

Comments
 (0)