Skip to content
10 changes: 8 additions & 2 deletions FuzzySharp.Benchmarks/BenchmarkAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ namespace Raffinert.FuzzySharp.Benchmarks;
[MemoryDiagnoser]
public class BenchmarkAll
{
[GlobalSetup]
public void GlobalSetup()
{
GlobalConfig.PartialRatioAccuracy = PartialRatioAccuracy.Strict;
}

[Benchmark]
public int Ratio()
{
Expand All @@ -18,7 +24,7 @@ public int Ratio()
[Benchmark]
public int PartialRatio()
{
return Fuzz.PartialRatio("similar", "somewhresimlrbetweenthisstring");
return Fuzz.PartialRatio("Supplier: ACME Corp. International, Address: 221B Baker St., London NW1 6XE", "Order: PO-100923, Supplier: Acme Corporation International, Address: 221B Baker Street, London NW1 6XE, VAT: GB123456789, Contact: accounting@acme.example");
}

[Benchmark]
Expand Down Expand Up @@ -96,7 +102,7 @@ public int RatioClassic()
[Benchmark]
public int PartialRatioClassic()
{
return Classic.Fuzz.PartialRatio("similar", "somewhresimlrbetweenthisstring");
return Classic.Fuzz.PartialRatio("Supplier: ACME Corp. International, Address: 221B Baker St., London NW1 6XE", "Order: PO-100923, Supplier: Acme Corporation International, Address: 221B Baker Street, London NW1 6XE, VAT: GB123456789, Contact: accounting@acme.example");
}

[Benchmark]
Expand Down
50 changes: 50 additions & 0 deletions FuzzySharp.Benchmarks/BenchmarkFastPartial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using BenchmarkDotNet.Attributes;
using Raffinert.FuzzySharp.PreProcess;

namespace Raffinert.FuzzySharp.Benchmarks;

[MemoryDiagnoser]
public class BenchmarkFastPartial
{
[GlobalSetup]
public void GlobalSetup()
{
GlobalConfig.PartialRatioAccuracy = PartialRatioAccuracy.Fast;
}

[Benchmark]
public int PartialRatio()
{
return Fuzz.PartialRatio("Supplier: ACME Corp. International, Address: 221B Baker St., London NW1 6XE", "Order: PO-100923, Supplier: Acme Corporation International, Address: 221B Baker Street, London NW1 6XE, VAT: GB123456789, Contact: accounting@acme.example");
}

[Benchmark]
public int PartialTokenSortRatio()
{
return Fuzz.PartialTokenSortRatio("order words out of", " words out of order");
}

[Benchmark]
public int PartialTokenSetRatio()
{
return Fuzz.PartialTokenSetRatio("fuzzy was a bear", "fuzzy fuzzy fuzzy bear");
}

[Benchmark]
public int WeightedRatio()
{
return Fuzz.WeightedRatio("The quick brown fox jimps ofver the small lazy dog", "the quick brown fox jumps over the small lazy dog");
}

[Benchmark]
public int PartialTokenInitialismRatio()
{
return Fuzz.PartialTokenInitialismRatio("NASA", "National Aeronautics Space Administration, Kennedy Space Center, Cape Canaveral, Florida 32899");
}

[Benchmark]
public int PartialTokenAbbreviationRatio()
{
return Fuzz.PartialTokenAbbreviationRatio("bl 420", "Baseline section 420", PreprocessMode.Full);
}
}
3 changes: 2 additions & 1 deletion FuzzySharp.Benchmarks/FuzzySharp.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>NET10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>$(MSBuildProjectName)</AssemblyName>
Expand All @@ -13,6 +13,7 @@
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
<PackageReference Include="Fastenshtein" Version="1.0.10" />
<PackageReference Include="FuzzySharp" Version="2.0.2" />
<PackageReference Include="Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers" Version="18.3.36812.1" />
<PackageReference Include="Quickenshtein" Version="1.5.1" />
</ItemGroup>

Expand Down
40 changes: 3 additions & 37 deletions FuzzySharp.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,9 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;

//using Raffinert.FuzzySharp;
//using Raffinert.FuzzySharp.SimilarityRatio;
//using Raffinert.FuzzySharp.SimilarityRatio.Scorer.Composite;
//using Classic = FuzzySharp;
using Raffinert.FuzzySharp.Benchmarks;

var config = ManualConfig.Create(DefaultConfig.Instance)
.AddJob(Job.ShortRun); // ← built-in short run

BenchmarkRunner.Run(typeof(Program).Assembly, config);

//var input1 = "+30.0% Damage to Close Enemies [30.01%";
//var input2Collection = new[]
//{
// "+#% Damage",
// "+#% Damage to Crowd Controlled Enemies",
// "+#% Damage to Close Enemies",
// "+#% Damage to Chilled Enemies",
// "+#% Damage to Poisoned Enemies",
// "#% Block Chance#% Blocked Damage Reduction",
// "#% Damage Reduction from Bleeding Enemies",
// "#% Damage Reduction",
// "+#% Cold Damage"
//};

//var classicScorer = Classic.SimilarityRatio.ScorerCache.Get<Classic.SimilarityRatio.Scorer.Composite.WeightedRatioScorer>();

//Func<string, int> classicScorerFunc = input2 => classicScorer.Score(input1, input2);

//var classicResult = input2Collection.Select(classicScorerFunc).ToList();

//var scorer = ScorerCache.Get<WeightedRatioScorer>();

//Func<string, int> scorerFunc = input2 => scorer.Score(input1, input2);

//var result = input2Collection.Select(scorerFunc).ToList();

//Console.WriteLine();
.AddJob(Job.ShortRun);

//Console.WriteLine(Fuzz.WeightedRatio("The quick brown fox jimps ofver the small lazy dog", "the quick brown fox jumps over the small lazy dog"));
BenchmarkSwitcher.FromAssembly(typeof(BenchmarkFastPartial).Assembly).Run(args, config);
2 changes: 1 addition & 1 deletion FuzzySharp.Test/FuzzySharp.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netframework4.6.2;netframework4.7.2;NET8.0;NET9.0</TargetFrameworks>
<TargetFrameworks>netframework4.6.2;netframework4.7.2;NET8.0;NET10.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>12.0</LangVersion>
<AssemblyName>Raffinert.$(MSBuildProjectName)</AssemblyName>
Expand Down
237 changes: 237 additions & 0 deletions FuzzySharp.Test/FuzzyTests/FastPartialRatioTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
using System;
using NUnit.Framework;
using Raffinert.FuzzySharp.PreProcess;

namespace Raffinert.FuzzySharp.Test.FuzzyTests;

[TestFixture]
public class FastPartialRatioTests : IDisposable
{
#region Private Fields
private string _s1,
_s1A,
_s2,
_s3,
_s4,
_s5,
_s6,
_s7,
_s8,
_s8A,
_s9,
_s9A,
_s10,
_s10A;

private string[] _cirqueStrings, _baseballStrings;
#endregion

[SetUp]
public void Setup()
{
GlobalConfig.PartialRatioAccuracy = PartialRatioAccuracy.Fast;
_s1 = "new york mets";
_s1A = "new york mets";
_s2 = "new YORK mets";
_s3 = "the wonderful new york mets";
_s4 = "new york mets vs atlanta braves";
_s5 = "atlanta braves vs new york mets";
_s6 = "new york mets - atlanta braves";
_s7 = "new york city mets - atlanta braves";
// Edge cases
_s8 = "{";
_s8A = "{";
_s9 = "{a";
_s9A = "{a";
_s10 = "a{";
_s10A = "{b";
}

public void Dispose()
{
GlobalConfig.PartialRatioAccuracy = PartialRatioAccuracy.Strict;
}

[Test]
public void Test_Equal()
{
Assert.AreEqual(Fuzz.Ratio(_s1, _s1A), 100);
Assert.AreEqual(Fuzz.Ratio(_s8, _s8A), 100);
Assert.AreEqual(Fuzz.Ratio(_s9, _s9A), 100);
}

[Test]
public void Test_Case_Insensitive()
{
Assert.AreNotEqual(Fuzz.Ratio(_s1, _s2), 100);
Assert.AreEqual(Fuzz.Ratio(_s1, _s2, PreprocessMode.Full), 100);
}

[Test]
public void Test_Partial()
{
Assert.AreEqual(Fuzz.PartialRatio(_s1, _s3), 100);
}

[Test]
public void TestTokenSortRatio()
{
Assert.AreEqual(Fuzz.TokenSortRatio(_s1, _s1A), 100);
}

[Test]
public void TestPartialTokenSortRatio()
{
Assert.AreEqual(Fuzz.PartialTokenSortRatio(_s1, _s1A, PreprocessMode.Full), 100);
Assert.AreEqual(Fuzz.PartialTokenSortRatio(_s4, _s5, PreprocessMode.Full), 100);
Assert.AreEqual(Fuzz.PartialTokenSortRatio(_s8, _s8A), 100);
Assert.AreEqual(Fuzz.PartialTokenSortRatio(_s9, _s9A, PreprocessMode.Full), 100);
Assert.AreEqual(Fuzz.PartialTokenSortRatio(_s9, _s9A), 100);

//var al = Fuzz1.PartialRatioAlignment("a certain string".AsSpan(), "cetain".AsSpan());

Assert.AreEqual(Fuzz.PartialTokenSortRatio(_s10, _s10A), 50); // 67 in strict mode
Assert.AreEqual(Fuzz.PartialTokenSortRatio(_s10, _s10A, PreprocessMode.Full), 0);
}

[Test]
public void TestTokenSetRatio()
{
Assert.AreEqual(Fuzz.TokenSetRatio(_s4, _s5, PreprocessMode.Full), 100);
Assert.AreEqual(Fuzz.TokenSetRatio(_s8, _s8A), 100);
Assert.AreEqual(Fuzz.TokenSetRatio(_s9, _s9A, PreprocessMode.Full), 100);
Assert.AreEqual(Fuzz.TokenSetRatio(_s9, _s9A), 100);
Assert.AreEqual(Fuzz.TokenSetRatio(_s10, _s10A), 50);
}

[Test]
public void TestTokenAbbreviationRatio()
{
Assert.AreEqual(Fuzz.TokenAbbreviationRatio("bl 420", "Baseline section 420", PreprocessMode.Full), 40);
Assert.AreEqual(Fuzz.PartialTokenAbbreviationRatio("bl 420", "Baseline section 420", PreprocessMode.Full), 50); // 67 in strict mode
}

[Test]
public void TestPartialTokenSetRatio()
{
Assert.AreEqual(Fuzz.PartialTokenSetRatio(_s4, _s7), 100);
}

[Test]
public void TestWeightedRatioEqual()
{
Assert.AreEqual(Fuzz.WeightedRatio(_s1, _s1A), 100);
}

[Test]
public void TestWeightedRatioCaseInsensitive()
{
Assert.AreEqual(Fuzz.WeightedRatio(_s1, _s2, PreprocessMode.Full), 100);
}

[Test]
public void TestWeightedRatioPartialMatch()
{
Assert.AreEqual(Fuzz.WeightedRatio(_s1, _s3), 90);
}

[Test]
public void TestWeightedRatioMisorderedMatch()
{
Assert.AreEqual(Fuzz.WeightedRatio(_s4, _s5), 95);
}

[Test]
public void TestEmptyStringsScore0()
{
Assert.That(Fuzz.Ratio("test_string", ""), Is.EqualTo(0));
Assert.That(Fuzz.PartialRatio("test_string", ""), Is.EqualTo(0));
Assert.That(Fuzz.Ratio("", ""), Is.EqualTo(0));
Assert.That(Fuzz.PartialRatio("", ""), Is.EqualTo(0));
}

[Test]
public void TestIssueSeven()
{
_s1 = "HSINCHUANG";
_s2 = "SINJHUAN";
_s3 = "LSINJHUANG DISTRIC";
_s4 = "SINJHUANG DISTRICT";

Assert.IsTrue(Fuzz.PartialRatio(_s1, _s2) > 75);
Assert.IsTrue(Fuzz.PartialRatio(_s1, _s3) > 75);
Assert.IsTrue(Fuzz.PartialRatio(_s1, _s4) > 75);
}

[Test]
public void TestIssueEight()
{
// https://github.com/JakeBayer/FuzzySharp/issues/8
Assert.AreEqual(85, Fuzz.PartialRatio("Partnernummer", "Partne\nrnum\nmerASDFPartnernummerASDF")); // 100 in strict mode
Assert.AreEqual(77, Fuzz.PartialRatio("Partnernummer", "PartnerrrrnummerASDFPartnernummerASDF")); // 100 in strict mode

// https://github.com/xdrop/fuzzywuzzy/issues/39
Assert.AreEqual(57, Fuzz.PartialRatio("kaution", "kdeffxxxiban:de1110010060046666666datum:16.11.17zeit:01:12uft0000899999tan076601testd.-20-maisonette-z4-jobas-hagkautionauszug")); // 100 in strict mode

// https://github.com/seatgeek/fuzzywuzzy/issues/79
Assert.AreEqual(93, Fuzz.PartialRatio("this is a test", "is this is a not really thing this is a test!")); // 100 in strict mode

// https://github.com/Raffinert/FuzzySharp/issues/2
Assert.AreEqual(100, Fuzz.PartialRatio("sh", "Growing eshops without a popular platform", PreprocessMode.Full));
Assert.AreEqual(100, Fuzz.PartialRatio("shop", "Growing eshops without a popular platform", PreprocessMode.Full));
}

[Test]
public void MorePartialRatio()
{
Assert.AreEqual(100, Fuzz.PartialRatio("geeks for geeks", "geeks for geeks!"));
Assert.AreEqual(64, Fuzz.PartialRatio("geeks for geeks", "geeks geeks")); // 71 in strict mode
Assert.AreEqual(100, Fuzz.TokenSortRatio("geeks for geeks", "for geeks geeks"));
}

[Test]
public void TestPartialRatioUnicodeString()
{
_s1 = "\u00C1";
_s2 = "ABCD";
var score = Fuzz.PartialRatio(_s1, _s2);
Assert.AreEqual(0, score);
}

[Test]
public void TestZeroRatio()
{
var ratio = Fuzz.PartialTokenSortRatio("abc", "def");

Assert.True(ratio == 0);
}

[Test]
public void Test03()
{
var ratio = Fuzz.PartialTokenSortRatio("new york mets", "atlanta braves vs new york mets");

Assert.True(ratio == 77);
}

[Test]
public void TestRatioUnicodeString()
{
_s1 = "\u00C1";
_s2 = "ABCD";
var score = Fuzz.WeightedRatio(_s1, _s2);
Assert.AreEqual(0, score);

// Cyrillic.
_s1 = "\u043f\u0441\u0438\u0445\u043e\u043b\u043e\u0433";
_s2 = "\u043f\u0441\u0438\u0445\u043e\u0442\u0435\u0440\u0430\u043f\u0435\u0432\u0442";
score = Fuzz.WeightedRatio(_s1, _s2);
Assert.AreNotEqual(0, score);

// Chinese.
_s1 = "\u6211\u4e86\u89e3\u6570\u5b66";
_s2 = "\u6211\u5b66\u6570\u5b66";
score = Fuzz.WeightedRatio(_s1, _s2);
Assert.AreNotEqual(0, score);
}
}
Loading