Created
June 22, 2021 07:32
-
-
Save mshavliuk/83b4439fadb294e96c07ac276424d83a to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
from itertools import product | |
from nltk.corpus import wordnet | |
def test_words(word1: str, word2: str): | |
meanings1 = wordnet.synsets(word1, lang="eng") | |
meanings2 = wordnet.synsets(word2, lang="eng") | |
similarities = [] | |
for sense1, sense2 in product(meanings1, meanings2): | |
score = sense1.wup_similarity(sense2) | |
similarities.append((score, sense1, sense2)) | |
similarities = sorted(similarities, key=lambda tup: tup[1], reverse=True) | |
for score, sense1, sense2 in similarities: | |
if score is None: | |
continue | |
print("{:3.2f} - {} {}".format(score, sense1, sense2)) | |
def main(): | |
test_words("burglary", "shoplifting") | |
test_words("robbery", "vandalism") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Console output: