def addReview(self, reviewDict): polarity = reviewDict["sentiment"] tempDict = {} tempDict["date"] = dt.datetime.strptime(reviewDict["date"], '%Y-%m-%d').date().year tempDict["negative"] = 0 tempDict["positive"] = 0 tempDict["neutral"] = 0 tempDict["sentiment"] = polarity tempDict["count"] = 1 tempDict["stars"] = reviewDict["stars"] if polarity < -0.05: tempDict["negative"] += 1 for word in tm.getWordVector(reviewDict["text"]): self.negativeCorpus.append(word) elif polarity > 0.05: tempDict["positive"] += 1 else: tempDict["neutral"] += 1 isPresent = False for item in self.dict["violations"]: if item["date"] == tempDict["date"]: if "sentiment" not in item: item["negative"] = tempDict["negative"] item["positive"] = tempDict["positive"] item["neutral"] = tempDict["neutral"] item["sentiment"] = tempDict["sentiment"] item["stars"] = tempDict["stars"] item["count"] = tempDict["count"] else: item["negative"] += tempDict["negative"] item["positive"] += tempDict["positive"] item["neutral"] += tempDict["neutral"] item["sentiment"] += tempDict["sentiment"] item["stars"] += tempDict["stars"] item["count"] += tempDict["count"] isPresent = True if not isPresent: self.dict["violations"].append(tempDict)
# "Fast Food Items", # "Seafood Menu Items", # "Ambiance & Hospitality", # "Dinner & Drinks"] # pickle.dump(topics, open("trained-model-topics",'w')) # topics = [] topics = pickle.load(file("trained-model-topics")) ldaModel = pickle.load(file("trained-negative-model")) ldaDictionary = pickle.load(file("model-dictionary")) print "Dictionary and Trained Model loaded!" # print ldaModel.show_topics(num_topics=10, num_words=10, formatted=False) for item in reviewsSet: topics_found = [] models = ldaModel[ldaDictionary.doc2bow(tm.getWordVector(item))] models = sorted(models, key=lambda k: k[1], reverse = True) # print models # if len(models) == 2: # print "-"*5 + str(topics[models[0][0]]) + "-"*5 + str(topics[models[1][0]]) + "-"*5 # print item for single_topic in models: topics_found.append(topics[single_topic[0]]) if len(topics_found) > 2: break print "-"*5 + str(topics_found) + "-"*5 print item c.close()