Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions FuzzySharp/Extractor/ResultExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ namespace FuzzySharp.Extractor
{
public static class ResultExtractor
{
public static IEnumerable<ExtractedResult<T>> ExtractWithoutOrder<T>(T query, IEnumerable<T> choices, Func<T,string> processor, IRatioScorer scorer, int cutoff = 0)
public static IEnumerable<ExtractedResult<T>> ExtractWithoutOrder<T>(string query, IEnumerable<T> choices, Func<T,string> processor, IRatioScorer scorer, int cutoff = 0)
{
int index = 0;
var processedQuery = processor(query);
foreach (var choice in choices)
{
int score = scorer.Score(processedQuery, processor(choice));
int score = scorer.Score(query, processor(choice));
if (score >= cutoff)
{
yield return new ExtractedResult<T>(choice, score, index);
Expand All @@ -23,17 +22,37 @@ public static IEnumerable<ExtractedResult<T>> ExtractWithoutOrder<T>(T query, IE
}
}


public static IEnumerable<ExtractedResult<T>> ExtractWithoutOrder<T>(T query, IEnumerable<T> choices, Func<T,string> processor, IRatioScorer scorer, int cutoff = 0)
{
return ExtractWithoutOrder(processor(query), choices, processor, scorer, cutoff);
}

public static ExtractedResult<T> ExtractOne<T>(T query, IEnumerable<T> choices, Func<T, string> processor, IRatioScorer calculator, int cutoff = 0)
{
return ExtractWithoutOrder(query, choices, processor, calculator, cutoff).Max();
}
public static ExtractedResult<T> ExtractOne<T>(string query, IEnumerable<T> choices, Func<T, string> processor, IRatioScorer calculator, int cutoff = 0)
{
return ExtractWithoutOrder(query, choices, processor, calculator, cutoff).Max();
}

public static IEnumerable<ExtractedResult<T>> ExtractSorted<T>(T query, IEnumerable<T> choices, Func<T, string> processor, IRatioScorer calculator, int cutoff = 0)
{
return ExtractWithoutOrder(query, choices, processor, calculator, cutoff).OrderByDescending(r => r.Score);
}

public static IEnumerable<ExtractedResult<T>> ExtractSorted<T>(string query, IEnumerable<T> choices, Func<T, string> processor, IRatioScorer calculator, int cutoff = 0)
{
return ExtractWithoutOrder(query, choices, processor, calculator, cutoff).OrderByDescending(r => r.Score);
}

public static IEnumerable<ExtractedResult<T>> ExtractTop<T>(T query, IEnumerable<T> choices, Func<T, string> processor, IRatioScorer calculator, int limit, int cutoff = 0)
{
return ExtractWithoutOrder(query, choices, processor, calculator, cutoff).MaxN(limit).Reverse();
}

public static IEnumerable<ExtractedResult<T>> ExtractTop<T>(string query, IEnumerable<T> choices, Func<T, string> processor, IRatioScorer calculator, int limit, int cutoff = 0)
{
return ExtractWithoutOrder(query, choices, processor, calculator, cutoff).MaxN(limit).Reverse();
}
Expand Down
85 changes: 85 additions & 0 deletions FuzzySharp/Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ public static IEnumerable<ExtractedResult<string>> ExtractAll(
return ResultExtractor.ExtractWithoutOrder(query, choices, processor, scorer, cutoff);
}

/// <summary>
/// Creates a list of ExtractedResult which contain all the choices with
/// their corresponding score where higher is more similar
/// </summary>
/// <param name="query"></param>
/// <param name="choices"></param>
/// <param name="processor"></param>
/// <param name="scorer"></param>
/// <param name="cutoff"></param>
/// <returns></returns>
public static IEnumerable<ExtractedResult<T>> ExtractAll<T>(
string query,
IEnumerable<T> choices,
Func<T, string> processor,
IRatioScorer scorer = null,
int cutoff = 0)
{
if (scorer == null) scorer = s_defaultScorer;
return ResultExtractor.ExtractWithoutOrder(query, choices, processor, scorer, cutoff);
}

/// <summary>
/// Creates a list of ExtractedResult which contain all the choices with
/// their corresponding score where higher is more similar
Expand Down Expand Up @@ -84,6 +105,30 @@ public static IEnumerable<ExtractedResult<string>> ExtractTop(
}


/// <summary>
/// Creates a sorted list of ExtractedResult which contain the
/// top limit most similar choices
/// </summary>
/// <param name="query"></param>
/// <param name="choices"></param>
/// <param name="processor"></param>
/// <param name="scorer"></param>
/// <param name="limit"></param>
/// <param name="cutoff"></param>
/// <returns></returns>
public static IEnumerable<ExtractedResult<T>> ExtractTop<T>(
string query,
IEnumerable<T> choices,
Func<T, string> processor,
IRatioScorer scorer = null,
int limit = 5,
int cutoff = 0)
{
if (scorer == null) scorer = s_defaultScorer;
return ResultExtractor.ExtractTop(query, choices, processor, scorer, limit, cutoff);
}


/// <summary>
/// Creates a sorted list of ExtractedResult which contain the
/// top limit most similar choices
Expand Down Expand Up @@ -130,6 +175,26 @@ public static IEnumerable<ExtractedResult<string>> ExtractSorted(
return ResultExtractor.ExtractSorted(query, choices, processor, scorer, cutoff);
}

/// <summary>
/// Creates a sorted list of ExtractedResult with the closest matches first
/// </summary>
/// <param name="query"></param>
/// <param name="choices"></param>
/// <param name="processor"></param>
/// <param name="scorer"></param>
/// <param name="cutoff"></param>
/// <returns></returns>
public static IEnumerable<ExtractedResult<T>> ExtractSorted<T>(
string query,
IEnumerable<T> choices,
Func<T, string> processor,
IRatioScorer scorer = null,
int cutoff = 0)
{
if (scorer == null) scorer = s_defaultScorer;
return ResultExtractor.ExtractSorted(query, choices, processor, scorer, cutoff);
}

/// <summary>
/// Creates a sorted list of ExtractedResult with the closest matches first
/// </summary>
Expand Down Expand Up @@ -173,6 +238,26 @@ public static ExtractedResult<string> ExtractOne(
return ResultExtractor.ExtractOne(query, choices, processor, scorer, cutoff);
}

/// <summary>
/// Find the single best match above a score in a list of choices.
/// </summary>
/// <param name="query"></param>
/// <param name="choices"></param>
/// <param name="processor"></param>
/// <param name="scorer"></param>
/// <param name="cutoff"></param>
/// <returns></returns>
public static ExtractedResult<T> ExtractOne<T>(
string query,
IEnumerable<T> choices,
Func<T, string> processor,
IRatioScorer scorer = null,
int cutoff = 0)
{
if (scorer == null) scorer = s_defaultScorer;
return ResultExtractor.ExtractOne(query, choices, processor, scorer, cutoff);
}

/// <summary>
/// Find the single best match above a score in a list of choices.
/// </summary>
Expand Down