-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaspect_extraction.py
164 lines (145 loc) · 4.68 KB
/
aspect_extraction.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
import pandas as pd
pd.set_option('display.width', 2000)
import numpy as np
import csv
import os
from stanfordcorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP(r'..\coreNLP\stanford-corenlp-full-2018-02-27')
def read_file_as_str(file_path):
if not os.path.isfile(file_path):
raise TypeError(file_path + " does not exist")
all_the_text = open(file_path).read()
return all_the_text
import datetime
s = datetime.datetime.now()
### BOOK
### BOOK
### BOOK
print("---------- book start---- book start--- book start---")
for i in range(0, 3000):
num = 0
file = read_file_as_str('../Book/pos/%u.txt' % i)
pos_tag = nlp.pos_tag(file)
for pos in pos_tag:
if pos[1] == 'NN':
num += 1
with open('../Book/pos_aspect/%u.txt' % i, "a") as f:
f.write(pos[0])
f.write(' ')
if num == 0:
with open('../Book/pos_aspect/%u.txt' % i, "a") as f:
f.write('null')
print("%u is no target", i)
for i in range(0, 3000):
num = 0
file = read_file_as_str('../Book/neg/%u.txt' % i)
pos_tag = nlp.pos_tag(file)
for pos in pos_tag:
if pos[1] == 'NN':
num += 1
with open('../Book/neg_aspect/%u.txt' % i, "a") as f:
f.write(pos[0])
f.write(' ')
if num == 0:
with open('../Book/neg_aspect/%u.txt' % i, "a") as f:
f.write('null')
print("%u is no target", i)
### DVD
### DVD
### DVD
print("---------- dvd start---- dvd start--- dvd start---")
for i in range(0, 3000):
num = 0
file = read_file_as_str('../DVD/pos/%u.txt' % i)
pos_tag = nlp.pos_tag(file)
for pos in pos_tag:
if pos[1] == 'NN':
num += 1
with open('../DVD/pos_aspect/%u.txt' % i, "a") as f:
f.write(pos[0])
f.write(' ')
if num == 0:
with open('../DVD/pos_aspect/%u.txt' % i, "a") as f:
f.write('null')
print("file txt %u is no target", i)
for i in range(0, 3000):
num = 0
file = read_file_as_str('../DVD/neg/%u.txt' % i)
pos_tag = nlp.pos_tag(file)
for pos in pos_tag:
if pos[1] == 'NN':
num += 1
with open('../DVD/neg_aspect/%u.txt' % i, "a") as f:
f.write(pos[0])
f.write(' ')
if num == 0:
with open('../DVD/neg_aspect/%u.txt' % i, "a") as f:
f.write('null')
print("file txt %u is no target", i)
### Electronics
### Electronics
### Electronics
print("---------- ele start---- ele start--- ele start---")
for i in range(0, 3000):
num = 0
file = read_file_as_str('../Electronics/pos/%u.txt' % i)
pos_tag = nlp.pos_tag(file)
for pos in pos_tag:
if pos[1] == 'NN':
num += 1
with open('../Electronics/pos_aspect/%u.txt' % i, "a") as f:
f.write(pos[0])
f.write(' ')
if num == 0:
with open('../Electronics/pos_aspect/%u.txt' % i, "a") as f:
f.write('null')
print("file txt %u is no target", i)
for i in range(0, 3000):
num = 0
file = read_file_as_str('../Electronics/neg/%u.txt' % i)
pos_tag = nlp.pos_tag(file)
for pos in pos_tag:
if pos[1] == 'NN':
num += 1
with open('../Electronics/neg_aspect/%u.txt' % i, "a") as f:
f.write(pos[0])
f.write(' ')
if num == 0:
with open('../Electronics/neg_aspect/%u.txt' % i, "a") as f:
f.write('null')
print("file txt %u is no target", i)
### Kitchen
### Kitchen
### Kitchen
print("---------- kitchen start---- kitchen start--- kitchen start---")
for i in range(0, 3000):
num = 0
file = read_file_as_str('../Kitchen/pos/%u.txt' % i)
pos_tag = nlp.pos_tag(file)
for pos in pos_tag:
if pos[1] == 'NN':
num += 1
with open('../Kitchen/pos_aspect/%u.txt' % i, "a") as f:
f.write(pos[0])
f.write(' ')
if num == 0:
with open('../Kitchen/pos_aspect/%u.txt' % i, "a") as f:
f.write('null')
print("file txt %u is no target", i)
for i in range(0, 3000):
file = read_file_as_str('../Kitchen/neg/%u.txt' % i)
num = 0
pos_tag = nlp.pos_tag(file)
for pos in pos_tag:
if pos[1] == 'NN':
num += 1
with open('../Kitchen/neg_aspect/%u.txt' % i, "a") as f:
f.write(pos[0])
f.write(' ')
if num == 0:
with open('../Kitchen/neg_aspect/%u.txt' % i, "a") as f:
f.write('null')
print("file txt %u is no target", i)
nlp.close()
e = datetime.datetime.now()
print("Time last : ", e-s)