/
preprocess.py
executable file
·268 lines (175 loc) · 6.67 KB
/
preprocess.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 30 21:54:54 2020
@author: anooppanyam
"""
from __future__ import division
from os import path
from collections import Counter
import re
import string
try:
import cPickle as pickle
except:
import pickle
import numpy as np
import pandas as pd
from nltk.tokenize import RegexpTokenizer
from nltk.corpus import stopwords
from verbalexpressions import VerEx
__file__ = 'preprocess.py'
PROJECT_DIR = (path.dirname(path.abspath(__file__)))
STOPWORDS = stopwords.words('english')
class TextProcessor(object):
"""
Process raw yelp review data
Added features:
- (avg) review sentiment
- (avg) word count
- (avg) punctuation count
@Params:
- df: DataFrame containing a reviews column
"""
def __init__(self, df):
self.df = df
def pos_neg_words(self, value, file):
words = {}
with open(file, encoding="ISO-8859-1") as f:
for line in f:
if (len(line) == 0) or (line == '\n') or (line[0] == ';'):
continue
words[line.replace('\n', '')] = value
return words
def sentiment_words(self, filename):
"""
Parameters
----------
filename : str
file path for sentiment scores. Represented with a pos, neg, or both score. .
Returns
-------
Dictionary of sentiment scores for words.
"""
df = pd.read_table(filename, skiprows=26)
df['score'] = df['PosScore'] - df['NegScore']
df = df[['SynsetTerms', 'score']]
df.columns = ['words', 'score']
# remove neutral words
mask = df['score'] != 0
df = df[mask]
# Regex to find number
rx1 = re.compile('#([0-9])')
# Regex to find words
verEx = VerEx()
exp = verEx.range('a', 'z', 'A', 'Z')
rx2 = re.compile(exp.source())
sent_dict = {}
for i, row in df.iterrows():
w = row['words']
s = row['score']
nums = re.findall(rx1, w)
w = w.split(' ')
words = []
if len(w) == 1:
words = ''.join(re.findall(rx2, str(w)))
else:
words = [''.join(re.findall(rx2, str(string))) for string in w]
for nn, ww in zip(nums, words):
# only sentiment for the most common meaning of the word
if nn == '1':
sent_dict[ww] = s
return sent_dict
def create_sent_dicts(self):
"""
Returns
-------
None
Create sentiment dictionaries from three sources :
- https://www.quora.com/Is-there-a-downloadable-database-of-positive-and-negative-words
- http://sentiwordnet.isti.cnr.it/
- https://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html
"""
# Positive words
self.pos_neg_dict = self.pos_neg_words(1, PROJECT_DIR + '/data/positive-words.txt')
#Combine with negative words
self.pos_neg_dict.update(self.pos_neg_words(-1, PROJECT_DIR + '/data/negative-words.txt'))
self.sent_dict = self.sentiment_words(PROJECT_DIR + '/data/SentiWordNet.txt')
def update_sentiment_score(self, val, sent_dict):
"""
Parameters
----------
val : float
sentiment score
sent_dict : dict
{'score': #, 'pos_cnt': #, 'neg_cnt': #}
Returns
-------
Dict with values updated on sign(val)
"""
sent_dict['score'] += val
if val > 0:
sent_dict['pos_cnt']+=1
elif val < 0:
sent_dict['neg_cnt'] += 1
return sent_dict
def text_features(self, row):
text = row['text'].lower()
reg = re.compile('[%s]' % re.escape(string.punctuation))
punc_ct = Counter(re.findall(reg, text))
row['e'] = punc_ct['!']
row['q'] = punc_ct['?']
row['punc'] = np.sum(punc_ct.values())
row['chars'] = len(re.sub('\s+', '', text))
# Strip punctuation
text = re.sub("'", '', text)
text = re.sub(reg, ' ', text).strip()
words = re.split(r'\s+', text)
row['words'] = len(words)
# Add sentiment scores
sent_dict_1 = {'score':0.0, 'pos_cnt':0.0, 'neg_cnt':0.0}
sent_dict_2 = sent_dict_1.copy()
for w in words:
val_1 = self.sent_dict.get(w, 0)
val_2 = self.pos_neg_dict.get(w, 0)
sent_dict_1 = self.update_sentiment_score(val_1, sent_dict_1)
sent_dict_2 = self.update_sentiment_score(val_2, sent_dict_2)
# Add sentiment features
row['sent_1_score'] = sent_dict_1['score']
row['sent_1_rate'] = sent_dict_1['score']/len(words)
row['sent_1_pct'] = sent_dict_1['pos_cnt']
row['sent_1_nct'] = sent_dict_1['neg_cnt']
row['sent_2_score'] = sent_dict_2['score']
row['sent_2_rate'] = sent_dict_2['score']/len(words)
row['sent_2_pct'] = sent_dict_2['pos_cnt']
row['sent_2_nct'] = sent_dict_2['neg_cnt']
return row
def update_feats(self):
"""
MAIN -> update df features
Returns
-------
None.
"""
self.create_sent_dicts()
self.df = self.df.apply(self.text_features, axis=1)
def save_df(self, path=PROJECT_DIR + '/data/saved_df.pkl'):
"""Save the DataFrame to disk as pickle object."""
self.df.to_pickle(path)
def load_df(self, path=PROJECT_DIR + '/data/saved_df.pkl'):
"""Return a saved DataFrame."""
with open(path, 'r') as f:
return pickle.load(f)
def tokenize(text):
"""
Parameters
----------
text : str
Returns
-------
Return tokenized words with stopwords removed
"""
tokenizer = RegexpTokenizer(r'\w+')
text = text.lower().replace("'", '')
tokens = tokenizer.tokenize(text)
return np.array(tokens), np.array([word for word in tokens if not word in STOPWORDS])