-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunkreview.py
More file actions
139 lines (118 loc) · 3.44 KB
/
Copy pathchunkreview.py
File metadata and controls
139 lines (118 loc) · 3.44 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
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
# -*- coding:utf-8 -*-
from nltk.tokenize import StanfordTokenizer
import nltk
from nltk import *
import codecs
from nltk.stem import WordNetLemmatizer
def readfile(filepath):
file_object = codecs.open(filepath,'r','utf-8')
try:
all_text = file_object.read()
finally:
file_object.close()
return all_text
linenoun = []
def traverse(t):
try:
t.label()
except AttributeError:
return linenoun
else:
if t.label()=='NP':
onenp =[]
arr = t.leaves()
for a in arr:
temp = a.split('/')
onenp.append(temp[0])
linenoun.append(onenp)
for child in t:
traverse(child)
else:
for child in t:
traverse(child)
def chunk(filepath):
all_text = readfile(filepath)
tokenizer = StanfordTokenizer()
wordnet_lemmatizer = WordNetLemmatizer()
arr = tokenizer.tokenize(all_text)
grammar = "NP: {<JJ>*<NN>+}"
cp = nltk.RegexpParser(grammar)
res = []
for line in arr:
ar = line.split()
newarr = []
for i in ar:
a = wordnet_lemmatizer.lemmatize(i)
newarr.append(a)
list = nltk.pos_tag(newarr)
if len(list)==0:
continue
result = cp.parse(list)
if len(result)==0:
continue
res.append(str(result))
return res
def getsymbol(file_path):
file_object = codecs.open(file_path,'r','utf-8')
try:
alltext = file_object.read()
symbols = alltext.split()
finally:
file_object.close()
return symbols
def create_txt(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:
for y in x:
for z in y:
f.write(z)
f.write(' ')
f.write('\n')
f.close()
def write_lines(file_path, lines):
if os.path.exists(file_path):
os.remove(file_path)
f=codecs.open(file_path,'w','utf-8')
for line in lines:
f.write(line)
f.write('\n')
f.close()
def isspace(line):
l = len(line)
re = ''
for i in range(l):
re+=' '
if re==line:
return True
return False
res = chunk("F:/course/sentimentcode/feature/data/corpuswithoutstop")
nounarr = []
for r in res:
tree=Tree.fromstring(r)
traverse(tree)
if len(linenoun) == 0:
continue
nounarr.append(linenoun)
linenoun = []#linenoun.clear() clear memory data
symbols = getsymbol('F:/course/sentimentcode/feature/symbol.txt')
nounpath = 'F:/course/sentimentcode/feature/data/noun'
create_txt(nounpath, nounarr)
file_object = codecs.open(nounpath,'r','utf-8')
newlines = []
try:
lines = file_object.readlines()
for line in lines:
line = line.replace('\n','')
for symbol in symbols:
if symbol in line:
line = line.replace(symbol,' ')
if len(line) == 0:
break
if not len(line) == 0 and not isspace(line):
newlines.append(line)
finally:
file_object.close()
newnounpath = 'F:/course/sentimentcode/feature/data/newnoun'
write_lines(newnounpath,newlines)