Created
November 7, 2020 11:00
-
-
Save shinysu/d4c25f9b7f043fa674c092608f31b39d to your computer and use it in GitHub Desktop.
Wordbot using wordnet
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
""" | |
bot.py : Given a word, gets the meaning, synonyms and antonyms for the word | |
""" | |
import PySimpleGUI as sg | |
from utils import get_meaning | |
greeting = "Hi, I am a word bot. I can help you with words\n" | |
layout = [ | |
[sg.Multiline(greeting, font=("Arial", 14), size=(70, 15), key='output')], | |
[sg.InputText("", font=("Arial", 14), size=(50, 1), key='input', enable_events=True)], | |
[sg.Button("Meaning", font=("Arial", 14), bind_return_key=True, key='meaning'), | |
sg.Button("Synonyms", font=("Arial", 14), key='synonym'), | |
sg.Button("Antonyms", font=("Arial", 14), key='antonym'), | |
sg.Button("Clear", font=("Arial", 14), key='clear') | |
] | |
] | |
def display_meaning(word): | |
""" | |
Displays the word and the meaning of the word | |
:param word: string, input word | |
""" | |
meaning = get_meaning(word) | |
window['output'].print("WORD: " + word) | |
if meaning: | |
window['output'].print("MEANING: ", meaning) | |
else: | |
display_error("Word is not found in corpus") | |
def display_error(message): | |
""" | |
Displays an error message in the output window | |
:param message: string, the error message to be displayed | |
""" | |
window['output'].print("ERROR: " + message, text_color='red') | |
if __name__ == '__main__': | |
window = sg.Window('File Explorer', layout) | |
while True: | |
event, values = window.Read() | |
if event == sg.WINDOW_CLOSED: | |
break | |
elif event == 'meaning': | |
display_meaning(values['input']) | |
window.Close() |
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
from nltk.corpus import wordnet as wn | |
def get_meaning(word): | |
""" | |
get the meaning of the given word from wordnet | |
:param word: string, the word for which meaning is to be found | |
:return: string, the meaning of the word if the word is found in corpus; None otherwise | |
""" | |
synset = wn.synsets(word) | |
if synset: | |
return synset[0].definition() | |
else: | |
return None | |
def get_synonyms(word): | |
""" | |
get the synonyms for the given word from wordnet | |
:param word: string, the word for which synonyms are to be found | |
:return: the synonyms for the word | |
""" | |
synonyms = [] | |
for synset in wn.synsets(word): | |
for lemma in synset.lemmas(): | |
if lemma: | |
synonyms.append(lemma.name()) | |
return set(synonyms) | |
def get_antonyms(word): | |
""" | |
get the antonyms for the given word from wordnet | |
:param word: string, the word for which antonyms are to be found | |
:return: the antonyms for the word | |
""" | |
antonyms = [] | |
for synset in wn.synsets(word): | |
for lemma in synset.lemmas(): | |
if lemma.antonyms(): | |
antonyms.append(lemma.antonyms()[0].name()) | |
return set(antonyms) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment