Last active
June 8, 2023 10:44
-
-
Save mkakh/c364793c06cfd2acb96199ad5db2b9af to your computer and use it in GitHub Desktop.
created by BingAI
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
| #!/usr/bin/env python3 | |
| import os | |
| from PyDictionary import PyDictionary | |
| from gruut import sentences | |
| dictionary = PyDictionary() | |
| def get_meaning(word): | |
| # check if dict.txt exists | |
| if not os.path.exists('dict.txt'): | |
| open('dict.txt', 'w').close() | |
| # check if the word is already contained in dict.txt | |
| with open('dict.txt', 'r') as f: | |
| contents = f.read() | |
| if f'[{word}]' in contents: | |
| start_index = contents.index(f'[{word}]') + len(word) + 3 | |
| end_index = contents.index('\n\n', start_index) | |
| return contents[start_index:end_index] | |
| # search meaning with PyDictionary | |
| meaning = dictionary.meaning(word, disable_errors=True) | |
| if meaning is None: | |
| return f'Sorry, no meaning was found for "{word}".' | |
| formatted_meaning = '\n'.join([f'{k}: {", ".join(v)}' for k, v in meaning.items()]) | |
| with open('dict.txt', 'a') as f: | |
| f.write(f'[{word}]\n{formatted_meaning}\n\n') | |
| return formatted_meaning | |
| try: | |
| while True: | |
| word = input('Enter a word (or "q" to quit): ') | |
| if word == 'q': | |
| break | |
| formatted_meaning = get_meaning(word) | |
| # get IPA pronunciation with gruut | |
| for sent in sentences(word, lang="en-us"): | |
| for w in sent: | |
| if w.phonemes: | |
| ipa = ''.join(w.phonemes) | |
| if ipa: | |
| print(f'[{word}]({ipa})\n{formatted_meaning}\n') | |
| else: | |
| print(f'[{word}]\n{formatted_meaning}\n') | |
| except (KeyboardInterrupt, EOFError): | |
| print('\nExiting...') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment