-
Notifications
You must be signed in to change notification settings - Fork 14
/
reviews_preprocessor.py
722 lines (572 loc) · 26.4 KB
/
reviews_preprocessor.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
import os
import random
import time
import langdetect
from langdetect import DetectorFactory
from langdetect.lang_detect_exception import LangDetectException
import numpy
from gensim import corpora
from nltk import PerceptronTagger
from nltk.corpus import stopwords
from etl import ETLUtils
from etl.reviews_dataset_analyzer import ReviewsDatasetAnalyzer
from evaluation import classifier_evaluator
from nlp import nlp_utils
from topicmodeling.context import lda_context_utils
from topicmodeling.context import topic_model_creator
from topicmodeling.context.context_extractor import ContextExtractor
from topicmodeling.context.context_transformer import ContextTransformer
from topicmodeling.context.reviews_classifier import ReviewsClassifier
from topicmodeling.nmf_topic_extractor import NmfTopicExtractor
from tripadvisor.fourcity import extractor
from utils import utilities
from utils.constants import Constants
from utils.utilities import all_context_words
class ReviewsPreprocessor:
def __init__(self, use_cache=False):
self.use_cache = use_cache
self.records = None
self.dictionary = None
def load_records(self):
print('%s: load records' % time.strftime("%Y/%m/%d-%H:%M:%S"))
self.records = ETLUtils.load_json_file(Constants.RECORDS_FILE)
def shuffle_records(self):
print('%s: shuffle records' % time.strftime("%Y/%m/%d-%H:%M:%S"))
random.shuffle(self.records)
self.records = self.records
def transform_yelp_records(self):
new_records = []
for record in self.records:
user_id = record['user_id']
item_id = record['business_id']
new_records.append(
{
Constants.REVIEW_ID_FIELD: record['review_id'],
Constants.USER_ID_FIELD: user_id,
Constants.ITEM_ID_FIELD: item_id,
Constants.RATING_FIELD: record['stars'],
Constants.TEXT_FIELD: record['text'],
Constants.USER_ITEM_KEY_FIELD: '%s|%s' %
(str(user_id), str(item_id)),
}
)
self.records = new_records
def transform_fourcity_records(self):
new_records = []
for record in self.records:
user_id = record['author']['id']
item_id = record['offering_id']
new_records.append(
{
Constants.REVIEW_ID_FIELD: record['id'],
Constants.USER_ID_FIELD: user_id,
Constants.ITEM_ID_FIELD: item_id,
Constants.RATING_FIELD: record['ratings']['overall'],
Constants.TEXT_FIELD: record['text'],
Constants.USER_ITEM_KEY_FIELD: '%s|%s' % (user_id, item_id),
}
)
self.records = new_records
def transform_records(self):
if 'yelp' in Constants.ITEM_TYPE:
self.transform_yelp_records()
elif 'fourcity' in Constants.ITEM_TYPE:
self.transform_fourcity_records()
def summarize_dataset(self):
rda = ReviewsDatasetAnalyzer(self.records)
print('density: %f' % rda.calculate_density_approx())
print('sparsity: %f' % rda.calculate_sparsity_approx())
print('total_records: %d' % len(self.records))
user_ids = \
extractor.get_groupby_list(self.records, Constants.USER_ID_FIELD)
item_ids = \
extractor.get_groupby_list(self.records, Constants.ITEM_ID_FIELD)
print('total users', len(user_ids))
print('total items', len(item_ids))
def add_integer_ids(self):
users_map = {}
items_map = {}
user_index = 0
item_index = 0
for record in self.records:
user_id = record[Constants.USER_ID_FIELD]
item_id = record[Constants.ITEM_ID_FIELD]
if user_id not in users_map:
users_map[user_id] = user_index
user_index += 1
if item_id not in items_map:
items_map[item_id] = item_index
item_index += 1
record[Constants.USER_INTEGER_ID_FIELD] = users_map[user_id]
record[Constants.ITEM_INTEGER_ID_FIELD] = items_map[item_id]
record[Constants.USER_ITEM_INTEGER_KEY_FIELD] = \
'%d|%d' % (users_map[user_id], items_map[item_id])
def tag_reviews_language(self):
print('%s: tag reviews language' % time.strftime("%Y/%m/%d-%H:%M:%S"))
if os.path.exists(Constants.LANGUAGE_RECORDS_FILE):
print('Records have already been tagged with language field')
self.records = \
ETLUtils.load_json_file(Constants.LANGUAGE_RECORDS_FILE)
return
DetectorFactory.seed = 0
for record in self.records:
try:
language = langdetect.detect(record[Constants.TEXT_FIELD])
except LangDetectException:
language = 'unknown'
record[Constants.LANGUAGE_FIELD] = language
ETLUtils.save_json_file(Constants.LANGUAGE_RECORDS_FILE, self.records)
def remove_foreign_reviews(self):
print('%s: remove foreign reviews' % time.strftime("%Y/%m/%d-%H:%M:%S"))
initial_length = len(self.records)
self.records = ETLUtils.filter_records(
self.records, Constants.LANGUAGE_FIELD, [Constants.LANGUAGE])
final_length = len(self.records)
removed_records_count = initial_length - final_length
percentage = removed_records_count / float(initial_length) * 100
msg = "A total of %d (%f%%) records were removed because their " \
"language was not '%s'" % (
removed_records_count, percentage, Constants.LANGUAGE)
print(msg)
def remove_users_with_low_reviews(self):
print('%s: remove users with low reviews' %
time.strftime("%Y/%m/%d-%H:%M:%S"))
# Remove from the dataset users with a low number of reviews
min_reviews_per_user = Constants.MIN_REVIEWS_PER_USER
if min_reviews_per_user is None or min_reviews_per_user < 2:
print('No records where removed')
return
num_records_before = len(self.records)
self.records = extractor.remove_users_with_low_reviews(
self.records, min_reviews_per_user)
num_records_removed = num_records_before - len(self.records)
print('A total of %d records where removed' % num_records_removed)
def remove_items_with_low_reviews(self):
print('%s: remove items with low reviews' % time.strftime(
"%Y/%m/%d-%H:%M:%S"))
# Remove from the dataset items with a low number of reviews
min_reviews_per_item = Constants.MIN_REVIEWS_PER_ITEM
if min_reviews_per_item is None or min_reviews_per_item < 2:
print('No records where removed')
return
num_records_before = len(self.records)
self.records = extractor.remove_items_with_low_reviews(
self.records, min_reviews_per_item)
num_records_removed = num_records_before - len(self.records)
print('A total of %d records where removed' % num_records_removed)
def clean_reviews(self):
print('%s: clean reviews' % time.strftime("%Y/%m/%d-%H:%M:%S"))
initial_length = len(self.records)
if Constants.ITEM_TYPE == 'fourcity_hotel':
self.records = ETLUtils.filter_out_records(
self.records, Constants.USER_ID_FIELD, ['', 'CATID_'])
final_length = len(self.records)
removed_records_count = initial_length - final_length
percentage = removed_records_count / float(initial_length) * 100
msg = "A total of %d (%f%%) records were removed because they " \
"were dirty" % (removed_records_count, percentage)
print(msg)
def remove_duplicate_reviews(self):
print('%s: remove duplicate records' % time.strftime(
"%Y/%m/%d-%H:%M:%S"))
ids_set = set()
non_duplicated_records = []
initial_length = len(self.records)
for record in self.records:
if record[Constants.USER_ITEM_INTEGER_KEY_FIELD] not in ids_set:
ids_set.add(record[Constants.USER_ITEM_INTEGER_KEY_FIELD])
non_duplicated_records.append(record)
self.records = non_duplicated_records
final_length = len(self.records)
removed_records_count = initial_length - final_length
percentage = removed_records_count / float(initial_length) * 100
msg = "A total of %d (%f%%) records were removed because they " \
"were duplicated" % (removed_records_count, percentage)
print(msg)
def remove_reviews_from_classifier_training_set(self):
"""
Removes the records that are part of the training set of the reviews
classifier
"""
classifier_records = \
ETLUtils.load_json_file(Constants.CLASSIFIED_RECORDS_FILE)
classifier_review_ids = \
{record[Constants.REVIEW_ID_FIELD] for record in classifier_records}
self.records = ETLUtils.filter_out_records(
self.records, Constants.REVIEW_ID_FIELD, classifier_review_ids)
def count_frequencies(self):
"""
Counts the number of reviews each user and item have and stores the
results in two separate files, one for the users and another one for the
items. Note that the integer IDs are used and not the original user and
item IDs
"""
print('%s: count frequencies' % time.strftime("%Y/%m/%d-%H:%M:%S"))
user_frequency_map = ETLUtils.count_frequency(
self.records, Constants.USER_INTEGER_ID_FIELD)
item_frequency_map = ETLUtils.count_frequency(
self.records, Constants.ITEM_INTEGER_ID_FIELD)
user_frequency_file = Constants.generate_file_name(
'user_frequency_map', 'json', Constants.CACHE_FOLDER, None, None,
False
)
item_frequency_file = Constants.generate_file_name(
'item_frequency_map', 'json', Constants.CACHE_FOLDER, None, None,
False
)
ETLUtils.save_json_file(user_frequency_file, [user_frequency_map])
ETLUtils.save_json_file(item_frequency_file, [item_frequency_map])
@staticmethod
def pos_tag_reviews(records):
print('%s: tag reviews' % time.strftime("%Y/%m/%d-%H:%M:%S"))
tagger = PerceptronTagger()
for record in records:
tagged_words =\
nlp_utils.tag_words(record[Constants.TEXT_FIELD], tagger)
record[Constants.POS_TAGS_FIELD] = tagged_words
@staticmethod
def lemmatize_reviews(records):
"""
Performs a POS tagging on the text contained in the reviews and
additionally finds the lemma of each word in the review
:type records: list[dict]
:param records: a list of dictionaries with the reviews
"""
print('%s: lemmatize reviews' % time.strftime("%Y/%m/%d-%H:%M:%S"))
record_index = 0
for record in records:
#
tagged_words =\
nlp_utils.lemmatize_text(record[Constants.TEXT_FIELD])
record[Constants.POS_TAGS_FIELD] = tagged_words
record_index += 1
return records
# print('')
@staticmethod
def lemmatize_sentences(records):
print('%s: lemmatize sentences' % time.strftime("%Y/%m/%d-%H:%M:%S"))
sentence_records = []
record_index = 0
document_level = Constants.DOCUMENT_LEVEL
for record in records:
sentences = \
nlp_utils.get_sentences(record[Constants.TEXT_FIELD])
sentence_index = 0
for sentence in sentences:
if isinstance(document_level, (int, float)) and\
sentence_index >= document_level:
break
tagged_words = nlp_utils.lemmatize_sentence(sentence)
sentence_record = {}
sentence_record.update(record)
sentence_record[Constants.TEXT_FIELD] = sentence
sentence_record['sentence_index'] = sentence_index
sentence_record[Constants.POS_TAGS_FIELD] = tagged_words
sentence_records.append(sentence_record)
sentence_index += 1
# print(sentence_record)
record_index += 1
# print('\rrecord index: %d/%d' % (record_index, len(records))),
return sentence_records
def lemmatize_records(self):
if os.path.exists(Constants.LEMMATIZED_RECORDS_FILE):
print('Records were already lemmatized')
self.records = \
ETLUtils.load_json_file(Constants.LEMMATIZED_RECORDS_FILE)
return
if Constants.DOCUMENT_LEVEL == 'review':
self.records = self.lemmatize_reviews(self.records)
elif Constants.DOCUMENT_LEVEL == 'sentence' or\
isinstance(Constants.DOCUMENT_LEVEL, (int, long)):
self.records = self.lemmatize_sentences(self.records)
ETLUtils.save_json_file(Constants.LEMMATIZED_RECORDS_FILE, self.records)
def classify_reviews(self):
print('%s: classify reviews' % time.strftime("%Y/%m/%d-%H:%M:%S"))
print(Constants.CLASSIFIED_RECORDS_FILE)
training_records =\
ETLUtils.load_json_file(Constants.CLASSIFIED_RECORDS_FILE)
# If document level set to sentence (can be either 'sentence' or int)
document_level = Constants.DOCUMENT_LEVEL
if document_level != 'review':
if document_level == 'sentence':
document_level = float("inf")
training_records = [
record for record in training_records
if record['sentence_index'] < document_level
]
for record in training_records:
record['specific'] = \
'yes' if record['sentence_type'] == 'specific' else 'no'
print('num training records', len(training_records))
training_records = self.lemmatize_reviews(training_records)
classifier = ReviewsClassifier(classifier_evaluator.load_pipeline())
classifier.train(training_records)
classifier.label_json_reviews(self.records)
def build_bag_of_words(self):
print('%s: build bag of words' % time.strftime("%Y/%m/%d-%H:%M:%S"))
bow_type = Constants.BOW_TYPE
cached_stop_words = set(stopwords.words("english"))
cached_stop_words |= {
't', 'didn', 'doesn', 'haven', 'don', 'aren', 'isn', 've', 'll',
'couldn', 'm', 'hasn', 'hadn', 'won', 'shouldn', 's', 'wasn',
'wouldn'}
if Constants.LEMMATIZE:
tagged_word_index = 2
else:
tagged_word_index = 0
for record in self.records:
bag_of_words = []
tagged_words = record[Constants.POS_TAGS_FIELD]
for tagged_word in tagged_words:
if bow_type is None or tagged_word[1].startswith(bow_type):
bag_of_words.append(tagged_word[tagged_word_index])
bag_of_words = [
word for word in bag_of_words if word not in cached_stop_words
]
record[Constants.BOW_FIELD] = bag_of_words
def build_dictionary(self):
print('%s: build dictionary' % time.strftime("%Y/%m/%d-%H:%M:%S"))
if self.use_cache and os.path.exists(Constants.DICTIONARY_FILE):
print('Dictionary already exists')
self.dictionary = corpora.Dictionary.load(Constants.DICTIONARY_FILE)
return
all_words = []
for record in self.records:
all_words.append(record[Constants.BOW_FIELD])
self.dictionary = corpora.Dictionary(all_words)
self.dictionary.filter_extremes(
Constants.MIN_DICTIONARY_WORD_COUNT,
Constants.MAX_DICTIONARY_WORD_COUNT)
self.dictionary.save(Constants.DICTIONARY_FILE)
def tag_contextual_reviews(self):
"""
Puts a tag of contextual or non-contextual to the the records that have
contextual words (the ones that appear in the manually defined set in
topics_analyzer.all_context_words)with those records
"""
context_words = set(all_context_words[Constants.ITEM_TYPE])
num_context_records = 0
num_no_context_records = 0
for record in self.records:
words = set(record[Constants.BOW_FIELD])
# If there is an intersection
if context_words & words:
record[Constants.HAS_CONTEXT_FIELD] = True
num_context_records += 1
else:
record[Constants.HAS_CONTEXT_FIELD] = False
num_no_context_records += 1
print('important records: %d' % len(self.records))
print('context records: %d' % num_context_records)
print('no context records: %d' % num_no_context_records)
def build_corpus(self):
print('%s: build corpus' % time.strftime("%Y/%m/%d-%H:%M:%S"))
for record in self.records:
record[Constants.CORPUS_FIELD] =\
self.dictionary.doc2bow(record[Constants.BOW_FIELD])
def export_records(self):
print('%s: export records' % time.strftime("%Y/%m/%d-%H:%M:%S"))
ETLUtils.save_json_file(
Constants.FULL_PROCESSED_RECORDS_FILE, self.records)
self.drop_unnecessary_fields()
ETLUtils.save_json_file(Constants.PROCESSED_RECORDS_FILE, self.records)
def label_review_targets(self):
if Constants.TOPIC_MODEL_TARGET_TYPE == 'context':
for record in self.records:
record[Constants.TOPIC_MODEL_TARGET_FIELD] = \
record[Constants.PREDICTED_CLASS_FIELD]
elif Constants.TOPIC_MODEL_TARGET_TYPE == 'sentiment':
for record in self.records:
rating = record[Constants.RATING_FIELD]
sentiment = 'positive' if rating >= 4.0 else\
('neutral' if rating >= 3.0 else 'negative')
record[Constants.TOPIC_MODEL_TARGET_FIELD] = sentiment
@staticmethod
def find_topic_distribution(records):
print('%s: finding topic distributions'
% time.strftime("%Y/%m/%d-%H:%M:%S"))
if Constants.TOPIC_MODEL_TYPE == 'lda':
topic_model = topic_model_creator.load_topic_model(None, None)
corpus = [record[Constants.CORPUS_FIELD] for record in records]
lda_context_utils.update_reviews_with_topics(
topic_model, corpus, records)
elif Constants.TOPIC_MODEL_TYPE == 'ensemble':
topic_extractor = NmfTopicExtractor()
topic_extractor.load_trained_data()
topic_extractor.update_reviews_with_topics(records)
else:
raise ValueError('unrecognized topic modeling algorithm: \'%s\'' %
Constants.TOPIC_MODEL_TYPE)
if Constants.TOPIC_MODEL_NORMALIZE:
ReviewsPreprocessor.normalize_topics(records)
@staticmethod
def normalize_topics(records):
print('%s: normalizing topics' % time.strftime("%Y/%m/%d-%H:%M:%S"))
num_topics = len(records[0][Constants.TOPICS_FIELD])
for record in records:
topics = record[Constants.TOPICS_FIELD]
normalized_topics = []
total_topics_weight = 0.0
for topic in topics:
topic_weight = topic[1]
total_topics_weight += topic_weight
if total_topics_weight > 0:
for topic in topics:
normalized_topics.append(
(topic[0], topic[1] / total_topics_weight))
else:
for topic in topics:
normalized_topics.append(
(topic[0], 1.0 / num_topics))
record[Constants.TOPICS_FIELD] = normalized_topics
@staticmethod
def update_context_topics(records):
print('%s: updating records with contextual information'
% time.strftime("%Y/%m/%d-%H:%M:%S"))
context_extractor = ContextExtractor(records)
context_extractor.separate_reviews()
context_extractor.get_context_rich_topics()
context_extractor.find_contextual_topics(records)
def separate_recsys_topic_model_records(self):
print('%s: separate_recsys_topic_model_records' %
time.strftime("%Y/%m/%d-%H:%M:%S"))
num_records = len(self.records)
topic_model_records = self.records[:num_records / 2]
if not Constants.USE_CONTEXT:
recsys_records = self.records[num_records / 2:]
file_name = \
Constants.generate_file_name(
'recsys_contextual_records', 'json', Constants.CACHE_FOLDER,
None, None, False, True)
print('Records without context file: %s' % file_name)
for record in recsys_records:
record[Constants.CONTEXT_TOPICS_FIELD] = {'na': 1.0}
ETLUtils.save_json_file(file_name, recsys_records)
return
topic_model_creator.train_topic_model(topic_model_records)
if os.path.exists(Constants.RECSYS_TOPICS_PROCESSED_RECORDS_FILE):
print('Recsys topic records have already been generated')
recsys_records = ETLUtils.load_json_file(
Constants.RECSYS_TOPICS_PROCESSED_RECORDS_FILE)
else:
recsys_records = self.records[num_records / 2:]
self.find_topic_distribution(recsys_records)
ETLUtils.save_json_file(
Constants.RECSYS_TOPICS_PROCESSED_RECORDS_FILE, recsys_records)
if os.path.exists(Constants.RECSYS_CONTEXTUAL_PROCESSED_RECORDS_FILE):
print('Recsys contextual records have already been generated')
print(Constants.RECSYS_CONTEXTUAL_PROCESSED_RECORDS_FILE)
recsys_records = ETLUtils.load_json_file(
Constants.RECSYS_CONTEXTUAL_PROCESSED_RECORDS_FILE)
else:
self.update_context_topics(recsys_records)
ETLUtils.save_json_file(
Constants.RECSYS_CONTEXTUAL_PROCESSED_RECORDS_FILE,
recsys_records
)
context_transformer = ContextTransformer(recsys_records)
context_transformer.load_data()
context_transformer.transform_records()
context_transformer.export_records()
def drop_unnecessary_fields(self):
print(
'%s: drop unnecessary fields' % time.strftime("%Y/%m/%d-%H:%M:%S"))
unnecessary_fields = [
Constants.TEXT_FIELD,
Constants.POS_TAGS_FIELD,
# Constants.BOW_FIELD
]
ETLUtils.drop_fields(unnecessary_fields, self.records)
def load_full_records(self):
records_file = Constants.FULL_PROCESSED_RECORDS_FILE
self.records = ETLUtils.load_json_file(records_file)
def count_specific_generic_ratio(self):
"""
Prints the proportion of specific and generic documents
"""
specific_count = 0.0
generic_count = 0.0
for record in self.records:
if record[Constants.PREDICTED_CLASS_FIELD] == 'specific':
specific_count += 1
if record[Constants.PREDICTED_CLASS_FIELD] == 'generic':
generic_count += 1
print('Specific reviews: %f%%' % (
specific_count / len(self.records) * 100))
print('Generic reviews: %f%%' % (
generic_count / len(self.records) * 100))
print('Specific reviews: %d' % specific_count)
print('Generic reviews: %d' % generic_count)
def export_to_triplet(self):
print('%s: export to triplet' % time.strftime("%Y/%m/%d-%H:%M:%S"))
users_map = {}
items_map = {}
user_index = 0
item_index = 0
triplet_list = []
for record in self.records:
user_id = record[Constants.USER_ID_FIELD]
item_id = record[Constants.ITEM_ID_FIELD]
if user_id not in users_map:
users_map[user_id] = user_index
user_index += 1
if item_id not in items_map:
items_map[item_id] = item_index
item_index += 1
triplet = [users_map[user_id], items_map[item_id],
record[Constants.RATING_FIELD]]
triplet_list.append(triplet)
matrix = numpy.array(triplet_list)
print('matrix shape', matrix.shape)
with open(Constants.RATINGS_FILE, 'w') as f:
numpy.savetxt(f, matrix, fmt='%d')
print('Records exported as a triplet in: %s' % Constants.RATINGS_FILE)
def preprocess(self):
self.load_records()
self.transform_records()
self.summarize_dataset()
self.add_integer_ids()
self.clean_reviews()
self.remove_duplicate_reviews()
self.tag_reviews_language()
self.remove_foreign_reviews()
self.remove_reviews_from_classifier_training_set()
self.lemmatize_records()
self.remove_users_with_low_reviews()
self.remove_items_with_low_reviews()
self.count_frequencies()
self.shuffle_records()
print('total_records: %d' % len(self.records))
self.classify_reviews()
self.build_bag_of_words()
self.tag_contextual_reviews()
# self.load_full_records()
self.build_dictionary()
self.build_corpus()
self.label_review_targets()
self.export_records()
self.count_specific_generic_ratio()
self.export_to_triplet()
self.summarize_dataset()
def full_cycle(self):
Constants.print_properties()
print('%s: full cycle' % time.strftime("%Y/%m/%d-%H:%M:%S"))
utilities.plant_seeds()
if self.use_cache and \
os.path.exists(Constants.PROCESSED_RECORDS_FILE):
print('Records have already been processed')
self.records = \
ETLUtils.load_json_file(Constants.PROCESSED_RECORDS_FILE)
else:
self.preprocess()
if Constants.SEPARATE_TOPIC_MODEL_RECSYS_REVIEWS:
self.separate_recsys_topic_model_records()
def main():
reviews_preprocessor = ReviewsPreprocessor(use_cache=True)
reviews_preprocessor.full_cycle()
# reviews_preprocessor.preprocess()
# start = time.time()
# main()
# end = time.time()
# total_time = end - start
# print("Total time = %f seconds" % total_time)