forked from HebrewTools/Reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minitf.py
243 lines (203 loc) · 6.76 KB
/
minitf.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from functools import reduce
from tf.core.api import NodeFeature, EdgeFeature
class MiniApi(object):
def __init__(
self,
nodes=None,
features={},
featureType={},
locality={},
text={},
langs=set()):
self.nodes = () if nodes is None else tuple(nodes)
self.F = NodeFeatures()
self.E = EdgeFeatures()
rank = {n: i for (i, n) in enumerate(self.nodes)}
self.rank = rank
self.sortKey = lambda n: rank[n]
for f in features:
fType = featureType[f]
if fType:
fObj = EdgeFeature(self, None, features[f], fType == 1)
setattr(self.E, f, fObj)
else:
fObj = NodeFeature(self, None, features[f])
setattr(self.F, f, fObj)
self.L = Locality(self, locality)
self.T = Text(self, text, langs)
def Fs(self, fName):
return getattr(self.F, fName, None)
def Es(self, fName):
return getattr(self.E, fName, None)
def Fall(self):
return sorted(x[0] for x in self.F.__dict__.items())
def Eall(self):
return sorted(x[0] for x in self.E.__dict__.items())
def N(self):
for n in self.nodes:
yield n
def sortNodes(self, nodeSet):
return sorted(nodeSet, key=self.sortKey)
class NodeFeatures(object):
pass
class EdgeFeatures(object):
pass
class Locality(object):
def __init__(self, api, data):
self.api = api
self.data = data
for member in ('u', 'd', 'n', 'p'):
_makeLmember(self, member)
class Text(object):
def __init__(self, api, langs, text):
self.api = api
self.langs = langs
self.formats = set(text)
self.data = text
def text(self, slots, fmt=None):
if fmt is None:
fmt = DEFAULT_FORMAT
thisText = self.data.get(fmt, None)
if thisText is None:
return ' '.join(str(s) for s in slots)
return ''.join(thisText.get(s, '?') for s in slots)
def _makeLmember(dest, member):
def memberFunction(n, otype=None):
data = dest.data
if n not in data.get(member, {}):
return ()
ms = data[member][n]
if otype is None:
return ms
api = dest.api
F = api.F
return tuple(m for m in ms if F.otype.v(m) == otype)
setattr(dest, member, memberFunction)
def gather_context(api, context, results):
F = api.F
Fs = api.Fs
Es = api.Es
L = api.L
T = api.T
TF = api.TF
sortNodes = api.sortNodes
# quit quickly if no context is required
if not context or not results:
return {}
# parse the context requirements
if context is True:
langs = True
featureSpec = True
doLocality = True
textFormats = True
else:
langs = context.get('languages', set())
featureSpec = context.get('features', set())
doLocality = context.get('locality', False)
textFormats = context.get('formats', set())
if type(langs) is str:
langs = set(langs.strip().split())
elif langs is True:
langs = set(T.languages)
if type(featureSpec) is str:
featureSpec = set(featureSpec.strip().split())
elif featureSpec is True:
featureSpec = {f[0] for f in TF.features.items() if not (f[1].isConfig or f[1].method)}
else:
featureSpec = {fName for fName in featureSpec}
testLangs = langs | {None}
featureSpec = {fName for fName in featureSpec if _depLang(fName) in testLangs}
if type(textFormats) is str:
textFormats = set(textFormats.strip().split())
elif textFormats is True:
textFormats = T.formats
# generate context: features
loadedFeatures = api.ensureLoaded(featureSpec)
allNodes = reduce(
set.union,
(set(r) for r in results),
set(),
)
features = {}
featureType = {}
for f in sorted(loadedFeatures):
fObj = TF.features[f]
isEdge = fObj.isEdge
isNode = not (isEdge or fObj.isConfig or fObj.method)
if isNode:
featureType[f] = 0
data = {}
for n in allNodes:
val = Fs(f).v(n)
if val is not None:
data[n] = val
features[f] = data
elif isEdge:
if f == 'oslots':
featureType[f] = -1
data = {}
for n in allNodes:
vals = tuple(m for m in Es(f).s(n) if m in allNodes)
if vals:
data[n] = vals
features[f] = data
else:
hasValues = TF.features[f].edgeValues
featureType[f] = 1 if hasValues else -1
dataF = {}
dataT = {}
if hasValues:
for n in allNodes:
valsF = tuple(x for x in Es(f).f(n) if x[0] in allNodes)
valsT = tuple(x for x in Es(f).t(n) if x[0] in allNodes)
if valsF:
dataF[n] = valsF
if valsT:
dataT[n] = valsT
else:
for n in allNodes:
valsF = tuple(m for m in Es(f).f(n) if m in allNodes)
valsT = tuple(m for m in Es(f).t(n) if m in allNodes)
if valsF:
dataF[n] = valsF
if valsT:
dataT[n] = valsT
features[f] = (dataF, dataT)
# generate context: locality
locality = {}
if doLocality:
lu = {}
ld = {}
ln = {}
lp = {}
for n in allNodes:
lu[n] = tuple(m for m in L.u(n) if m in allNodes)
ld[n] = tuple(m for m in L.d(n) if m in allNodes)
ln[n] = tuple(m for m in L.n(n) if m in allNodes)
lp[n] = tuple(m for m in L.p(n) if m in allNodes)
locality['u'] = lu
locality['d'] = ld
locality['n'] = ln
locality['p'] = lp
# generate context: formats
slotType = F.otype.slotType
text = {}
slots = sorted(n for n in allNodes if F.otype.v(n) == slotType)
for fmt in textFormats:
data = {}
for n in slots:
data[n] = T.text([n], fmt=fmt)
text[fmt] = data
return dict(
nodes=','.join(str(n) for n in sortNodes(allNodes)),
features=features,
featureType=featureType,
locality=locality,
text=text,
langs=langs,
)
def _depLang(feature):
if '@' not in feature:
return None
else:
return feature.rsplit('@', 1)[1]