-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetnewopinion.py
More file actions
57 lines (54 loc) · 1.66 KB
/
Copy pathgetnewopinion.py
File metadata and controls
57 lines (54 loc) · 1.66 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
import os
import codecs
import nltk
import enchant
import re
wordlist = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
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 create_txt1(file_path, content):
if os.path.exists(file_path):
os.remove(file_path)
f=codecs.open(file_path,'w','utf-8')
for x in content:
f.write(x)
f.write('\n')
f.close()
def featureshandle():
featurearr = getopnion("F:/course/sentimentcode/feature/data/opinion")
newfeature = []
d = enchant.Dict("en_US")
for feature in featurearr:
arr = []
#handle with misspelled word
if not d.check(feature):
suggest = d.suggest(feature)
if len(suggest)==0:
feature = ""
else:
feature = suggest[0]
if feature=="":
continue
#remove single-word word
if feature in wordlist:
continue
#remove word containing digit
if re.search('\d', feature):
continue
newfeature.append(feature)
# arr.append(feature)
# taglist = nltk.pos_tag(arr)
# for tag in taglist:
# if tag[1]=='JJ':
# newfeature.append(tag[0])
# else:
# print(tag)
create_txt1("F:/course/sentimentcode/feature/data/newopinion", newfeature)
featureshandle()