-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummerizeReview.py
More file actions
81 lines (70 loc) · 2.49 KB
/
Copy pathsummerizeReview.py
File metadata and controls
81 lines (70 loc) · 2.49 KB
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
import codecs
import os
from textblob import TextBlob
def getopnion(file_path):
file_object = codecs.open(file_path,'r','utf-8')
featurearr = []
try:
all_text = file_object.read()
arr = all_text.split()
finally:
file_object.close()
return arr
def getreview(file_path):
file_object = codecs.open(file_path,'r','utf-8')
featurearr = []
try:
lines = file_object.readlines()
finally:
file_object.close()
return lines
def getpolarity():
reviewdic = readJsontodic("F:/course/sentimentcode/feature/data/featuredic")
polarityscore = dict()
for word,indexandarr in reviewdic.items():
if not word in polarityscore:
polarityscore[word]=[]
polarityscore[word].append([])
polarityscore[word].append([])
polarityscore[word].append([])
for indexdic in indexandarr:
for index,wordarr,in indexdic.items():
score = 0
for w in wordarr:
blob = TextBlob(w)
for sentence in blob.sentences:
score += sentence.sentiment.polarity
if score < 0:
polarityscore[word][0].append(index)
if score == 0:
polarityscore[word][1].append(index)
if score > 0:
polarityscore[word][2].append(index)
return polarityscore
import json
def readJsontodic(filepath):
file_object = codecs.open(filepath,'r','utf-8')
dic = json.load(file_object)
return dic
def writepolarity(file_path):
polarityscore = getpolarity()
reviewlines = getreview("F:/course/sentimentcode/feature/data/corpus")
if os.path.exists(file_path):
os.remove(file_path)
f=codecs.open(file_path,'w','utf-8')
for word,sentencesarr in polarityscore.items():
f.write(word+':\n')
f.write("postive:\n")
for i in sentencesarr[2]:
f.write(reviewlines[int(i)])
f.write(word+':\n')
f.write("negative:\n")
for i in sentencesarr[0]:
f.write(reviewlines[int(i)])
f.write(word+':\n')
f.write("neutral:\n")
for i in sentencesarr[1]:
f.write(reviewlines[int(i)])
f.write('\n')
f.close()
writepolarity("F:/course/sentimentcode/feature/data/reviewsummerization")