-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang_proc_2.py
188 lines (146 loc) · 5.61 KB
/
lang_proc_2.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
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
import pandas as pd
import pickle
import wordcloud
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import json
import os
import nltk
import re
import string
from sklearn.feature_extraction import text
from sklearn.feature_extraction.text import CountVectorizer
global_path = 'C:/Users/17742/Desktop/win_art_writing/nytimes_articles/articles_dump'
path = 'artnet_articles'
#path = 'artnet_articles'
#path = 'artnet_articles'
#folderpath = os.path.expanduser(os.path.join(global_path, path))
filelist = os.listdir(global_path)
data = {}
data_df = pd.DataFrame(data)
cleaned_titles = []
cleaned_paras = []
for file in filelist[:5000]:
filepath = os.path.join(global_path, file)
f = open(filepath) # , encoding='ascii', errors='ignore')
try:
j_import = json.load(f)
j_import = j_import[0]
# print(j_import)
# title = j_import['title']
try:
para = j_import['para']
para = para[0]
para = para.replace(" ", "").replace("Follow on Facebook:", '').replace("\n", ' ')
para = para.replace("\r", " ")
para = para.strip()
# print(para)
title = j_import['title']
title = "@".join(title)
title = title.replace(" ", "").replace("\n", ' ')
title = title.replace("\r", " ")
title = title.strip()
# text = para
# cleaned_paras.append(text)
# print(para)
new_row = {"title": title, "para": para}
data_df = data_df.append(new_row, ignore_index=True)
f.close()
except KeyError:
f.close()
pass
except json.decoder.JSONDecodeError:
f.close()
pass
data_df = data_df.set_index('title')
def combine_text(list_of_text):
'''Takes a list of text and combines them into one large chunk of text.'''
combined_text = ' '.join(list_of_text)
return combined_text
def clean_text_round1(text):
'''Make text lowercase, remove text in square brackets, remove punctuation and remove words containing numbers.'''
text = text.lower()
text = text.replace('-', ' ')
text = text.replace("’s ", " ").replace("' ", " ")
text = text.replace("s’ ", " ").replace("s' ", " ")#remove proper possesives
text = re.sub('\[.*?\]', '', text)
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
text = re.sub('\w*\d\w*', '', text)
return text
round1 = lambda x: clean_text_round1(x)
def clean_text_round2(text):
'''Get rid of some additional punctuation and non-sensical text that was missed the first time around.'''
text = re.sub('[‘’“”…]', '', text)
text = re.sub('\n', '', text)
text = re.sub('new york', 'newyork', text)
text = re.sub('new year', 'newyear', text)
text = re.sub('the new museum', 'newmuseum', text)
#text = text.replace(r'[^\u0020-\u007E]', '')
return text
round2 = lambda x: clean_text_round2(x)
data_clean = pd.DataFrame(data_df.para.apply(round1))
pd.set_option('display.max_colwidth',500)
data_clean = pd.DataFrame(data_clean.para.apply(round2))
cv = CountVectorizer(stop_words='english')
data_cv = cv.fit_transform(data_clean.para)
data_dtm = pd.DataFrame(data_cv.toarray(), columns=cv.get_feature_names())
data_dtm.index = data_clean.index
data_dtm = data_dtm.transpose()
print(data_dtm.head())
#data_dtm.to_pickle(f"{path}doc_term_matrix.pkl")
# Let's also pickle the cleaned data (before we put it in document-term matrix format) and the CountVectorizer object
#data_clean.to_pickle(f"{path}data_clean.pkl")
#pickle.dump(cv, open(f"{path}cv.pkl", "wb"))
# Find the top 30 words in each article
top_dict = {}
for ind, column in enumerate(data_dtm.columns):
col = data_dtm.iloc[:,[ind]]
#print(col)
top = col[column].sort_values(ascending=False)
top = top.head(30)
top.set_index = data_dtm.iloc[0, ind]
top_dict[column]= list(zip(top.index, top.values))
#print(top_dict)
# Print the top 15 words in each article
for title, top_words in top_dict.items():
print(title)
print(', '.join([word for word, count in top_words[0:14]]))
print('---')
# Look at the most common top words --> add them to the stop word list
from collections import Counter
words = []
for article in data_dtm.columns:
top = [word for (word, count) in top_dict[article]]
for t in top:
words.append(t)
#print(words)
#Counter(words).most_common()
# If more than half of the comedians have it as a top word, exclude it from the list
cutoff = data_dtm.shape[1] * (1/3)
add_stop_words = [word for word, count in Counter(words).most_common() if count > cutoff]
print(add_stop_words)
# update document-term matrix with the new list of stop words
# Read in cleaned data
#data_clean = pd.read_pickle('data_clean.pkl')
# Add new stop words
stop_words = text.ENGLISH_STOP_WORDS.union(add_stop_words)
# Recreate document-term matrix
cv = CountVectorizer(stop_words=stop_words)
data_cv = cv.fit_transform(data_clean.para)
data_stop = pd.DataFrame(data_cv.toarray(), columns=cv.get_feature_names())
data_stop.index = data_clean.index
# Pickle it for later use
pickle.dump(cv, open(f"{path}cv_stop.pkl", "wb"))
data_stop.to_pickle(f"{path}dtm_stop.pkl")
prep = " ".join(data_clean['para'])
wc = WordCloud(stopwords=stop_words, background_color="white", colormap="Dark3",
max_font_size=200, random_state=42)
plt.rcParams['figure.figsize'] = [300, 150]
# Create subplots for each comedian
#for index, comedian in enumerate(data.columns):
wc.generate(prep)
#plt.subplot(3, 4, index+1)
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
#plt.title(full_names[index])
plt.show()