![]() |
VOOZH | about |
dotnet add package FluentSimilarity --version 1.0.7
NuGet\Install-Package FluentSimilarity -Version 1.0.7
<PackageReference Include="FluentSimilarity" Version="1.0.7" />
<PackageVersion Include="FluentSimilarity" Version="1.0.7" />Directory.Packages.props
<PackageReference Include="FluentSimilarity" />Project file
paket add FluentSimilarity --version 1.0.7
#r "nuget: FluentSimilarity, 1.0.7"
#:package FluentSimilarity@1.0.7
#addin nuget:?package=FluentSimilarity&version=1.0.7Install as a Cake Addin
#tool nuget:?package=FluentSimilarity&version=1.0.7Install as a Cake Tool
The FluentSimilarity library provides a fluent API for comparing objects and calculating similarity between their properties. It supports a wide variety of comparison methods, including string similarity, numeric similarity, and date-based similarity. This library is designed to be easy to extend and integrate into any C# project.
To use the FluentSimilarity library, you can install the NuGet package:
Install-Package FluentSimilarity
To use the FluentSimilarity library, you need to create similarity rules for each property of the object you want to compare. The library allows you to define rules for strings, numbers, and dates. Each rule can apply multiple comparison methods, and the highest similarity score is used.
Below is an example where we define similarity rules for a Person class with properties for FirstName, LastName, Age, and BirthDate.
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public DateTime BirthDate { get; set; }
}
public class PersonSimilarity : AbstractSimilarity<Person>
{
public PersonSimilarity()
{
RuleFor(x => x.FirstName)
.LevenshteinCompare()
.SoundexCompare();
RuleFor(x => x.LastName)
.JaroWinklerCompare()
.LevenshteinCompare();
RuleFor(x => x.Age)
.ExactMatch()
.RangeSimilarity(10)
.PercentageDifference(15);
RuleFor(x => x.BirthDate)
.ExactDateMatch()
.PartialDateMatch();
}
}
Once you've defined the similarity rules for your class, you can use the Compare method to calculate the similarity between two objects.
var person1 = new Person { FirstName = "John", LastName = "Doe", Age = 30, BirthDate = new DateTime(1993, 5, 21) };
var person2 = new Person { FirstName = "Jon", LastName = "Doe", Age = 35, BirthDate = new DateTime(1992, 5, 21) };
var personSimilarity = new PersonSimilarity();
double similarityScore = personSimilarity.Compare(person1, person2);
Console.WriteLine($"Similarity Score: {similarityScore}%");
In this case, the similarity score will be calculated based on the rules defined for each property, using the highest similarity score from the comparison methods.
The library supports a wide range of comparison methods, including:
The library provides several string comparison methods, including Levenshtein, Soundex, and Jaro-Winkler. These methods calculate similarity between strings based on different algorithms. Here's an example of how to use the LevenshteinCompare and SoundexCompare methods for the FirstName property.
RuleFor(x => x.FirstName)
.LevenshteinCompare()
.SoundexCompare();
The library supports various number comparison methods, such as ExactMatch, RangeSimilarity, and PercentageDifference.
// Range similarity: calculates similarity based on how close two integers are within a given range.
RuleFor(x => x.Age)
.RangeSimilarity(10); // 10-year range
// Percentage difference: calculates similarity based on the percentage difference between two integers.
// The score decreases as the percentage difference increases. If the difference exceeds the maxPercentage, the score is 0.
RuleFor(x => x.Age)
.PercentageDifference(15); // Max percentage difference allowed is 15%
For DateTime properties, you can use ExactDateMatch to check for exact matches and PartialDateMatch for partial matches (same day and month, but different year, for example).
RuleFor(x => x.BirthDate)
.ExactDateMatch()
.PartialDateMatch();
The library allows chaining of comparison methods for each property. The highest similarity score from the chained methods is used for the final similarity calculation.
You can define your own custom comparison rules using the CustomComparison method. Here's an example:
RuleFor(x => x.FirstName)
.CustomComparison((firstName1, firstName2) =>
{
return firstName1.Equals(firstName2, StringComparison.OrdinalIgnoreCase) ? 100.0 : 0.0;
});
FluentSimilarity is a flexible and extensible library that allows you to define custom similarity rules for comparing objects. Whether you're comparing strings, numbers, or dates, this library provides a fluent API that simplifies the process of calculating similarity scores.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.