Skip to content

Commit

Permalink
Merge pull request #10 from vforteli/fix/optimizations
Browse files Browse the repository at this point in the history
Fix/optimizations
  • Loading branch information
vforteli authored Nov 11, 2023
2 parents 5e6de8f + 3b7c6d5 commit d88cac4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion FuzzySearchNet/FuzzySearchNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<PropertyGroup>
<PackageId>FuzzySearch.Net</PackageId>
<VersionPrefix>0.3.2</VersionPrefix>
<VersionPrefix>0.3.3</VersionPrefix>
<Title>FuzzySearch.Net</Title>
<Authors>Verner Fortelius</Authors>
<Description>Fuzzy search library for finding strings in strings. Inspired by and attempts to be somewhat compatible with fuzzysearch for python https://github.com/taleinat/fuzzysearch</Description>
Expand Down
21 changes: 12 additions & 9 deletions FuzzySearchNet/src/FuzzySearchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@
public class FuzzySearchOptions
{
public int MaxTotalDistance { get; private set; }
public int? MaxSubstitutions { get; private set; }
public int? MaxDeletions { get; private set; }
public int? MaxInsertions { get; private set; }
public int MaxSubstitutions { get; private set; }
public int MaxDeletions { get; private set; }
public int MaxInsertions { get; private set; }

/// <summary>
/// Specify total maximum distance
/// </summary>
public FuzzySearchOptions(int maxTotalDistance)
{
MaxTotalDistance = maxTotalDistance;
MaxSubstitutions = maxTotalDistance;
MaxDeletions = maxTotalDistance;
MaxInsertions = maxTotalDistance;
}

/// <summary>
/// Specify total maximum distance and optionally limit substitutions, deletions and insertions individually
/// </summary>
public FuzzySearchOptions(int maxTotalDistance, int? maxSubstitutions = null, int? maxDeletions = null, int? maxInsertions = null) : this(maxTotalDistance)
{
MaxSubstitutions = maxSubstitutions;
MaxDeletions = maxDeletions;
MaxInsertions = maxInsertions;
MaxSubstitutions = maxSubstitutions ?? maxTotalDistance;
MaxDeletions = maxDeletions ?? maxTotalDistance;
MaxInsertions = maxInsertions ?? maxTotalDistance;
}

/// <summary>
Expand All @@ -39,9 +42,9 @@ public FuzzySearchOptions(int maxSubstitutions, int maxDeletions, int maxInserti
MaxTotalDistance = maxDeletions + maxInsertions + maxSubstitutions;
}

public bool CanSubstitute(int currentTotalDistance, int currentSubstitutions) => (!MaxSubstitutions.HasValue || currentSubstitutions < MaxSubstitutions) && (currentTotalDistance < MaxTotalDistance);
public bool CanSubstitute(int currentTotalDistance, int currentSubstitutions) => currentSubstitutions < MaxSubstitutions && currentTotalDistance < MaxTotalDistance;

public bool CanDelete(int currentTotalDistance, int currentDeletions) => (!MaxDeletions.HasValue || currentDeletions < MaxDeletions) && (currentTotalDistance < MaxTotalDistance);
public bool CanDelete(int currentTotalDistance, int currentDeletions) => currentDeletions < MaxDeletions && currentTotalDistance < MaxTotalDistance;

public bool CanInsert(int currentTotalDistance, int currentInsertions) => (!MaxInsertions.HasValue || currentInsertions < MaxInsertions) && (currentTotalDistance < MaxTotalDistance);
public bool CanInsert(int currentTotalDistance, int currentInsertions) => currentInsertions < MaxInsertions && currentTotalDistance < MaxTotalDistance;
}
12 changes: 6 additions & 6 deletions FuzzySearchNet/src/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ public static IEnumerable<MatchResult> GetBestMatches(IEnumerable<MatchResult> m
{
group.Add(matchesEnumerator.Current);

var match = matchesEnumerator.Current;
var matchStartIndex = matchesEnumerator.Current.StartIndex;

while (matchesEnumerator.MoveNext())
{
var currentMatch = matchesEnumerator.Current;

if (currentMatch != null)
{
if (currentMatch.StartIndex > (match.StartIndex + maxDistanece))
if (currentMatch.StartIndex > (matchStartIndex + maxDistanece))
{
yield return group.OrderBy(o => o.Distance).ThenByDescending(o => o.Match.Length).First();
group.Clear();
}

group.Add(currentMatch);

match = currentMatch;
matchStartIndex = currentMatch.StartIndex;
}
}
}
Expand All @@ -58,23 +58,23 @@ public static async IAsyncEnumerable<MatchResult> GetBestMatchesAsync(IAsyncEnum
{
group.Add(matchesEnumerator.Current);

var match = matchesEnumerator.Current;
var matchStartIndex = matchesEnumerator.Current.StartIndex;

while (await matchesEnumerator.MoveNextAsync())
{
var currentMatch = matchesEnumerator.Current;

if (currentMatch != null)
{
if (currentMatch.StartIndex > (match.StartIndex + maxDistanece))
if (currentMatch.StartIndex > (matchStartIndex + maxDistanece))
{
yield return group.OrderBy(o => o.Distance).ThenByDescending(o => o.Match.Length).First();
group.Clear();
}

group.Add(currentMatch);

match = currentMatch;
matchStartIndex = currentMatch.StartIndex;
}
}
}
Expand Down

0 comments on commit d88cac4

Please sign in to comment.