-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotation.py
85 lines (62 loc) · 1.74 KB
/
quotation.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
# module quotation.py
#
# Copyright (c) 2015 Rafael Reis
#
"""
quotation module - Main module, that trains and solve the task.
"""
__version__="1.0"
import wis
import numpy as np
def argmax(w, e, loss=0):
tasks = []
i = 0
for r in e.x:
l = 0
if loss > 0 and not find(r, e.y):
l = loss
coref = w * np.array(r.coref.feat)
t = wis.Task(r.quote.start, r.quote.end, np.sum(coref) + l, i)
tasks.append(t)
i += 1
wmax, set_tasks = wis.schedule(tasks)
result = convertTasks(set_tasks, e.x)
return result
def find(r, y):
start = r.quote.start
end = r.quote.end
coref = r.coref.label
for x in y:
if x.quote.start == start and x.quote.end == end and x.coref.label == coref:
return x
return None
def phi(e, y=None):
if not y:
y = e.y
if y:
phi = np.zeros( (len(y[0].coref.feat)), dtype=int )
else:
phi = np.zeros( (len(e.x[0].coref.feat)), dtype=int )
for r in y:
if r.coref.label != "dummy":
phi = phi + r.coref.feat
return phi
def convertTasks(resultTasks, x):
conv = [ x[t.index] for t in resultTasks ]
return conv
class Example:
def __init__(self, x, y):
self.x = x # List of Rows
self.y = y # List of Rows
class Row:
def __init__(self, quote, coref):
self.quote = quote # A Quote
self.coref = coref # A Coref
class Quote:
def __init__(self, start, end):
self.start = start # Start index of Quote
self.end = end # End index of Quote
class Coref:
def __init__(self, label, feat):
self.label = label # Coref label
self.feat = np.array([ int(e) for e in feat]) # Array of binary features