Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,43 @@
|
|
1 |
import streamlit as st
|
2 |
import itertools
|
3 |
-
from nltk.corpus import wordnet
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
6 |
synonyms = set()
|
|
|
|
|
|
|
|
|
7 |
for syn in wordnet.synsets(word):
|
8 |
for lemma in syn.lemmas():
|
9 |
synonyms.add(lemma.name())
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def generate_words(letters, length=None):
|
|
|
13 |
permutations = set()
|
14 |
for i in range(1, len(letters) + 1):
|
15 |
for p in itertools.permutations(letters, i):
|
16 |
word = "".join(p)
|
17 |
-
if length is None or len(word) == length:
|
18 |
permutations.add(word)
|
19 |
return permutations
|
20 |
|
@@ -28,8 +51,15 @@ if letters:
|
|
28 |
words = generate_words(letters, length=word_length if word_length > 0 else None)
|
29 |
st.write(words)
|
30 |
|
31 |
-
st.header("Thesaurus Lookup")
|
32 |
-
selected_word = st.selectbox("Select a word to look up
|
33 |
if selected_word:
|
34 |
-
|
35 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import itertools
|
3 |
+
from nltk.corpus import wordnet, words
|
4 |
|
5 |
+
# Download the necessary resources
|
6 |
+
nltk.download("wordnet")
|
7 |
+
nltk.download("words")
|
8 |
+
|
9 |
+
def get_related_words(word):
|
10 |
synonyms = set()
|
11 |
+
antonyms = set()
|
12 |
+
hypernyms = set()
|
13 |
+
hyponyms = set()
|
14 |
+
|
15 |
for syn in wordnet.synsets(word):
|
16 |
for lemma in syn.lemmas():
|
17 |
synonyms.add(lemma.name())
|
18 |
+
if lemma.antonyms():
|
19 |
+
antonyms.add(lemma.antonyms()[0].name())
|
20 |
+
|
21 |
+
for hyper in syn.hypernyms():
|
22 |
+
hypernyms.update(lemma.name() for lemma in hyper.lemmas())
|
23 |
+
|
24 |
+
for hypo in syn.hyponyms():
|
25 |
+
hyponyms.update(lemma.name() for lemma in hypo.lemmas())
|
26 |
+
|
27 |
+
return {
|
28 |
+
"synonyms": list(synonyms),
|
29 |
+
"antonyms": list(antonyms),
|
30 |
+
"hypernyms": list(hypernyms),
|
31 |
+
"hyponyms": list(hyponyms),
|
32 |
+
}
|
33 |
|
34 |
def generate_words(letters, length=None):
|
35 |
+
english_words = set(words.words())
|
36 |
permutations = set()
|
37 |
for i in range(1, len(letters) + 1):
|
38 |
for p in itertools.permutations(letters, i):
|
39 |
word = "".join(p)
|
40 |
+
if (length is None or len(word) == length) and word in english_words:
|
41 |
permutations.add(word)
|
42 |
return permutations
|
43 |
|
|
|
51 |
words = generate_words(letters, length=word_length if word_length > 0 else None)
|
52 |
st.write(words)
|
53 |
|
54 |
+
st.header("Thesaurus and Related Words Lookup")
|
55 |
+
selected_word = st.selectbox("Select a word to look up related words:", [""] + sorted(words))
|
56 |
if selected_word:
|
57 |
+
related_words = get_related_words(selected_word)
|
58 |
+
st.subheader("Synonyms")
|
59 |
+
st.write(related_words["synonyms"])
|
60 |
+
st.subheader("Antonyms")
|
61 |
+
st.write(related_words["antonyms"])
|
62 |
+
st.subheader("Hypernyms (more general terms)")
|
63 |
+
st.write(related_words["hypernyms"])
|
64 |
+
st.subheader("Hyponyms (more specific terms)")
|
65 |
+
st.write(related_words["hyponyms"])
|