check if two words are related to each other

前端 未结 3 1046
傲寒
傲寒 2020-12-18 09:56

I have two lists: one, the interests of the user; and second, the keywords about a book. I want to recommend the book to the user based on his given interests list. I am usi

3条回答
  •  醉话见心
    2020-12-18 10:14

    Firstly a word can have many senses and when you try to find similar words you might need some word sense disambiguation http://en.wikipedia.org/wiki/Word-sense_disambiguation.

    Given a pair of words, if we take the most similar pair of senses as the gauge of whether two words are similar, we can try this:

    from nltk.corpus import wordnet as wn
    from itertools import product
    
    wordx, wordy = "cat","dog"
    sem1, sem2 = wn.synsets(wordx), wn.synsets(wordy)
    
    maxscore = 0
    for i,j in list(product(*[sem1,sem2])):
      score = i.wup_similarity(j) # Wu-Palmer Similarity
      maxscore = score if maxscore < score else maxscore
    

    There are other similarity functions that you can use. http://nltk.googlecode.com/svn/trunk/doc/howto/wordnet.html. The only problem is when you encounter words not in wordnet. Then i suggest you fallback on difflib.

提交回复
热议问题