def get_word_cloud(url, trip = None, write = False):
t = requests.get(url)
j1 = json.loads(t.text)
morph = pymorphy2.MorphAnalyzer()
trip = trip
if trip is None:
posts = [bs4.BeautifulSoup(post['comment'], 'lxml').text for post in j1['threads'][0]['posts']]
elif trip == 'op':
posts = [bs4.BeautifulSoup(post['comment'], 'lxml').text for post in j1['threads'][0]['posts'] if post['op'] == 1]
else:
posts = [bs4.BeautifulSoup(post['comment'], 'lxml').text for post in j1['threads'][0]['posts'] if post['trip'] == trip]
bad_words = ['весь', 'мочь', 'быть', 'раз', 'тред', 'youtube', 'https', 'watch', 'человек', 'день', 'дело', 'год', 'время', \
'неделя', 'пост', 'пара', 'часть']
mas = [re.split('[^A-Za-zА-Яа-я]', post) for post in posts]
words = [item for sublist in mas for item in sublist]
norm_words = [morph.parse(word)[0] for word in words if len(word) > 2]
good_words = [word.normal_form for word in norm_words if word.tag.POS in ['NOUN', None]\
and word.normal_form not in bad_words]
text = (" ".join(good_words)).replace('деньга', 'деньги').replace('нихуй', 'нихуя').replace('песнь', 'песня').replace('лола', 'лол')
wc = wordcloud.WordCloud(min_font_size = 15, width = 2000, height = 800, mode = 'RGB', max_words = 500, \
background_color = 'white', collocations = False, colormap = 'viridis').generate(text)
plt.figure(figsize=(25,40))
plt.imshow(wc, interpolation='bilinear')
#plt.imshow(wc)
plt.axis("off")
if write:
wc.to_file("pic.png")
return plt.gcf()