-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
273 lines (235 loc) · 7.49 KB
/
Copy pathapp.py
File metadata and controls
273 lines (235 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import streamlit as st
import nltk
import numpy as np
import pymorphy2
import requests
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from collections import Counter
from natasha import MorphVocab, Doc, Segmenter
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from googlesearch import search
from bs4 import BeautifulSoup
from PIL import Image
import io
morph = pymorphy2.MorphAnalyzer()
morph_vocab = MorphVocab()
segmenter = Segmenter()
nltk.download("punkt", quiet=True)
nltk.download("stopwords", quiet=True)
nltk.download("punkt_tab", quiet=True)
nltk.download("averaged_perceptron_tagger", quiet=True)
stop_words = set(stopwords.words("russian"))
not_about_tourism = [
"личный",
"кабинет",
"тур",
"согласие",
"вход",
"офис",
"агентство",
"компания",
"ул",
"поиск",
"набор",
"реестр",
"вариант",
"зима",
"день",
"январь",
"декабрь",
"дата",
"год",
"средний",
"зимний",
"выше",
"высокий",
"область",
"край",
"регион",
"проживание",
"новый",
"каникулы",
"вид",
"сложность",
"лето",
"весна",
"осень",
"человек",
"весь",
"отдых",
"нужно",
"россия",
"среднее",
"тот",
"который",
"рубль",
"руб",
"ввод",
"неверный",
"нагрузка",
"февраль",
"март",
"ввод",
"серия",
"дед",
"ноябрь",
"апрель",
"этот",
"данный",
"статья",
"страница",
"другой",
"свой",
"русский",
"дек",
"апр",
"км",
"ной",
"наш",
]
def get_search_results(query, num_results=5):
try:
results = search(query, num_results=num_results, lang="ru")
return list(results)
except Exception as e:
st.write(f"Ошибка при поиске: {e}")
return []
def extract_text_from_url(url):
try:
response = requests.get(url)
response.encoding = response.apparent_encoding
soup = BeautifulSoup(response.text, "html.parser")
body = soup.find("body")
if body:
for script in body(["script", "style"]):
script.decompose()
text = body.get_text(separator=" ")
return text
else:
return ""
except Exception as e:
st.write(f"Ошибка при извлечении текста из {url}: {e}")
return ""
def get_pos(word):
parsed = morph.parse(word)
if parsed:
return parsed[0].tag.POS
return None
def is_noun_or_adjective(word):
pos = get_pos(word)
return pos in ("NOUN", "ADJF", "ADJS")
def process_query(query, num_results=5):
urls = get_search_results(query, num_results=num_results)
all_text = []
for url in urls:
text = extract_text_from_url(url).strip()
all_text.append(text)
full_text = " ".join(all_text)
tokens = word_tokenize(full_text)
filtered_tokens = []
normal_forms_cache = {}
for word in tokens:
lower_word = word.lower()
if word.isalpha() and lower_word not in stop_words:
if is_noun_or_adjective(lower_word):
if lower_word not in normal_forms_cache:
parsed_word = morph.parse(lower_word)[0]
normal_forms_cache[lower_word] = parsed_word.normal_form
infinitive = normal_forms_cache[lower_word]
if infinitive not in not_about_tourism:
filtered_tokens.append(infinitive.capitalize())
word_freq = Counter(filtered_tokens)
return word_freq.most_common(50)
st.title("Визуализация облака слов для запросов")
query = st.text_input(
"Введите ваш запрос для парсинга",
value="поездки на новогодних каникулах по России в 2025",
)
num_results = st.number_input(
"Количество результатов для парсинга (больше число - точнее подобраны слова, меньше число - быстрее генерация",
min_value=1,
max_value=20,
value=5,
)
st.write("Настройки облака слов:")
bg_color = st.color_picker("Выберите цвет фона облака", "#FFFFFF")
custom_colors = [
"#A54040",
"#B96E6E",
"#CD9C9C",
"#98C665",
"#B0D28A",
"#C7DDAD",
"#A57865",
"#BA988A",
]
palettes = {
"Пастельная": ["#F1B2B2", "#F1D7B2", "#F1F1B2", "#B2F1B2", "#B2F1D7", "#E0B0FF"],
"Тёмная": ["#8B0000", "#006400", "#B22222", "#483D8B", "#D2691E"],
"Яркая": ["#FF0000", "#DAA520", "#00FF00", "#0000FF", "#9400D3", "#00FFFF"],
}
use_single_color = st.checkbox("Использовать один цвет для всех слов")
if use_single_color:
word_color = st.color_picker("Выберите цвет слов", "#000000")
def color_func_single(*args, **kwargs):
return word_color
color_func = color_func_single
else:
palette_choice = st.selectbox(
"Выберите палитру цветов",
["Пастельная", "Тёмная", "Яркая", "Made by shpingalety"],
)
if palette_choice == "Made by shpingalety":
def color_func_random(*args, **kwargs):
return np.random.choice(custom_colors)
color_func = color_func_random
else:
selected_palette = palettes[palette_choice]
def color_func_palette(*args, **kwargs):
return np.random.choice(selected_palette)
color_func = color_func_palette
font_choice = st.selectbox("Выберите шрифт", ["Roboto", "Ubuntu", "Montserrat"])
font_paths = {
"Roboto": "fonts/Roboto-Regular.ttf",
"Ubuntu": "fonts/Ubuntu-Regular.ttf",
"Montserrat": "fonts/Montserrat-Medium.ttf",
}
font_path = font_paths[font_choice]
mask_option = st.selectbox(
"Выберите форму облака", ("Прямоугольник", "Звезда", "Птица счастья")
)
mask = None
if mask_option != "Прямоугольник":
mask_file = {"Звезда": "masks/star.png", "Птица счастья": "masks/bird.png"}[
mask_option
]
try:
mask_image = Image.open(mask_file).convert("RGB")
mask = np.array(mask_image)
except:
st.write("Не удалось загрузить маску. Проверьте путь и наличие файла.")
mask = None
if st.button("Сгенерировать облако слов"):
most_common_words = process_query(query, num_results=num_results)
word_freq_dict = dict(most_common_words)
wc = WordCloud(
width=800,
height=400,
background_color=bg_color,
color_func=color_func,
font_path=font_path if font_path else None,
mask=mask,
)
wc.generate_from_frequencies(word_freq_dict)
fig, ax = plt.subplots(figsize=(10, 5))
ax.imshow(wc, interpolation="bilinear")
ax.axis("off")
st.pyplot(fig)
buf = io.BytesIO()
wc.to_image().save(buf, format="PNG")
buf.seek(0)
st.download_button(
"Скачать облако слов", data=buf, file_name="wordcloud.png", mime="image/png"
)