forked from azk0019/CourseProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.py
More file actions
59 lines (50 loc) · 1.71 KB
/
analyzer.py
File metadata and controls
59 lines (50 loc) · 1.71 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 stanza
class Analyzer():
def __init__(self, expression):
self.nlp = stanza.Pipeline('ja')
self.expression = expression
self.data = []
def load_data_from_path(self, path):
with open (path, 'r') as fin:
lines = fin.readlines()
for line in lines:
divided = line.split('\t')
self.data.append(divided[1].strip()) # 0 was the tag type, here we just need 1
def load_data_directly(self, results):
for tup in results:
text = tup[1] # we only need the text, not the tag it came from
self.data.append(text.strip())
def analyze_data(self):
for d in self.data:
try:
doc = self.nlp(d)
print('\nExtracted text: ', d)
for sentence in doc.sentences:
# print('printing sentence\n', sentence)
for word in sentence.words:
print('{}\t{}\t{}'.format(word.text, word.xpos, word.upos))
except:
print('STANZA PROCESSING ERROR: Stanza could not properly handle this text.')
def write_analysis(self):
f_name = self.expression + ' analysis.txt'
print('\n*** WRITING DATA TO FILE: ', f_name, ' ***')
fout = open(f_name, "w")
fout.write('Sentences and phrases containing target expression {} ...\n'.format(self.expression))
for d in self.data:
fout.write('\nExtracted text: ' + d + '\n')
try:
doc = self.nlp(d)
for sentence in doc.sentences:
for word in sentence.words:
line = word.text + '\t' + word.xpos + '\t' + word.upos + '\n'
fout.write(line)
except:
line = 'STANZA PROCESSING ERROR: Stanza could not properly handle this text.\n'
fout.write(line)
fout.write('\n')
fout.close()
if __name__=='__main__':
analyzer = Analyzer('黒文字')
analyzer.load_data_from_path('sample.txt')
analyzer.analyze_data()
analyzer.write_analysis()