-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrepare_holl.py
158 lines (139 loc) · 6.49 KB
/
Prepare_holl.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
156
157
158
import json
import codecs
import nltk
from Rouge import *
nltk.download('all')
input_file=r'holl\raw_data-20190221T150829Z-001\raw_data\train_data.json'
version='oracle_reduced'
output_file=r'holl\holl-train.oracle.json'
with codecs.open(root+r'\raw_data-20190221T150829Z-001\raw_data\train_data.json', encoding='utf-8') as f:
data = json.load(f)
print(len(data))
new_data=[]
for i in range(len(data)):
new_sample = dict()
sample=data[i]
new_sample['id']=sample['example_id']
new_sample['topic'] = ' '.join(nltk.word_tokenize(sample['movie_name']))
new_sample['query'] = ' '.join(nltk.word_tokenize(sample['query']))
new_sample['response'] = ' '.join(nltk.word_tokenize(sample['response']))
short_his=sample['short_history']
if len(short_his)==1 and short_his[0]=='NH':
short_his= ['<nan>']
else:
short_his = [' '.join(nltk.word_tokenize(res)) for res in sample['short_history']]
new_sample['context'] = short_his
span=' '.join(nltk.word_tokenize(sample['span']))
new_sample['span'] = span
rouge_1_fs=[]
rouge_1_ps=[]
rouge_1_rs=[]
# unstructured_knowledge=[]
plot=sample['all_documents']['plot']
plots=nltk.sent_tokenize(plot)
new_plots=[]
for plot in plots:
plot=' '.join(nltk.word_tokenize(plot))
rouge_1_f, rouge_1_p, rouge_1_r=rouge_n([plot], [span], 1)
rouge_1_fs.append(rouge_1_f)
rouge_1_ps.append(rouge_1_p)
rouge_1_rs.append(rouge_1_r)
new_plots.append('<plot> '+ plot)
new_sample['plot']=new_plots
# unstructured_knowledge+=new_plots
review = sample['all_documents']['review']
reviews = nltk.sent_tokenize(review)
new_reviews = []
for review in reviews:
review = ' '.join(nltk.word_tokenize(review))
rouge_1_f, rouge_1_p, rouge_1_r = rouge_n([review], [span], 1)
rouge_1_fs.append(rouge_1_f)
rouge_1_ps.append(rouge_1_p)
rouge_1_rs.append(rouge_1_r)
new_reviews.append('<review> '+review)
new_sample['review'] = new_reviews
# unstructured_knowledge+=new_reviews
comments= sample['all_documents']['comments']
new_comments = []
for comment in comments:
comment = ' '.join(nltk.word_tokenize(comment))
rouge_1_f, rouge_1_p, rouge_1_r = rouge_n([comment], [span], 1)
rouge_1_fs.append(rouge_1_f)
rouge_1_ps.append(rouge_1_p)
rouge_1_rs.append(rouge_1_r)
new_comments.append('<comment> '+comment)
new_sample['comment'] = new_comments
# unstructured_knowledge+=new_comments
fact_tables = sample['all_documents']['fact_table']
new_fact_tables = []
if 'taglines' in fact_tables:
taglines = fact_tables['taglines']
for tagline in taglines:
tagline = ' '.join(nltk.word_tokenize(tagline))
rouge_1_f, rouge_1_p, rouge_1_r = rouge_n([tagline], [span], 1)
rouge_1_fs.append(rouge_1_f)
rouge_1_ps.append(rouge_1_p)
rouge_1_rs.append(rouge_1_r)
new_fact_tables.append('<tagline> '+tagline)
if 'box_office' in fact_tables:
box_office=' '.join(nltk.word_tokenize(str(fact_tables['box_office'])))
rouge_1_f, rouge_1_p, rouge_1_r = rouge_n([box_office], [span], 1)
rouge_1_fs.append(rouge_1_f)
rouge_1_ps.append(rouge_1_p)
rouge_1_rs.append(rouge_1_r)
new_fact_tables.append('<box_office> '+box_office)
if 'awards' in fact_tables:
award = ' '.join(nltk.word_tokenize(' '.join(fact_tables['awards'])))
rouge_1_f, rouge_1_p, rouge_1_r = rouge_n([award], [span], 1)
rouge_1_fs.append(rouge_1_f)
rouge_1_ps.append(rouge_1_p)
rouge_1_rs.append(rouge_1_r)
new_fact_tables.append('<award> ' + award)
if 'similar_movies' in fact_tables:
similar_movie = ' '.join(nltk.word_tokenize(' '.join(fact_tables['similar_movies'])))
rouge_1_f, rouge_1_p, rouge_1_r = rouge_n([similar_movie], [span], 1)
rouge_1_fs.append(rouge_1_f)
rouge_1_ps.append(rouge_1_p)
rouge_1_rs.append(rouge_1_r)
new_fact_tables.append('<similar_movie> ' + similar_movie)
new_sample['fact_table'] = new_fact_tables
# unstructured_knowledge+=new_fact_tables
# new_sample['unstructured_knowledge']=unstructured_knowledge
new_sample['rouge_1_f'] = rouge_1_fs
new_sample['rouge_1_p'] = rouge_1_ps
new_sample['rouge_1_r'] = rouge_1_rs
background = nltk.word_tokenize(sample[version])#change here to other background length
new_sample['unstructured_knowledge'] = ' '.join(background)
span=span.split(' ')
if len(span) > 0:
for j in range(len(background)):
if ' '.join(background[j:j + len(span)]).lower() == new_sample['span'].lower():
new_sample['bg_ref_start'] = j
new_sample['bg_ref_end'] = j + len(span)
break
for j in range(len(new_sample['response'])):
if ' '.join(new_sample['response'][j:j + len(span)]).lower() == new_sample['span'].lower():
new_sample['res_ref_start'] = j
new_sample['res_ref_end'] = j + len(span)
break
if 'bg_ref_start' not in new_sample:
temp_span = span[:-2]
for j in range(len(background)):
if ' '.join(background[j:j + len(temp_span)]).lower() == ' '.join(temp_span).lower():
new_sample['bg_ref_start'] = j
new_sample['bg_ref_end'] = j + len(temp_span)
break
for j in range(len(new_sample['response'])):
if ' '.join(new_sample['response'][j:j + len(temp_span)]).lower() == ' '.join(temp_span).lower():
new_sample['res_ref_start'] = j
new_sample['res_ref_end'] = j + len(temp_span)
break
else:
print('no ref')
if 'bg_ref_start' not in new_sample:
print(span)
new_data.append(new_sample)
print(len(new_data))
file = codecs.open(output_file, "w", "utf-8")
file.write(json.dumps(new_data))
file.close()