-
Notifications
You must be signed in to change notification settings - Fork 5
/
data_classes.py
318 lines (288 loc) · 12 KB
/
data_classes.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import pandas as pd
import re
import nltk
from gensim.summarization.textcleaner import split_sentences
from gensim.models import Phrases
from gensim.models.phrases import Phraser
from textblob import TextBlob
#Define real words according to nltk corpus english vocabulary.
#This function is used for the remove_gibberish() method in the Review class
vocab_en = set(w.lower() for w in nltk.corpus.words.words())
#DEFINE CLEAN TEXT FUNCTION
def clean_text(text_block):
"""
This function cleans a block of text.
INPUT:
text_block = The block of text to clean.
OUTPUT:
A block of text stripped of punctuation and made lowercase.
"""
#This represents a non-breaking space in the text block that needs to be removed.
text_block = text_block.replace(u'\xa0', u' ')
#Replace & with and.
text_block = text_block.replace('&',' and ')
#Some reviews forget to have a space after a period. I want to force a space.
text_block = text_block.replace('.',' ')
#Replace underscores with spaces.
text_block = text_block.replace('_',' ')
#Remove all other punctuation and numbers (except apostrophe).
text_block = re.sub(r'[^\w\s\']|\d',' ',text_block.lower())
#Remove multiple spaces and make a single space. Also remove leading/trailing spaces.
text_block = re.sub(' +', ' ', text_block)
return text_block.strip()
#DEFINE PHRASE CLEAR FUNCTION
def phrase_clear(text_block):
"""
Removes underscores from tokens in tokenized data. Returns re-tokenized text.
INPUT:
text_block = tokenized text block containing n-grams to be removed.
OUTPUT:
Tokenized text block that has n-grams transformed back into individual words.
"""
cleared = []
for idx, word in enumerate(text_block):
cleared.extend(word.split("_"))
return cleared
#DEFINE REVIEW CLASS
class Review:
"""
Review class contains the date, text, star_rating, and sentiment polarity of a review.
"""
def __init__(self, text, date, star_rating):
self.text = split_sentences(text)
self.date = date
self.star_rating = star_rating
self.tokenized = False
self.polarity = 0
def get_pretty_review(self):
"""
This method will return the review text in a sentence format with the beginning of each sentence capitalized.
"""
if self.tokenized:
return '. '.join([' '.join(sentence).capitalize() for sentence in self.text])
else:
return '. '.join([sentence.capitalize() for sentence in self.text])
def get_review(self):
"""
This method will return the entire review as a single string.
"""
if self.tokenized:
return ' '.join([' '.join(sentence) for sentence in self.text])
else:
return ' '.join(self.text)
def get_all_tokens(self):
"""
This method will return all tokens for the review. Sentence structure is not preserved.
"""
if self.tokenized:
return [token for sentence in self.text for token in sentence]
else:
return None
def clean_text(self):
"""
This method applies text cleaning to the review text. It uses the clean_text function.
It should be called before tokenizing but can be called at any time.
"""
if self.tokenized:
self.text = [[clean_text(token) for token in sentence] for sentence in self.text]
self.drop_short_sentences(0)
else:
self.text = [clean_text(sentence) for sentence in self.text]
def remove_stopwords(self, stopwords):
"""
This method removes stopwords from the review text.
INPUT: stopwords = List of stopwords to be removed.
"""
if self.tokenized:
self.text = [[token for token in sentence if token not in stopwords] for sentence in self.text]
else:
self.text = [' '.join([word for word in sentence.split(sep=" ")
if word not in stopwords]) for sentence in self.text]
def remove_gibberish(self, vocab=vocab_en):
"""
This method removes words from review text that are not in the recognized vocabulary.
INPUT: vocab = The list of words in the vocabulary. Default is nltk corpus english.
"""
if self.tokenized:
self.text = [[token for token in sentence if token in vocab] for sentence in self.text]
else:
self.text = [' '.join([word for word in sentence.split(sep=" ")
if word in vocab]) for sentence in self.text]
def drop_short_sentences(self, length):
"""
This method removes short sentences.
INPUT: length = All sentences equal to or shorter than length will be dropped.
If tokenized, length refers to the number of words in the sentence.
If not tokenized, length refers to the number of characters in the sentence.
"""
#If tokenized, the length refers to number of words in the sentence.
#If not tokenized, the length refers to number of characters in the sentence.
self.text = [sentence for sentence in self.text if len(sentence)>length]
def drop_short_words(self, length):
"""
This method removes short words.
INPUT: length = All words equal to or shorter than length will be dropped.
"""
if self.tokenized:
self.text = [[word for word in sentence if len(word)>length]for sentence in self.text]
else:
self.text = [' '.join([word for word in sentence.split(sep=" ")
if len(word)>length]) for sentence in self.text]
def apply_phraser(self, phraser):
"""
Apply pre-fit phraser to the review text.
INPUT: phraser = Phraser to be applied.
"""
self.text = [phraser[sentence] for sentence in self.text]
def tokenize(self, tokenizer):
"""
Apply a tokenizer to the review text.
INPUT: tokenizer = Tokenizer to be applied.
"""
self.text = [tokenizer(sentence) for sentence in self.text]
self.tokenized = True
def assign_polarity(self):
"""
Assigns polarity to the review text using TextBlob.sentiment.polarity.
"""
self.polarity = TextBlob(self.get_review()).sentiment.polarity
def to_dict(self):
"""
Returns a dictionary containing all review information.
Keys: date, star_rating, polarity, review_text.
"""
return {'date':self.date,
'star_rating':self.star_rating,
'polarity':self.polarity,
'review_text':self.text
}
#DEFINE RESTAURANT CLASS
class Restaurant:
"""
The Restaurant Class contains the name, business id, address, category list, price range, star rating, keywords list,
and a list of Review objects for a restaurant.
"""
def __init__(self, name, business_index ,address, categories, price_range, star_rating):
self.name = name
self.biz_id = business_index
self.address = address
self.categories = categories
self.price_range = price_range
self.star_rating = star_rating
self.reviews = []
self.keywords = {1.0:[],2.0:[],3.0:[],4.0:[],5.0:[]}
def add_review(self, review_object):
"""
Add a single review object to the restaurant's reviews list.
INPUT: review_object = review to be added.
"""
self.reviews.append(review_object)
def set_reviews(self, review_list):
"""
Set all reviews for a restaurant.
This function overwrites any reviews previously stored for the restaurant.
INPUT: review_list = list of Review objects to be applied.
"""
self.reviews = review_list
def get_document(self, star_rating=0):
"""
Returns a document containing all tokens for all reviews.
INPUT: star_rating = If zero, return document for all reviews,
otherwise return document for specific star_rating level (1-5).
"""
all_reviews = []
if star_rating==0:
#return doc of all reviews
for review in self.reviews:
all_reviews.extend(review.get_all_tokens())
else:
#return doc of only reviews matching star rating
for review in self.reviews:
if review.star_rating==star_rating:
all_reviews.extend(review.get_all_tokens())
return all_reviews
def get_review_sentences(self, star_rating=0):
"""
Returns a list containing all sentences for all reviews.
INPUT: star_rating = If zero, return document for all reviews,
otherwise return sentences for specific star_rating level (1-5).
"""
review_sentences = []
if star_rating == 0:
for review in self.reviews:
review_sentences.extend(review.text)
else:
for review in self.reviews:
if review.star_rating==star_rating:
review_sentences.extend(review.text)
return review_sentences
def get_reviews(self, star_rating=0):
"""
Returns a list of tokenized reviews (list of lists).
"""
reviews_tokenized = []
if star_rating == 0:
for review in self.reviews:
reviews_tokenized.append(review.get_all_tokens())
else:
for review in self.reviews:
if review.star_rating==star_rating:
reviews_tokenized.append(review.get_all_tokens())
return reviews_tokenized
def get_review_objects(self):
"""
Return list of review objects.
"""
return self.reviews
def get_keywords(self):
"""
Return restaurant keywords.
OUTPUT:
List of dictionaries containing the following keys: Restaurant Id, star_rating, word, score.
"""
keywords_list = []
for rating in self.keywords.keys():
for word in self.keywords[rating]:
word['star_rating'] = rating
word['Restaurant Id'] = self.biz_id
keywords_list.append(word)
return keywords_list
def is_in_category(self, category_name):
"""
Return True if restaurant is part of the category.
INPUT: category_name = Text string to check for.
"""
if category_name in self.categories:
return True
else:
return False
def get_review_polarities_by_date(self):
"""
Returns a Pandas DataFrame containing the date and polarity for each review.
Columns = date, polarity
"""
#return dates[], polarities[]
#return tuple(map(list,zip(*[(review.date, review.polarity) for review in self.reviews])))
return pd.DataFrame.from_records([(review.date, review.polarity) for review in self.reviews],
columns =['date', 'polarity'])
def set_keywords(self, star_rating, words, values):
"""
Set keywords variable for a restaurant.
INPUTS:
star_rating = The star rating that the keywords apply to.
words = List of words to be added to the keywords list.
values = List of values (scores) for each word.
"""
self.keywords[star_rating] = [{'word':word, 'score':value} for word, value in zip(words, values)]
def to_dict(self):
"""
Returns a dictionary containing restaurant information without the reviews.
Keys: Business Name, Business Address, Business Index, Category, Price Range, Star Rating.
"""
return {'Business Name':self.name,
'Business Address':self.address,
'Business Index':self.biz_id,
'Category':self.categories,
'Price Range':self.price_range,
'Star Rating':self.star_rating
}