-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.py
44 lines (40 loc) · 1.78 KB
/
render.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
import json
import scone.follower
import json
import textwrap
import base64
def analyze(instance_and_predicted):
inst, (lit_states, lit_actions), (prag_states, prag_actions) = instance_and_predicted
print("literal")
scone.follower.compare_prediction(None, inst, lit_states, lit_actions, None)
print("\npragmatic")
scone.follower.compare_prediction(None, inst, prag_states, prag_actions, None)
def render_pragmatic_follower(corpus_name, instance_and_predicted):
inst, (lit_states, lit_actions), (prag_states, prag_actions) = instance_and_predicted
data = []
for name, states in [("gold", inst.states), ("literal", lit_states), ("pragmatic", prag_states)]:
data.append({
'corpus': corpus_name,
'name': inst.id + ": " + name,
'sequence': collapse(states, inst.utterances)
})
return base64.b64encode(json.dumps(data).encode('utf-8')).decode('utf-8')
def render_pragmatic_speaker(corpus_name, instance_and_predicted):
inst, lit_utterances, prag_utterances = instance_and_predicted
data = []
for name, utts in [("gold", inst.utterances), ("literal", lit_utterances), ("pragmatic", prag_utterances)]:
data.append({
'corpus': corpus_name,
'name': inst.id + ": " + name,
'sequence': collapse(inst.states, utts)
})
return base64.b64encode(json.dumps(data).encode('utf-8')).decode('utf-8')
def collapse(states, utterances):
def render_state(state):
return ' '.join("%d:%s" % (i+1, s) for (i, s) in enumerate(state))
collapsed = []
for i in range(len(utterances)):
collapsed.append(render_state(states[i]))
collapsed.append(textwrap.fill(' '.join(utterances[i]), 40))
collapsed.append(render_state(states[-1]))
return collapsed