Skip to content

Commit

Permalink
Merge pull request #46 from burdoto/typebounds
Browse files Browse the repository at this point in the history
Implemented referents for the extraction
  • Loading branch information
xdrop authored Oct 29, 2018
2 parents 3481d37 + d494ebc commit 70978f4
Show file tree
Hide file tree
Showing 4 changed files with 412 additions and 53 deletions.
88 changes: 75 additions & 13 deletions src/me/xdrop/fuzzywuzzy/Extractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,34 @@ public Extractor with(int cutoff) {
* @param func The function to apply
* @return The list of results
*/
public List<ExtractedResult> extractWithoutOrder(String query, Collection<String> choices, Applicable func) {
public List<ExtractedResult<String>> extractWithoutOrder(String query, Collection<String> choices,
Applicable func) {
return extractWithoutOrder(query, choices, ToStringFunction.DEFAULT, func);
}

/**
* Returns the list of choices with their associated scores of similarity in a list
* of {@link ExtractedResult}
*
* @param query The query string
* @param choices The list of choices
* @param toStringFunction The ToStringFunction to be applied to all choices.
* @param func The function to apply
* @return The list of results
*/
public <T> List<ExtractedResult<T>> extractWithoutOrder(String query, Collection<T> choices,
ToStringFunction<T> toStringFunction, Applicable func) {

List<ExtractedResult> yields = new ArrayList<>();
List<ExtractedResult<T>> yields = new ArrayList<>();
int index = 0;

for (String s : choices) {
for (T t : choices) {

String s = toStringFunction.apply(t);
int score = func.apply(query, s);

if (score >= cutoff) {
yields.add(new ExtractedResult(s, score, index));
yields.add(new ExtractedResult<>(t, s, score, index));
}
index++;
}
Expand All @@ -57,13 +74,27 @@ public List<ExtractedResult> extractWithoutOrder(String query, Collection<String
* Find the single best match above a score in a list of choices.
*
* @param query A string to match against
* @param choice A list of choices
* @param choices A list of choices
* @param func Scoring function
* @return An object containing the best match and it's score
*/
public ExtractedResult extractOne(String query, Collection<String> choice, Applicable func) {
public ExtractedResult<String> extractOne(String query, Collection<String> choices, Applicable func) {
return extractOne(query, choices, ToStringFunction.DEFAULT, func);
}

List<ExtractedResult> extracted = extractWithoutOrder(query, choice, func);
/**
* Find the single best match above a score in a list of choices.
*
* @param query A string to match against
* @param choices A list of choices
* @param toStringFunction The ToStringFunction to be applied to all choices.
* @param func Scoring function
* @return An object containing the best match and it's score
*/
public <T> ExtractedResult<T> extractOne(String query, Collection<T> choices, ToStringFunction<T> toStringFunction,
Applicable func) {

List<ExtractedResult<T>> extracted = extractWithoutOrder(query, choices, toStringFunction, func);

return Collections.max(extracted);

Expand All @@ -78,10 +109,25 @@ public ExtractedResult extractOne(String query, Collection<String> choice, Appli
* @param func The scoring function
* @return A list of the results
*/
public List<ExtractedResult> extractTop(String query, Collection<String> choices, Applicable func) {
public List<ExtractedResult<String>> extractTop(String query, Collection<String> choices, Applicable func) {
return extractTop(query, choices, ToStringFunction.DEFAULT, func);
}

List<ExtractedResult> best = extractWithoutOrder(query, choices, func);
Collections.sort(best, Collections.<ExtractedResult>reverseOrder());
/**
* Creates a <b>sorted</b> list of {@link ExtractedResult} which contain the
* top @param limit most similar choices
*
* @param query The query string
* @param choices A list of choices
* @param toStringFunction The ToStringFunction to be applied to all choices.
* @param func The scoring function
* @return A list of the results
*/
public <T> List<ExtractedResult<T>> extractTop(String query, Collection<T> choices,
ToStringFunction<T> toStringFunction, Applicable func) {

List<ExtractedResult<T>> best = extractWithoutOrder(query, choices, toStringFunction, func);
Collections.sort(best, Collections.<ExtractedResult<T>>reverseOrder());

return best;
}
Expand All @@ -96,11 +142,27 @@ public List<ExtractedResult> extractTop(String query, Collection<String> choices
* the search (k-top heap sort) is used
* @return A list of the results
*/
public List<ExtractedResult> extractTop(String query, Collection<String> choices, Applicable func, int limit) {
public List<ExtractedResult<String>> extractTop(String query, Collection<String> choices, Applicable func, int limit) {
return extractTop(query, choices, ToStringFunction.DEFAULT, func, limit);
}

/**
* Creates a <b>sorted</b> list of {@link ExtractedResult} which contain the
* top @param limit most similar choices
*
* @param query The query string
* @param choices A list of choices
* @param toStringFunction The ToStringFunction to be applied to all choices.
* @param limit Limits the number of results and speeds up
* the search (k-top heap sort) is used
* @return A list of the results
*/
public <T> List<ExtractedResult<T>> extractTop(String query, Collection<T> choices,
ToStringFunction<T> toStringFunction, Applicable func, int limit) {

List<ExtractedResult> best = extractWithoutOrder(query, choices, func);
List<ExtractedResult<T>> best = extractWithoutOrder(query, choices, toStringFunction, func);

List<ExtractedResult> results = Utils.findTopKHeap(best, limit);
List<ExtractedResult<T>> results = Utils.findTopKHeap(best, limit);
Collections.reverse(results);

return results;
Expand Down
Loading

0 comments on commit 70978f4

Please # to comment.