-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_focus.py
168 lines (115 loc) · 5.05 KB
/
match_focus.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
159
160
161
162
163
164
165
166
167
168
import sys
import codecs
import string
import unicodedata
from translate import load_questions
class TranslatedQuestion(object):
def __init__(self, qid, text, alignment):
self.qid = qid
self.text = text
self.alignment = alignment
def load_translations(translations_file):
translations = []
with codecs.open(translations_file, "r", "utf-8") as f:
for line_n, line in enumerate(f):
try:
qid, text, focus = line.strip().split("\t")
translation = TranslatedQuestion(qid, text, focus)
translations.append(translation)
except Exception:
pass
return translations
def main(argv=sys.argv):
#questions_file = "path/to/file"
questions_file = "question-focus-classifier-on-trec.txt"
translations_file = "question-focus-tr.txt"
output = "question-focus-classifier-on-trec-ita.txt"
questions = load_questions(questions_file)
print "loaded %d questions" % (len(questions))
translations = load_translations(translations_file)
print "loaded %d translations" % (len(translations))
qids = [translation.qid for translation in translations]
questions = [question for question in questions if question.qid in qids]
question_translations = zip(questions, translations)
print "found %d <question, translation> pairs" % (len(question_translations))
out = codecs.open(output, "w", "utf-8")
matched_focus_n = 0
for question, translation in question_translations:
translated_focus = match_focus(question.text, translation.text, question.focus, translation.alignment)
out.write(translation.qid + "\t" + translation.text + "\t" + translated_focus + "\n")
out.close()
print "unmatched_focus_n: ", len(translations) - matched_focus_n
def overlap(t1, t2):
return set(range(*t1)) & set(range(*t2))
def find_most_likely_match(matches, start, end):
dists = []
for match in matches:
src, target = match.split("-")
#print "dist(" + src + " ->" + str(start) + ":" + str(end) + "):",
src = tuple(map(int, src.split(":")))
dist = abs(src[0] - start) + abs(src[1] - end)
dists.append(dist if overlap((start, end), src) else sys.maxint)
#print "dists:", dists
pos = dists.index(min(dists))
return matches[pos].split("-")[1]
def match_focus(question, translation, focus, alignment):
ix = question.index(focus)
begin, end = ix, ix + len(focus)
#print question
#print translation
#print alignment
#print "original focus: {0}".format(focus)
#print "focus spanning from {0} to {1}: {2}".format(begin, end, question[begin:end])
matches = dict([align.split("-") for align in alignment.split()])
key = str(begin) + ":" + str(end)
#translation = strip_accents_unicode(translation)
try:
lx, rx = map(int, matches[key].split(":"))
except Exception:
lx, rx = map(int, find_most_likely_match(alignment.split(), begin, end).split(":"))
"""Shifting the lx index for taking into account the unicode accents!"""
ilx = lx
lx = 0
i = 0
while lx < ilx:
if not unicodedata.combining(translation[i]):
lx += 1
i += 1
"""Shifitng the rx index for taking into account the unicode accents!"""
irx = rx
rx = 0
i = 0
while rx < irx:
if not unicodedata.combining(translation[i]):
rx += 1
i += 1
tr_focus = translation[lx:rx + 1].strip(string.punctuation)
#print "tr focus spanning from {0} to {1}: {2}".format(lx, rx, tr_focus.strip)
return tr_focus
def strip_accents_unicode(s):
return "".join([c for c in unicodedata.normalize("NFKD", s) if not unicodedata.combining(c)])
def print_alignment(a, b, alignment):
for align in alignment.split():
src, target = align.split("-")
src = tuple(map(int, src.split(":")))
target = tuple(map(int, target.split(":")))
#print "src:", src
#print "target:", target
print a[src[0]:src[1] + 1] + " -> " + b[target[0]:target[1] + 1]
def test():
question = "When did the story of Romeo and Juliet take place?"
focus = "place"
translation = "Quando la storia di Romeo e Giulietta avvenuta?"
alignment = "0:3-0:5 5:7-38:45 9:11-7:8 13:17-10:15 19:20-17:18 22:26-20:24 28:30-26:26 32:37-28:36 39:48-38:45 49:49-46:46"
print_alignment(question, translation, alignment)
print match_focus(question, translation.decode("utf-8"), focus, alignment)
if __name__ == "__main__":
"""
question = "Who sang \"Tennessee Waltz\"?"
focus = "Tennessee"
translation = "Chi ha cantato \"Tennessee Waltz\"?"
alignment = "0:2-0:2 4:7-4:13 9:18-15:24 20:26-26:32"
match_focus(question, translation, focus, alignment)
"""
#main()
pass