|
| 1 | +import ast |
1 | 2 | import requests
|
2 | 3 | import argparse
|
3 | 4 | from bs4 import BeautifulSoup
|
4 | 5 |
|
5 |
| - |
6 | 6 | class Poet:
|
7 | 7 | def __init__(self, word):
|
8 | 8 | self.word = word
|
@@ -78,6 +78,20 @@ def antonyms(self):
|
78 | 78 | return wordlist
|
79 | 79 |
|
80 | 80 |
|
| 81 | + def homophones(self): |
| 82 | + wordlist = [] |
| 83 | + url = "https://api.datamuse.com/words?rel_hom=" + self.word |
| 84 | + |
| 85 | + data = requests.get(url) |
| 86 | + |
| 87 | + for dlist in data: |
| 88 | + dlist = ast.literal_eval(dlist.decode("utf-8")) |
| 89 | + |
| 90 | + for d in dlist: |
| 91 | + wordlist.append(d["word"]) |
| 92 | + |
| 93 | + return wordlist |
| 94 | + |
81 | 95 | def meaning(self):
|
82 | 96 | string = self.word.split(" ")
|
83 | 97 | string = "-".join(string)
|
@@ -116,30 +130,38 @@ def display_wordlist(self, wordlist, num):
|
116 | 130 | parser.add_argument("-s", "--synonym", help="get synonym", action="store_true")
|
117 | 131 | parser.add_argument("-a", "--antonym", help="get antonyms", action="store_true")
|
118 | 132 | parser.add_argument("-m", "--meaning", help="get meaning", action="store_true")
|
| 133 | + parser.add_argument("-ho", "--homophones", help="get homophones", action="store_true") |
119 | 134 | parser.add_argument("-n", "--number", type=int, help="number of words should be returned", default=50)
|
120 | 135 |
|
121 | 136 | args = parser.parse_args()
|
122 | 137 |
|
123 | 138 | poet = Poet(args.word)
|
124 | 139 |
|
125 | 140 | if args.rhyme:
|
126 |
| - print("[*] Getting rhyming words for the word:", self.word,"...") |
| 141 | + print("[*] Getting rhyming words for the word:", args.word,"...") |
127 | 142 |
|
128 | 143 | wl = poet.rhyming_words()
|
129 | 144 | poet.display_wordlist(wl, args.number)
|
130 | 145 |
|
131 | 146 | if args.synonym:
|
132 |
| - print("[*] Getting synonyms for the word:", self.word, "...") |
| 147 | + print("[*] Getting synonyms for the word:", args.word, "...") |
133 | 148 |
|
134 | 149 | wl = poet.synonyms()
|
135 | 150 | poet.display_wordlist(wl, args.number)
|
136 | 151 |
|
137 | 152 | if args.antonym:
|
138 |
| - print("[*] Getting antonyms for the word:", self.word, "...") |
| 153 | + print("[*] Getting antonyms for the word:", args.word, "...") |
139 | 154 |
|
140 | 155 | wl = poet.antonyms()
|
141 | 156 | poet.display_wordlist(wl, args.number)
|
142 | 157 |
|
| 158 | + if args.homophones: |
| 159 | + print("[*] Getting homophones for the word:", args.word, "...") |
| 160 | + print("[!] Homophones are words that sound identical but are written differently [!]\n") |
| 161 | + wl = poet.homophones() |
| 162 | + poet.display_wordlist(wl, args.number) |
| 163 | + |
| 164 | + |
143 | 165 | if args.meaning:
|
144 | 166 | print("[*] Fetching meaning of the word...")
|
145 | 167 |
|
|
0 commit comments