-
Notifications
You must be signed in to change notification settings - Fork 4
/
build_dependency_graph.py
138 lines (118 loc) · 4.06 KB
/
build_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
import os
import stanza
import pickle
import sys
import random
import numpy as np
from tqdm import tqdm
# change your mkdir path of stanfordnlp model
nlp = stanza.Pipeline(lang='en', model_dir='D:\zhangchong\projects\exp\exp_for_git\OpenNRE\stanza_resources')
if len(sys.argv) < 1:
sys.exit("Use: python build_dependency_graph.py <dataset>")
dataset = sys.argv[1]
# load pre-trained word embeddings
word_embeddings_dim = 300
word_embeddings = {}
with open('glove.6B.' + str(word_embeddings_dim) + 'd.txt', 'r', encoding='utf-8') as f:
for line in f.readlines():
data = line.split()
word_embeddings[str(data[0])] = list(map(float, data[1:]))
# load label list
doc_name_list = []
doc_train_list = []
doc_test_list = []
with open('data/' + dataset + '.txt', 'r', encoding='utf-8') as f:
for line in f.readlines():
doc_name_list.append(line.strip())
temp = line.split("\t")
if temp[1].find('test') != -1:
doc_test_list.append(line.strip())
elif temp[1].find('train') != -1:
doc_train_list.append(line.strip())
# load content
doc_content_list = []
with open('data/corpus/' + dataset + '.clean.txt', 'r', encoding='utf-8') as f:
for line in f.readlines():
doc_content_list.append(line.strip())
# map and shuffle
train_ids = []
for train_name in doc_train_list:
train_id = doc_name_list.index(train_name)
train_ids.append(train_id)
random.shuffle(train_ids)
test_ids = []
for test_name in doc_test_list:
test_id = doc_name_list.index(test_name)
test_ids.append(test_id)
random.shuffle(test_ids)
ids = train_ids + test_ids
# train + test
shuffle_doc_name_list = []
shuffle_doc_words_list = []
for i in ids:
shuffle_doc_name_list.append(doc_name_list[int(i)])
shuffle_doc_words_list.append(doc_content_list[int(i)])
# build label list
label_set = set()
for doc_meta in shuffle_doc_name_list:
temp = doc_meta.split('\t')
label_set.add(temp[2])
label_list = list(label_set)
train_size = len(train_ids)
test_size = len(test_ids)
def build_dptree(start, end):
# build dptree data
trees = []
labels = []
features = []
for i in tqdm(range(start, end)):
content = shuffle_doc_words_list[i]
doc = nlp(content)
doc_dt = []
doc_feature = []
# trees features
bias = 0
for sentence in doc.sentences:
word_num = 0
for word in sentence.words:
word_num += 1
doc_feature.append(word_embeddings[word.text] if word.text in word_embeddings else np.random.uniform(-0.01, 0.01, word_embeddings_dim))
if word.head == 0:
deptree = {'id': int(word.id) + bias,
'text': word.text,
'parent': word.head}
else:
deptree = {'id': int(word.id) + bias,
'text': word.text,
'parent': word.head + bias}
doc_dt.append(deptree)
bias += word_num
trees.append(doc_dt)
features.append(doc_feature)
# labels
for i in range(start, end):
doc_meta = shuffle_doc_name_list[i]
temp = doc_meta.split('\t')
label = temp[2]
one_hot = [0 for l in range(len(label_list))]
label_index = label_list.index(label)
one_hot[label_index] = 1
labels.append(one_hot)
labels = np.array(labels)
return trees, features, labels
print('building graphs for training')
train_tree, train_feature, train_label = build_dptree(0, train_size)
print('building graphs for test')
test_tree, test_feature, test_label = build_dptree(train_size, train_size + test_size)
data = {'train_tree': train_tree,
'train_feature': train_feature,
'train_label': train_label,
'test_tree': test_tree,
'test_feature': test_feature,
'test_label': test_label
}
root_path = 'trees/'
if not os.path.exists(root_path):
os.makedirs(root_path)
fout = open(root_path + dataset + '.pickle', 'wb')
pickle.dump(data, fout)