-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordVisualizer.py
More file actions
261 lines (204 loc) · 7.41 KB
/
wordVisualizer.py
File metadata and controls
261 lines (204 loc) · 7.41 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
#Visualization code from
#https://www.kaggle.com/nicapotato/explore-the-spooky-n-grams-wordcloud-bayes
# Packages
import os
import numpy as np
import pandas as pd
import nltk
import random
import string as str
# Pre-Processing
import string
from nltk.tokenize import RegexpTokenizer
from nltk.corpus import stopwords
import re
from nltk.stem import PorterStemmer
from nltk.stem.lancaster import LancasterStemmer
from nltk.stem.porter import *
# Sentiment Analysis
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from nltk.sentiment.util import *
import matplotlib.pyplot as plt
# Visualization
import matplotlib as mpl
import matplotlib.pyplot as plt
#matplotlib inline
from subprocess import check_output
from wordcloud import WordCloud, STOPWORDS
import seaborn as sns
# N- Grams
from nltk.util import ngrams
from collections import Counter
# Topic Modeling
from nltk import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.decomposition import NMF, LatentDirichletAllocation
# Word 2 Vec
from gensim.models import Word2Vec
from sklearn.decomposition import PCA
# Models
import datetime
from nltk import naivebayes
import warnings
warnings.filterwarnings("ignore")
tokenizer = RegexpTokenizer(r'\w+')
#stop_words = set(stopwords.words('englishTangram'))
def preprocessing(data):
txt = data.str.lower().str.cat(sep=' ') #1
words = tokenizer.tokenize(txt) #2
words = [w for w in words if not w in stop_words] #3
#words = [w for w in words] # 3, don't get rid of STOPWORDS
#words = [ps.stem(w) for w in words] #4
return words
def wordfreqviz(text, x, title):
word_dist = nltk.FreqDist(text)
top_N = x
rslt = pd.DataFrame(word_dist.most_common(top_N),
columns=['Word', 'Frequency']).set_index('Word')
mpl.style.use('ggplot')
rslt.plot.bar(rot=0)
plt.savefig("./plots/" + title + "_WORDHIST.png")
def wordfreq(text, x):
word_dist = nltk.FreqDist(text)
top_N = x
rslt = pd.DataFrame(word_dist.most_common(top_N),
columns=['Word', 'Frequency']).set_index('Word')
print(rslt)
# Function
def cloud(text, title):
# Setting figure parameters
mpl.rcParams['figure.figsize'] = (10.0, 10.0) # (6.0,4.0)
# mpl.rcParams['font.size']=12 #10
mpl.rcParams['savefig.dpi'] = 100 # 72
mpl.rcParams['figure.subplot.bottom'] = .1
# Processing Text
stopwords = set(STOPWORDS) # Redundant
wordcloud = WordCloud(width=1400, height=800,
background_color='black',
collocations = False,
stopwords=stopwords,
).generate(" ".join(text))
# Output Visualization
plt.figure(figsize=(20, 10), facecolor='k')
plt.imshow(wordcloud)
plt.axis('off')
plt.tight_layout(pad=0)
plt.title(title, fontsize=50, color='y')
plt.savefig("./plots/" + title + "_WC.png")
#plt.imshow(plt.recolor( colormap= 'Pastel1_r' , random_state=17), alpha=0.98)
#fig.savefig("wordcloud.png", dpi=900)
## Helper Functions
def get_ngrams(text, n):
n_grams = ngrams((text), n)
return [ ' '.join(grams) for grams in n_grams]
def gramfreq(text,n,num):
# Extracting bigrams
result = get_ngrams(text,n)
# Counting bigrams
result_count = Counter(result)
# Converting to the result to a data frame
df = pd.DataFrame.from_dict(result_count, orient='index')
df = df.rename(columns={'index':'words', 0:'frequency'}) # Renaming index column name
return df.sort_values(["frequency"],ascending=[0])[:num]
def gram_table(sentences, gram, length):
out = pd.DataFrame(index=None)
for i in gram:
table = pd.DataFrame(gramfreq(preprocessing(sentences),i,length).reset_index())
table.columns = ["{}-Gram".format(i),"Occurence"]
out = pd.concat([out, table], axis=1)
return out
lemm = WordNetLemmatizer()
class LemmaCountVectorizer(CountVectorizer):
def build_analyzer(self):
analyzer = super(LemmaCountVectorizer, self).build_analyzer()
return lambda doc: (lemm.lemmatize(w) for w in analyzer(doc))
# Define helper function to print top words
def print_top_words(model, feature_names, n_top_words):
for index, topic in enumerate(model.components_):
message = "\nTopic #{}:".format(index)
message += " ".join([feature_names[i] for i in topic.argsort()[:-n_top_words - 1:-1]])
print(message)
print("=" * 70)
def LDA(data):
# Storing the entire training text in a list
text = list(data.values)
# Calling our overwritten Count vectorizer
tf_vectorizer = LemmaCountVectorizer(max_df=0.95, min_df=2,
stop_words='english',
decode_error='ignore')
tf = tf_vectorizer.fit_transform(text)
lda = LatentDirichletAllocation(n_topics=6, max_iter=5,
learning_method='online',
learning_offset=50.,
random_state=0)
lda.fit(tf)
n_top_words = 10
print("\nTopics in LDA model: ")
tf_feature_names = tf_vectorizer.get_feature_names()
print_top_words(lda, tf_feature_names, n_top_words)
#Dat visualization
baseDir = "./data/"
topWords = 20 #Top words to be visulatized
for subdir, dirs, files in os.walk(baseDir):
for file in files:
fn = subdir + "/" + file
if ".DS_Store" in fn:
continue
print(fn)
ss = subdir.split('/')
ss1 = file.split('.')
#title = ss[2] + '_' + ss[3] + '_' + ss1[0]
#print(title)
#inData = pd.read_csv(fn)
dataDF= pd.read_csv(fn)
#x = dataDF['UTTERANCE']
#y = dataDF.dataDF['LABEL'==0]
sentencesPos = dataDF.loc[dataDF['LABEL']==1]
SP = sentencesPos['UTTERANCE']
title = ss[2] + '_' + ss[3] + '_' + ss1[0] + '_POS'
words = preprocessing(SP)
wordfreqviz(words, topWords, title)
cloud(words, title)
ngramTable = gram_table(SP, gram=[1, 2, 3], length=20)
ngramTable.to_csv("./plots/" + title + "_NGRAM.csv")
sentencesNeg = dataDF.loc[dataDF['LABEL']==0]
SN = sentencesNeg['UTTERANCE']
title = ss[2] + '_' + ss[3] + '_' + ss1[0] + '_NEG'
words = preprocessing(SN)
wordfreqviz(words, topWords, title)
cloud(words, title)
ngramTable = gram_table(SN, gram=[1, 2, 3], length=20)
ngramTable.to_csv("./plots/" + title + "_NGRAM.csv")
#
# # Read Data
# neg = pd.read_csv(baseDir + dataFolder + "/" + subDataFolder + "/0.txt")
# pos = pd.read_csv(baseDir + dataFolder + "/" + subDataFolder + "/1.txt")
#
#
# negSent = neg.iloc[:,0] #Get sentence data
#
# #Process and visualize all the NEGATIVE WORDS
# words = preprocessing(negSent)
# wordfreqviz(words, topWords)
# wordfreq(words, topWords)
# cloud(words, 'test')
# a = gramfreq(words,2,25)
# print(a)
# b = gram_table(negSent, gram=[1,2,3,4], length=20)
# print(b)
#
# LDA(negSent)
#
#
# #Visualize and process all the positive words
# posSent = pos.iloc[:,0]
#ps = PorterStemmer()
#import sys
#from os import listdir
#from os.path import isfile, join
#mypath = './data/TB_NONTB/ALL'
#onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
#print(onlyfiles)
#inputFile = "./data/"
#df = pd.read_csv("../input/train.csv", index_col="id")
#test = pd.read_csv("../input/test.csv", index_col="id")