-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.py
56 lines (39 loc) · 1.46 KB
/
fields.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
'''
Utility functions for working with Instances, when speaker and listener instances
are floating around and it can be hard to keep track of when to use the "input"
and when to use the "output". Each method takes an instance and a flag to
indicate whether the instance is a speaker (input=color, output=utterance) or
listener (input=utterance, output=color) instance.
'''
from numbers import Number
from stanza.research import instance
def get_utt_simple(inst, listener):
return inst.input if listener else inst.output
def get_utt(inst, listener):
return get_multi(get_utt_simple(inst, listener))
def get_color_index(inst, listener):
return inst.output if listener else inst.input
def get_color(inst, listener):
index = get_color_index(inst, listener)
if isinstance(index, Number):
return inst.alt_outputs[index] if listener else inst.alt_inputs[index]
else:
return index
def get_context(inst, listener):
return inst.alt_outputs if listener else inst.alt_inputs
def build_instance(utt, target, context, listener):
if listener:
return instance.Instance(utt, target, alt_outputs=context)
else:
return instance.Instance(target, utt, alt_inputs=context)
def get_speaker_inst(inst, listener):
if listener:
return instance.inverted()
else:
return instance
def get_multi(val):
if isinstance(val, tuple):
assert len(val) == 1
return val[0]
else:
return val