I have a list of words
Nội dung chính
- Not the answer you're looking for? Browse other questions tagged python or ask your own question.
- How to Find Synonyms of a Word with NLTK WordNet and Python?
- How to Find Antonyms of a Word with NLTK WordNet and Python?
- How to use a custom Python Function for Finding Synonyms and Antonyms with NLTK WordNet?
- How to use POS Tagging for Synonym and Antonym Finding with NLTK WordNet?
- How to extract synonyms and antonyms from other languages besides English via NLTK Wordnet?
- What other Lexical Semantics can be extracted with NLTK WordNet besides Antonyms and Synonyms?
- How to Find Hypernym of a Word with NLTK WordNet and Python?
- How to Find Hyponym of a Word with NLTK WordNet and Python?
- How to Find Verb Frames of a Verb with NLTK WordNet and Python?
- How to Find Similar Words for a targeted Word with NLTK WordNet and Python?
- How to Find Topic Domains of a Word with NLTK WordNet and Python?
- How to Find Region Domains of a Word with NLTK WordNet and Python?
- How to Find Usage Domains of a Word with NLTK WordNet and Python?
- How to Use WordNet for other languages with Python NLTK?
- Last Thoughts on NLTK WordNet and Holistic SEO
- How do you get similar words in Python?
- How do you find two similar strings in Python?
- What is a synonym for Python?
list = ['car', 'animal', 'house', 'animation']
and I want to compare every list item with a string str1
and the output should be the most similar word. Example: If str1
would be anlmal
then animal
is the most similar word. How can I do this in python? Usually the words I have in my list are good distinguishable from each other.
asked Oct 9, 2014 at 16:42
0
Use difflib:
difflib.get_close_matches[word, ['car', 'animal', 'house', 'animation']]
As you can see from perusing the source, the "close" matches are sorted from best to worst.
>>> import difflib
>>> difflib.get_close_matches['anlmal', ['car', 'animal', 'house', 'animation']]
['animal']
answered Oct 9, 2014 at 16:44
mgilsonmgilson
287k60 gold badges600 silver badges673 bronze badges
1
I checked difflib.get_close_matches[], but it didn't work for me correctly. I write here a robust solution, use as:
closest_match, closest_match_idx = find_closet_match[test_str, list2check]
def find_closet_match[test_str, list2check]:
scores = {}
for ii in list2check:
cnt = 0
if len[test_str]