New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented sort order matches by common letter count largest to smallest #295
Open
ZihangH
wants to merge
4
commits into
seatgeek:master
Choose a base branch
from
Not-Forrest:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
134074b
Implemented character count ordering for strings with the same score
Not-Forrest ccb4773
Fixed coding style errors
Not-Forrest 1406166
Added test cases for sort_by_common_letter_count feature.
ZihangH 38e3419
Fixed issue with sortByCommonLetter, fixed incorrect variable names.
Not-Forrest File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,7 +119,7 @@ def no_process(x): | |
yield (choice, score) | ||
|
||
|
||
def extract(query, choices, processor=default_processor, scorer=default_scorer, limit=5): | ||
def extract(query, choices, processor=default_processor, scorer=default_scorer, limit=5, char_sort=False): | ||
"""Select the best match in a list or dictionary of choices. | ||
|
||
Find best matches in a list or dictionary of choices, return a | ||
|
@@ -165,11 +165,17 @@ def extract(query, choices, processor=default_processor, scorer=default_scorer, | |
[('train', 22, 'bard'), ('man', 0, 'dog')] | ||
""" | ||
sl = extractWithoutOrder(query, choices, processor, scorer) | ||
return heapq.nlargest(limit, sl, key=lambda i: i[1]) if limit is not None else \ | ||
sorted(sl, key=lambda i: i[1], reverse=True) | ||
if char_sort is False: | ||
return heapq.nlargest(limit, sl, key=lambda i: i[1]) if limit is not None else \ | ||
sorted(sl, key=lambda i: i[1], reverse=True) | ||
else: | ||
sl = sorted(sl, key=lambda i: i[1], reverse=True) | ||
return sortByCommonLetter(sl, query)[0: min(limit, len(sl))] if limit is not None else \ | ||
sortByCommonLetter(sl, query) | ||
|
||
|
||
def extractBests(query, choices, processor=default_processor, scorer=default_scorer, score_cutoff=0, limit=5): | ||
def extractBests(query, choices, processor=default_processor, scorer=default_scorer, score_cutoff=0, limit=5, | ||
char_sort=False): | ||
"""Get a list of the best matches to a collection of choices. | ||
|
||
Convenience function for getting the choices with best scores. | ||
|
@@ -188,13 +194,17 @@ def extractBests(query, choices, processor=default_processor, scorer=default_sco | |
|
||
Returns: A a list of (match, score) tuples. | ||
""" | ||
|
||
best_list = extractWithoutOrder(query, choices, processor, scorer, score_cutoff) | ||
return heapq.nlargest(limit, best_list, key=lambda i: i[1]) if limit is not None else \ | ||
sorted(best_list, key=lambda i: i[1], reverse=True) | ||
best_list = extractWithoutOrder(query, choices, processor, scorer) | ||
if char_sort is False: | ||
return heapq.nlargest(limit, best_list, key=lambda i: i[1]) if limit is not None else \ | ||
sorted(best_list, key=lambda i: i[1], reverse=True) | ||
else: | ||
best_list = sorted(best_list, key=lambda i: i[1], reverse=True) | ||
return sortByCommonLetter(best_list, query)[0: min(limit, len(best_list))] if limit is not None else \ | ||
sortByCommonLetter(best_list, query) | ||
|
||
|
||
def extractOne(query, choices, processor=default_processor, scorer=default_scorer, score_cutoff=0): | ||
def extractOne(query, choices, processor=default_processor, scorer=default_scorer, score_cutoff=0, char_sort=False): | ||
"""Find the single best match above a score in a list of choices. | ||
|
||
This is a convenience method which returns the single best choice. | ||
|
@@ -216,10 +226,70 @@ def extractOne(query, choices, processor=default_processor, scorer=default_score | |
was found that was above score_cutoff. Otherwise, returns None. | ||
""" | ||
best_list = extractWithoutOrder(query, choices, processor, scorer, score_cutoff) | ||
try: | ||
return max(best_list, key=lambda i: i[1]) | ||
except ValueError: | ||
return None | ||
if char_sort is False: | ||
try: | ||
return max(best_list, key=lambda i: i[1]) | ||
except ValueError: | ||
return None | ||
else: | ||
best_list = sorted(best_list, key=lambda i: i[1], reverse=True) | ||
try: | ||
return max(sortByCommonLetter(best_list, query), key=lambda i: i[1]) | ||
except ValueError: | ||
return None | ||
|
||
|
||
def sortByCommonLetter(sl, query): | ||
"""This function further sorts the strings with the same scores by common letter count to the query.""" | ||
current_score, last_index = -1, -1 | ||
# Iterate over list and look for words with the same scores | ||
for i in range(0, len(sl)): | ||
# Identify the indexes of the strings with the same scores | ||
if sl[i][1] != current_score or i == len(sl) - 1: | ||
current_score = sl[i][1] | ||
# First iteration, there are no previous words so we do not have to do anything | ||
if last_index == -1: | ||
last_index = i | ||
continue | ||
# Found a group of words with the same scores! Now sort them | ||
if i - last_index > 1: | ||
count_list = [] | ||
for j in range(last_index, i): | ||
count_list.append((sl[j][0], calculateCommonLetter(query, sl[j][0]))) | ||
count_list = sorted(count_list, key=lambda k: k[1], reverse=True) | ||
# Copy the sorted portion | ||
for j in range(0, len(count_list)): | ||
sl[last_index + j] = (count_list[j][0], current_score) | ||
last_index = i | ||
return sl | ||
Comment on lines
+242
to
+264
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This algorithm is broken for quite a few cases. A couple quick examples:
|
||
|
||
|
||
def calculateCommonLetter(s1, s2): | ||
char_dict = {} | ||
commonLetterCount = 0 | ||
s1 = utils.full_process(s1) | ||
s2 = utils.full_process(s2) | ||
for char in s1: | ||
if char in char_dict: | ||
char_dict[char] += 1 | ||
else: | ||
char_dict[char] = 1 | ||
|
||
for char in s2: | ||
if char in char_dict: | ||
commonLetterCount += 1 | ||
char_dict[char] -= 1 | ||
if char_dict[char] == 0: | ||
del char_dict[char] | ||
# Add penalty for extra letters | ||
else: | ||
commonLetterCount -= 1 | ||
|
||
# Add penalty for missing letters | ||
for char in char_dict: | ||
commonLetterCount -= 1 | ||
|
||
return commonLetterCount | ||
|
||
|
||
def dedupe(contains_dupes, threshold=70, scorer=fuzz.token_set_ratio): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from a performance standpoint this is a bad idea. When the user is only interested in 5 elements (the default), but has e.g. 1 million choices, this will sort 1 million choices (with a slow algorithm, since it counts all the letters) and then only takes the best 5 elements.