-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
66 lines (53 loc) · 1.79 KB
/
utils.py
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
import pandas as pd
import numpy as np
import streamlit as st
from nltk import word_tokenize
from nltk.util import ngrams
from sklearn.feature_extraction.text import CountVectorizer
import string
import re
import nltk
from nltk.corpus import stopwords
from wordcloud import WordCloud
import matplotlib.pyplot as plt
nltk.download("stopwords")
def get_top_ngram(corpus, n=None):
vec = CountVectorizer(ngram_range=(n, n)).fit(corpus)
bag_of_words = vec.transform(corpus)
sum_words = bag_of_words.sum(axis=0)
words_freq = [(word, sum_words[0, idx])
for word, idx in vec.vocabulary_.items()]
words_freq =sorted(words_freq, key = lambda x: x[1], reverse=True)
return words_freq[:10]
def remove_linebreaks(s):
return s.replace("\n", " ")
def tokenize(s):
return word_tokenize(s, language="english")
def remove_stopwords(s):
stpwrds = set(stopwords.words("english"))
return [w for w in s if not w in stpwrds]
def stem(s):
return " ".join([stemmer.stem(w.lower()) for w in s])
def vectorize(s):
return vectorizer.fit_transform(s)
def clean(s):
s = re.sub(r'\d+', '', s) # remove numbers
s = "".join([char.lower() for char in s if char not in string.punctuation]) # remove punctuations and convert characters to lower case
s = re.sub('\s+', ' ', s).strip() # substitute multiple whitespace with single whitespace
s = remove_linebreaks(s)
s = tokenize(s)
s = remove_stopwords(s)
# s = stem(s)
return s
def show_wordcloud(data, maxwords):
cloud = WordCloud(
background_color='white',
max_words=maxwords,
max_font_size=30,
scale=3,
random_state=1)
output=cloud.generate(str(data))
fig = plt.figure(1, figsize=(12, 12))
plt.axis('off')
plt.imshow(output)
plt.show()