-
Notifications
You must be signed in to change notification settings - Fork 5
/
dependency_graph.py
138 lines (116 loc) · 4.78 KB
/
dependency_graph.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
# -*- coding: utf-8 -*-
import numpy as np
import spacy
import pickle
from spacy.tokens import Doc
class WhitespaceTokenizer(object):
def __init__(self, vocab):
self.vocab = vocab
def __call__(self, text):
words = text.split()
# All tokens 'own' a subsequent space character in this tokenizer
spaces = [True] * len(words)
return Doc(self.vocab, words=words, spaces=spaces)
nlp = spacy.load('en_core_web_sm')
nlp.tokenizer = WhitespaceTokenizer(nlp.vocab)
def dependency_adj_matrix(text):
# https://spacy.io/docs/usage/processing-text
tokens = nlp(text)
words = text.split()
matrix = np.zeros((len(words), len(words))).astype('float32')
assert len(words) == len(list(tokens))
for token in tokens:
matrix[token.i][token.i] = 1
for child in token.children:
matrix[token.i][child.i] = 1
matrix[child.i][token.i] = 1
return matrix
def process(filename):
fin = open(filename, 'r', encoding='utf-8', newline='\n', errors='ignore')
lines = fin.readlines()
fin.close()
idx2graph = {}
fout = open(filename+'.graph', 'wb')
for i in range(0, len(lines), 3):
text_left, _, text_right = [s.strip() for s in lines[i].partition("$T$")]
aspect = lines[i + 1].strip()
adj_matrix = dependency_adj_matrix(text_left+' '+aspect+' '+text_right)
idx2graph[i] = adj_matrix
pickle.dump(idx2graph, fout)
fout.close()
def cl_process(filename):
fin = open(filename, 'r', encoding='utf-8', newline='\n', errors='ignore')
lines = fin.readlines()
fin.close()
idx2graph = {}
fout = open(filename+'.graph', 'wb')
for i in range(0, len(lines), 4):
text_left, _, text_right = [s.strip() for s in lines[i].partition("$T$")]
aspect = lines[i + 1].strip()
adj_matrix = dependency_adj_matrix(text_left+' '+aspect+' '+text_right)
idx2graph[i] = adj_matrix
pickle.dump(idx2graph, fout)
fout.close()
def cl2X3_process(filename):
fin = open(filename, 'r', encoding='utf-8', newline='\n', errors='ignore')
lines = fin.readlines()
fin.close()
idx2graph = {}
fout = open(filename+'.graph', 'wb')
for i in range(0, len(lines), 5):
text_left, _, text_right = [s.strip() for s in lines[i].partition("$T$")]
aspect = lines[i + 1].strip()
adj_matrix = dependency_adj_matrix(text_left+' '+aspect+' '+text_right)
idx2graph[i] = adj_matrix
pickle.dump(idx2graph, fout)
fout.close()
if __name__ == '__main__':
# process("./datasets/semeval14/restaurant_train.raw")
# process("./datasets/semeval14/restaurant_test.raw")
#
# process("./datasets/semeval15/restaurant_train.raw")
# process("./datasets/semeval15/restaurant_test.raw")
#
# process("./datasets/semeval16/restaurant_train.raw")
# process("./datasets/semeval16/restaurant_test.raw")
#
# process("./datasets/MAMS/mams_test.raw")
# process("./datasets/MAMS/mams_train.raw")
#
# process("./datasets/acl-14-short-data/train.raw")
# process("./datasets/acl-14-short-data/test.raw")
#
# process("./datasets/semeval14/laptop_train.raw")
# process("./datasets/semeval14/laptop_test.raw")
#-----
# cl_process("./datasets/cl_data/2014acl_cl.raw")
# cl_process("./datasets/cl_data/2014acl_cl_6.raw")
#
# cl_process("./datasets/cl_data/2014laptop_cl.raw")
# cl_process("./datasets/cl_data/2014laptop_cl_6.raw")
#
# cl_process("./datasets/cl_data/2014res_cl.raw")
# cl_process("./datasets/cl_data/2014res_cl_6.raw")
#
# cl_process("./datasets/cl_data/2015res_cl.raw")
# cl_process("./datasets/cl_data/2015res_cl_6.raw")
#
# cl_process("./datasets/cl_data/2016res_cl.raw")
# cl_process("./datasets/cl_data/2016res_cl_6.raw")
#
# cl_process("./datasets/cl_data/mams_cl.raw")
# cl_process("./datasets/cl_data/mams_cl_6.raw")
#----
# cl2X3_process("./datasets/cl_data_2X3/2014acl_cl_2X3.raw")
# cl2X3_process("./datasets/cl_data_2X3/2014laptop_cl_2X3.raw")
# cl2X3_process("./datasets/cl_data_2X3/2014res_cl_2X3.raw")
# cl2X3_process("./datasets/cl_data_2X3/2015res_cl_2X3.raw")
# cl2X3_process("./datasets/cl_data_2X3/2016res_cl_2X3.raw")
# cl2X3_process("./datasets/cl_data_2X3/mams_cl_2X3.raw")
process("./datasets/No_overlap_aspect_data/not_overlap_aspect_acl14_test.raw")
process("./datasets/No_overlap_aspect_data/not_overlap_aspect_laptop_test.raw")
process("./datasets/No_overlap_aspect_data/not_overlap_aspect_rest15_test.raw")
process("./datasets/No_overlap_aspect_data/not_overlap_aspect_rest16_test.raw")
process("./datasets/No_overlap_aspect_data/not_overlap_aspect_rest14_test.raw")
process("./datasets/No_overlap_aspect_data/not_overlap_aspect_mams_test.raw")
pass