-
Notifications
You must be signed in to change notification settings - Fork 0
/
derive_cross_graph.py
72 lines (61 loc) · 2.34 KB
/
derive_cross_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
# -*- coding: utf-8 -*-
import numpy as np
import spacy
import pickle
nlp = spacy.load('en_core_web_sm')
def load_seed_word(path):
seed_words = {}
fp = open(path, 'r')
for line in fp:
line = line.strip()
if not line:
continue
word, weight = line.split('\t')
seed_words[word] = weight
fp.close()
return seed_words
def dependency_adj_matrix(text, seed_words):
# https://spacy.io/docs/usage/processing-text
document = nlp(text)
seq_len = len(text.split())
matrix = np.zeros((seq_len, seq_len)).astype('float32')
for token in document:
if str(token) in seed_words:
weight = float(seed_words[str(token)])
else:
weight = 1
if token.i < seq_len:
matrix[token.i][token.i] = 1 * weight * 2
# https://spacy.io/docs/api/token
for child in token.children:
if str(child) in seed_words:
weight += float(seed_words[str(child)])
if child.i < seq_len:
matrix[token.i][child.i] = 1 * weight
matrix[child.i][token.i] = 1 * weight
#print(matrix)
#print('='*30)
return matrix
def process(filename, seedname):
seed_words = load_seed_word(seedname)
fin = open(filename, 'r', encoding='utf-8', newline='\n', errors='ignore')
lines = fin.readlines()
fin.close()
idx2graph = {}
fout = open(filename+'.graph.cross.stance.3way11t', 'wb')
graph_idx = 0
for i in range(0, len(lines), 3):
text = lines[i].lower().strip()
target = lines[i+1].lower().strip()
adj_matrix = dependency_adj_matrix(text, seed_words)
idx2graph[i] = adj_matrix
pickle.dump(idx2graph, fout)
print('done !!!'+filename)
fout.close()
if __name__ == '__main__':
# process('./raw_data/fm.raw', './seed_words/seed_word.domain.FMLA.stance.3way11.tf')
# process('./raw_data/la.raw', './seed_words/seed_word.domain.FMLA.stance.3way11.tf')
# process('./raw_data/dt.raw', './seed_words/seed_word.domain.DTHC.stance.3way11.tf')
# process('./raw_data/hc.raw', './seed_words/seed_word.domain.DTHC.stance.3way11.tf')
process('./raw_data/tp.raw', './seed_words/seed_word.domain.HCTP.stance.3way11.tf')
process('./raw_data/hc.raw', './seed_words/seed_word.domain.HCTP.stance.3way11.tf')