Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import matplotlib.pyplot as plt
|
2 |
+
from wordcloud import WordCloud
|
3 |
+
import nltk
|
4 |
+
from collections import Counter
|
5 |
+
from googletrans import Translator
|
6 |
+
from nltk.corpus import stopwords
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# Download necessary NLTK data
|
10 |
+
nltk.download('punkt')
|
11 |
+
nltk.download('averaged_perceptron_tagger')
|
12 |
+
nltk.download('stopwords')
|
13 |
+
|
14 |
+
translator = Translator()
|
15 |
+
stop_words = set(stopwords.words('english'))
|
16 |
+
|
17 |
+
# Define example sentences and synonyms for the word list
|
18 |
+
word_data_examples = {
|
19 |
+
"village": ("The village was quiet at night.", "hamlet, community"),
|
20 |
+
"adventure": ("They went on an exciting adventure in the forest.", "expedition, quest"),
|
21 |
+
"map": ("We used a map to find the hidden treasure.", "chart, atlas"),
|
22 |
+
"cave": ("They explored a dark cave in the mountains.", "cavern, grotto"),
|
23 |
+
"among": ("She found her book among the pile of papers.", "amidst, between"),
|
24 |
+
"mountains": ("The mountains were covered with snow in winter.", "peaks, ranges"),
|
25 |
+
"children": ("The children played games in the park.", "kids, youngsters"),
|
26 |
+
"known": ("He was known for his kindness and bravery.", "recognized, famous"),
|
27 |
+
"hidden": ("They found a hidden door behind the bookshelf.", "concealed, secret"),
|
28 |
+
"local": ("The local market was full of fresh produce.", "regional, native"),
|
29 |
+
"discovery": ("The discovery of the old map excited everyone.", "finding, revelation"),
|
30 |
+
"eagle": ("An eagle soared high above the valley.", "raptor, bird of prey"),
|
31 |
+
"villagers": ("The villagers gathered in the square for the festival.", "residents, townsfolk"),
|
32 |
+
"legend": ("The legend of the lost city intrigued the adventurers.", "myth, lore"),
|
33 |
+
"tales": ("Grandma told us tales of her childhood.", "stories, narratives"),
|
34 |
+
"daring": ("His daring escape from the cave was legendary.", "bold, audacious"),
|
35 |
+
"spirit": ("The spirit of adventure was alive in their hearts.", "soul, essence"),
|
36 |
+
"exploring": ("They spent the summer exploring the forest.", "investigating, discovering"),
|
37 |
+
"old": ("The old castle was full of secrets.", "ancient, aged"),
|
38 |
+
"lost": ("He felt lost without his best friend.", "missing, misplaced"),
|
39 |
+
"ancient": ("They discovered ancient artifacts in the desert.", "archaic, antique"),
|
40 |
+
"inside": ("Inside the box was a beautiful necklace.", "within, interior"),
|
41 |
+
"treasure": ("They dreamed of finding hidden treasure.", "riches, valuables"),
|
42 |
+
"whispering": ("The trees were whispering secrets in the wind.", "murmuring, softly speaking"),
|
43 |
+
"hollow": ("They found a hollow tree to hide in during the storm.", "cavity, void"),
|
44 |
+
"decided": ("She decided to take the long way home.", "determined, resolved"),
|
45 |
+
"journey": ("Their journey took them across the country.", "trip, voyage"),
|
46 |
+
"together": ("They worked together to solve the mystery.", "jointly, collectively"),
|
47 |
+
"way": ("She found a new way to solve the puzzle.", "method, manner"),
|
48 |
+
"reached": ("They finally reached the top of the hill.", "arrived, attained"),
|
49 |
+
"chest": ("The chest was filled with gold coins.", "trunk, box"),
|
50 |
+
"boulder": ("A large boulder blocked the path.", "rock, stone"),
|
51 |
+
"artifacts": ("The museum displayed artifacts from ancient Egypt.", "relics, antiquities"),
|
52 |
+
"legends": ("The legends spoke of a hidden kingdom.", "myths, sagas"),
|
53 |
+
"explore": ("They wanted to explore the old mansion.", "investigate, examine"),
|
54 |
+
"secret": ("She kept the secret hidden from everyone.", "confidential, hidden"),
|
55 |
+
"small": ("The small kitten was very playful.", "tiny, little"),
|
56 |
+
"mountain": ("The mountain was covered in thick forests.", "peak, hill"),
|
57 |
+
"part": ("Each part of the puzzle was important.", "piece, segment"),
|
58 |
+
"everyday": ("He wore his everyday clothes to the party.", "daily, routine"),
|
59 |
+
"life": ("Life in the village was peaceful.", "existence, being"),
|
60 |
+
"nestled": ("The cabin was nestled in the woods.", "tucked, situated"),
|
61 |
+
"towering": ("The towering trees made the forest dark and cool.", "lofty, soaring"),
|
62 |
+
"peaks": ("The mountain peaks were covered in snow.", "summits, crests"),
|
63 |
+
"said": ("He said he would be back soon.", "stated, remarked"),
|
64 |
+
"protected": ("The ancient ruins were protected by law.", "guarded, sheltered"),
|
65 |
+
"massive": ("The massive ship docked at the port.", "enormous, huge"),
|
66 |
+
"supposedly": ("The treasure was supposedly buried under the tree.", "allegedly, reportedly"),
|
67 |
+
"watched": ("They watched the movie together.", "observed, viewed"),
|
68 |
+
"perch": ("The bird found a perch on the windowsill.", "roost, rest")
|
69 |
+
}
|
70 |
+
|
71 |
+
# Words to be excluded from both the word cloud and the word list
|
72 |
+
exclude_words = set([
|
73 |
+
'i', 'you', 'he', 'she', 'it', 'we', 'they', 'me', 'him', 'her', 'us', 'them',
|
74 |
+
'my', 'your', 'his', 'its', 'our', 'their', 'mine', 'yours', 'hers', 'ours', 'theirs',
|
75 |
+
'alex', 'mia', 'sam', 'echo', 'ridge', 'guardian', 'of', 'the', 'glen'
|
76 |
+
])
|
77 |
+
|
78 |
+
def process_text(text):
|
79 |
+
words = nltk.word_tokenize(text)
|
80 |
+
words = [word.lower() for word in words if word.isalnum() and word.lower() not in stop_words and word.lower() not in exclude_words]
|
81 |
+
word_freq = Counter(words)
|
82 |
+
pos_tags = nltk.pos_tag(words)
|
83 |
+
return word_freq, pos_tags
|
84 |
+
|
85 |
+
def generate_wordcloud(word_freq):
|
86 |
+
wordcloud = WordCloud(width=800, height=400, background_color='white').generate_from_frequencies(word_freq)
|
87 |
+
plt.figure(figsize=(10, 5))
|
88 |
+
plt.imshow(wordcloud, interpolation='bilinear')
|
89 |
+
plt.axis('off')
|
90 |
+
plt.savefig('wordcloud.png')
|
91 |
+
return 'wordcloud.png'
|
92 |
+
|
93 |
+
def translate_and_get_pos(word_freq, pos_tags):
|
94 |
+
pos_map = {
|
95 |
+
'NN': 'n.', 'NNS': 'n.', 'VB': 'v.', 'VBD': 'v.', 'VBG': 'v.', 'VBN': 'v.',
|
96 |
+
'VBP': 'v.', 'VBZ': 'v.', 'JJ': 'adj.', 'JJR': 'adj.', 'JJS': 'adj.', 'RB': 'adv.',
|
97 |
+
'RBR': 'adv.', 'RBS': 'adv.'
|
98 |
+
}
|
99 |
+
|
100 |
+
word_data = []
|
101 |
+
for word, freq in word_freq.items():
|
102 |
+
pos = [pos_tag[1] for pos_tag in pos_tags if pos_tag[0] == word]
|
103 |
+
if pos and (pos[0] in ['NNP', 'NNPS'] or word in exclude_words):
|
104 |
+
continue # Skip proper nouns, pronouns, and specific excluded words
|
105 |
+
translation = translator.translate(word, dest='ko').text
|
106 |
+
pos = pos_map.get(pos[0], 'N/A') if pos else 'N/A'
|
107 |
+
example_sentence, synonyms = word_data_examples.get(word, (f"ex) The word '{word}' in a sentence.", ""))
|
108 |
+
word_data.append((word, freq, translation, pos, example_sentence, synonyms))
|
109 |
+
word_data.sort(key=lambda x: x[1], reverse=True)
|
110 |
+
return word_data[:50]
|
111 |
+
|
112 |
+
def main(text):
|
113 |
+
word_freq, pos_tags = process_text(text)
|
114 |
+
wordcloud_image = generate_wordcloud(word_freq)
|
115 |
+
word_data = translate_and_get_pos(word_freq, pos_tags)
|
116 |
+
|
117 |
+
word_data_str = "\n".join([f"{i+1}. {word}: {pos} {translation}, ex) {example_sentence} 동의어: {synonyms}." for i, (word, freq, translation, pos, example_sentence, synonyms) in enumerate(word_data)])
|
118 |
+
return wordcloud_image, word_data_str
|
119 |
+
|
120 |
+
# Gradio interface
|
121 |
+
interface = gr.Interface(
|
122 |
+
fn=main,
|
123 |
+
inputs="text",
|
124 |
+
outputs=["image", "text"],
|
125 |
+
title="Language Learning App",
|
126 |
+
description="Input text to generate a word cloud and a frequency list with Korean meanings, parts of speech, and example sentences."
|
127 |
+
)
|
128 |
+
|
129 |
+
# Launch the interface
|
130 |
+
interface.launch()
|