I'm trying to find the book name from an array of book titles that is closest to the one entered by the user.
However, this doesn't really work as I want it to; using "harry potter" as the search string doesn't actually yield any harry potter books. It works if you get more specific like writing "Harry Potter and the Sorcerer", but how can I make it just detect with "harry potter"? (see attached images)
#๐ Difflib get_close_matches() not working as expected
12 messages ยท Page 1 of 1 (latest)
@sour sluice
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
this is being used on finding similarity from typo instead of matching content
!d difflib
Source code: Lib/difflib.py
This module provides classes and functions for comparing sequences. It can be used for example, for comparing files, and can produce information about file differences in various formats, including HTML and context and unified diffs. For comparing directories and files, see also, the filecmp module.
difflib.get_close_matches(word, possibilities, n=3, cutoff=0.6)```
Return a list of the best โgood enoughโ matches. *word* is a sequence for which close matches are desired (typically a string), and *possibilities* is a list of sequences against which to match *word* (typically a list of strings).
Optional argument *n* (default `3`) is the maximum number of close matches to return; *n* must be greater than `0`.
Optional argument *cutoff* (default `0.6`) is a float in the range \[0, 1]. Possibilities that donโt score at least that similar to *word* are ignored.
The best (no more than *n*) matches among the possibilities are returned in a list, sorted by similarity score, most similar first.
for this usecase, I would recommend either find some algorithm that is helpful, or you can build algorithm to per-word base where it return the result containing matching word
are there any already built in / popular algorithms for this? im not sure where to start; stuff like Levenshtein distance would also not work for this application right?
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.