-
Notifications
You must be signed in to change notification settings - Fork 1
/
TAC_get_bigram_overlap.py
135 lines (93 loc) · 3.68 KB
/
TAC_get_bigram_overlap.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
import os
import TAC_ILP_baseline
import fio
import json
import numpy as np
import NLTKWrapper
import postProcess
def get_bigram_overlap(document, summary_file):
sentences = [line.strip() for line in fio.ReadFile(document)]
summaryList = [line.strip() for line in fio.ReadFile(summary_file)]
bigram_ta = 0.
covered_bigram_ta = 0.
for summary in summaryList:
bigrams = NLTKWrapper.getNgram(summary, 2)
bigram_ta += len(bigrams)
if bigram_ta == 0:
print summary_file
for token in bigrams:
if postProcess.CheckKeyword(token, summaryList):
covered_bigram_ta += 1
if bigram_ta == 0:
return 0.0
return covered_bigram_ta/bigram_ta
def get_bigram_overlap_doc(prefix):
document = prefix + '.key'
rs = []
summries = []
for porfix in ['ref.0', 'ref.1']:#['.ref1.summary', '.ref2.summary', '.ref3.summary', '.ref4.summary']:
summary_file = prefix[:-len('sentence')] + porfix
r = get_bigram_overlap(document, summary_file)
rs.append(r)
return np.average(rs)
def get_overlap():
ilpdir = "../../data/TAC_ILP_Sentence/"
data = {}
for year in [
's08',
's09',
's10',
's11'
]:
path = os.path.join(ilpdir, year)
print path
for subdir, file in TAC_ILP_baseline.iter_folder(path, '.key'):
doc_id = file[:-4]
prefix = os.path.join(path, doc_id)
print prefix
print doc_id
overlap = get_bigram_overlap_doc(prefix)
data[doc_id] = overlap
fio.SaveDict2Json(data, '../../data/TAC/bigram_overlap.json')
def get_overlap_IE256():
ilpdir = "../../data/IE256/ILP_Baseline_Sentence/"
data = {}
types = ['q1', 'q2']
sheets = range(0,26)
overlaps = []
for i, sheet in enumerate(sheets):
week = i + 1
dir = ilpdir + str(week) + '/'
for type in types:
prefix = dir + type + "." + 'sentence'
print prefix
if not fio.IsExist(prefix+'.key'):continue
summary_file = prefix[:-len('sentence')] + 'ref.1'
print summary_file
if not fio.IsExist(summary_file): continue
overlap = get_bigram_overlap_doc(prefix)
data[type + '_' + str(week)] = overlap
overlaps.append(overlap)
print np.mean(overlaps)
fio.SaveDict2Json(data, '../../data/IE256/bigram_overlap.json')
def rank_rouge_by_overlap():
bigram_overlap_json = '../../data/TAC/bigram_overlap.json'
rouge_ILP_json = "../../data/TAC_ILP_Sentence/rouge.json"
rouge_MC_json = "../../data/TAC_ILP_Sentence_MC/rouge.json"
bigram_overlap = fio.LoadDictJson(bigram_overlap_json)
rouge_ILP = fio.LoadDictJson(rouge_ILP_json)
rouge_MC = fio.LoadDictJson(rouge_MC_json)
doc_ids = sorted(bigram_overlap, key=bigram_overlap.get)
RougeHeader = ['R1-R', 'R1-P', 'R1-F', 'R2-R', 'R2-P', 'R2-F', 'RSU4-R', 'RSU4-P', 'RSU4-F',]
head = ['doc_id', 'overlap'] + RougeHeader + RougeHeader
body = []
for doc_id in doc_ids:
row = [doc_id]
row.append(bigram_overlap[doc_id])
row = row + rouge_ILP[doc_id]
row = row + rouge_MC[doc_id]
body.append(row)
fio.WriteMatrix('../../data/TAC/rouge_sort.txt', body, head)
if __name__ == '__main__':
#rank_rouge_by_overlap()
get_overlap_IE256()