Repository navigation
BHoM Tkinter: searchable list box - #255
Open
Felix-Mallinder wants to merge 2 commits into
Open
Felix-Mallinder wants to merge 2 commits into
Felix-Mallinder wants to merge 2 commits into
Conversation
Tom-Kingstone
left a comment
Contributor
There was a problem hiding this comment.
see comments below
Comment on lines
+34
to
+67
| def fuzzy_score(query: str, text: str) -> Optional[float]: | ||
| """Score how well `query` matches `text` (higher is better), or None if it does not match. | ||
|
|
||
| Every whitespace-separated word of the query must match the text, in one of these ways (best first): | ||
| - contained in the text, with a bonus when it starts a word ("lon" in "London_TRY") | ||
| - its letters appear in order with only small gaps ("ldn" in "London"), scored down by the gaps | ||
| - a close misspelling of one of the text's words ("birmigham" for "Birmingham"), words of 4+ letters | ||
| An empty query matches everything with score 0. | ||
| """ | ||
| text_l = text.lower() | ||
| tokens = [t for t in query.lower().split() if t] | ||
| if not tokens: | ||
| return 0.0 | ||
| words = [w for w in _WORD_SPLIT.split(text_l) if w] | ||
| score = 0.0 | ||
| for token in tokens: | ||
| index = text_l.find(token) | ||
| if index >= 0: | ||
| starts_word = index == 0 or not text_l[index - 1].isalnum() | ||
| score += 100 + (20 if starts_word else 0) - min(index, 20) * 0.5 | ||
| continue | ||
| gaps = _subsequence_gaps(token, text_l) | ||
| # Letters in order only count when they are close together, otherwise short words match almost anything. | ||
| if gaps is not None and gaps <= max(3, len(token)): | ||
| score += max(60 - gaps * 4, 20) | ||
| continue | ||
| similar = [w for w in words if abs(len(w) - len(token)) <= _TYPO_MAX_LENGTH_DIFF] | ||
| if len(token) >= _TYPO_MIN_LENGTH and similar: | ||
| ratio = max(difflib.SequenceMatcher(None, token, w).ratio() for w in similar) | ||
| if ratio >= _TYPO_MIN_RATIO: | ||
| score += ratio * 50 | ||
| continue | ||
| return None | ||
| return score |
Contributor
There was a problem hiding this comment.
is there not a fuzzy matching library for python? We use one for c# so if you want we could do this via pythonnet instead, I don't see why we need to implement our own fuzzy match method
Comment on lines
+164
to
+165
| def _visible(self) -> List[str]: | ||
| return self.list_box.get_options() |
Contributor
There was a problem hiding this comment.
what does this mean???
if you need a private alias for self.list_box.get_options(), you might want to call the method _get_options instead. This will make other methods that use this easier to read
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
NOTE: Depends on
Issues addressed by this PR
Closes #243
Test files
Quick test at the bottom of file
Changelog
SearchableListBoxAdditional comments