-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_datasets.py
155 lines (136 loc) · 5.21 KB
/
get_datasets.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
import spacy
from tqdm import tqdm
import _pickle as pickle
import params
class SummaryDataProcessor():
def __init__(self, topk):
self.nlp = spacy.load("en_core_web_lg")
self.doc = ""
self.summary = ""
self.topk = topk
self.doc_sentences = []
self.summary_sentences = []
self.candidate = []
self.pos2id = dict()
self.dependency2id = dict()
self.word2id = dict()
self.word_count = 0
self.dependency_count = 0
self.pos_count = 0
def update(self, doc, summary):
if "\n" in doc:
self.doc = doc.split("\n")[0]
else:
self.doc = doc
if "\n" in summary:
self.summary = summary.split("\n")[0]
else:
self.summary = summary
self.doc_sentences = self.doc.split(" . ")[:-1]
if " . " in self.summary:
self.summary_sentences = self.summary.split(" . ")[:-1]
else:
self.summary_sentences = [self.summary]
self.candidate = []
def jacsim(self, str1, str2):
list1 = str1.split(" ")
list2 = str2.split(" ")
unionlength = float(len(set(list1) | set(list2)))
interlength = float(len(set(list1) & set(list2)))
return float('%.3f' % (interlength / unionlength))
def make_k_candidate(self):
for summary_sentence in self.summary_sentences:
jacsim_list = [
self.jacsim(sentence, summary_sentence)
for sentence in self.doc_sentences
]
sorted_sentences = [
item[1]
for item in sorted(zip(jacsim_list, self.doc_sentences),
reverse=True)
]
pos_sample = sorted_sentences[0]
step = (len(jacsim_list) - 1) // (self.topk - 1)
try:
temp = sorted_sentences[1::step] # negative sample set
except ValueError:
print(step)
print(len(self.doc_sentences))
print(len(jacsim_list))
print(self.doc_sentences)
print(self.doc)
break
temp = temp[:self.topk - 1] # only topk-1 negative sample
temp.insert(0, pos_sample) # second is pos_sample
temp.insert(0, summary_sentence) # first is summary
self.candidate.append(temp)
break
def build_graph(self, index):
sample = []
for summary_wise in self.candidate:
for sentence in summary_wise:
sample_dict = dict()
doc = self.nlp(sentence)
sample_dict['sentence'] = sentence
sample_dict['index'] = index
edges = []
for token in doc:
if token.text not in self.word2id:
self.word2id[token.text] = self.word_count
self.word_count += 1
if token.pos_ not in self.pos2id:
self.pos2id[token.pos_] = self.pos_count
self.pos_count += 1
if token.dep_ not in self.dependency2id:
self.dependency2id[token.dep_] = self.dependency_count
self.dependency_count += 1
edges.append(
tuple([
token.text, token.i, token.pos_, token.dep_,
token.head.text, token.head.i, token.head.pos_
]))
sample_dict['edges'] = edges
sample.append(sample_dict)
return sample
if __name__ == "__main__":
sdp = SummaryDataProcessor(params.topk)
src = []
tgt = []
max_count = params.train_count
result = []
with open("./data/cnndm/train.src", "r") as f:
for idx, line in enumerate(f):
src.append(line)
if idx == max_count:
break
with open("./data/cnndm/train.tgt", "r") as f:
for idx, line in enumerate(f):
tgt.append(line)
if idx == max_count:
break
save_every = params.train_count // 10
skip_count = 0
for i in tqdm(range(max_count)):
sdp.update(src[i], tgt[i])
if len(sdp.doc_sentences) < params.topk:
skip_count += 1
continue
sdp.make_k_candidate()
graph = sdp.build_graph(i)
if len(graph) == 0:
print(sdp.candidate)
print(sdp.doc_sentences)
print(sdp.summary_sentences)
print([sdp.doc, sdp.summary])
else:
result.append(graph)
if i % save_every == 0:
pickle.dump(result, open("./data/train.bin", "wb"))
pickle.dump(sdp.word2id, open("./data/word2id", "wb"))
pickle.dump(sdp.dependency2id, open("./data/dependency2id", "wb"))
pickle.dump(sdp.pos2id, open("./data/pos2id", "wb"))
pickle.dump(result, open("./data/train.bin", "wb"))
pickle.dump(sdp.word2id, open("./data/word2id", "wb"))
pickle.dump(sdp.dependency2id, open("./data/dependency2id", "wb"))
pickle.dump(sdp.pos2id, open("./data/pos2id", "wb"))
print("skipped %d samples" % skip_count)