Skip to content

Commit 37e63eb

Browse files
committed
NEW: homophones() function to calculate homophones
1 parent d149150 commit 37e63eb

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

script.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import ast
12
import requests
23
import argparse
34
from bs4 import BeautifulSoup
45

5-
66
class Poet:
77
def __init__(self, word):
88
self.word = word
@@ -78,6 +78,20 @@ def antonyms(self):
7878
return wordlist
7979

8080

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+
8195
def meaning(self):
8296
string = self.word.split(" ")
8397
string = "-".join(string)
@@ -116,30 +130,38 @@ def display_wordlist(self, wordlist, num):
116130
parser.add_argument("-s", "--synonym", help="get synonym", action="store_true")
117131
parser.add_argument("-a", "--antonym", help="get antonyms", action="store_true")
118132
parser.add_argument("-m", "--meaning", help="get meaning", action="store_true")
133+
parser.add_argument("-ho", "--homophones", help="get homophones", action="store_true")
119134
parser.add_argument("-n", "--number", type=int, help="number of words should be returned", default=50)
120135

121136
args = parser.parse_args()
122137

123138
poet = Poet(args.word)
124139

125140
if args.rhyme:
126-
print("[*] Getting rhyming words for the word:", self.word,"...")
141+
print("[*] Getting rhyming words for the word:", args.word,"...")
127142

128143
wl = poet.rhyming_words()
129144
poet.display_wordlist(wl, args.number)
130145

131146
if args.synonym:
132-
print("[*] Getting synonyms for the word:", self.word, "...")
147+
print("[*] Getting synonyms for the word:", args.word, "...")
133148

134149
wl = poet.synonyms()
135150
poet.display_wordlist(wl, args.number)
136151

137152
if args.antonym:
138-
print("[*] Getting antonyms for the word:", self.word, "...")
153+
print("[*] Getting antonyms for the word:", args.word, "...")
139154

140155
wl = poet.antonyms()
141156
poet.display_wordlist(wl, args.number)
142157

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+
143165
if args.meaning:
144166
print("[*] Fetching meaning of the word...")
145167

0 commit comments

Comments
 (0)