-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsession2json.py
More file actions
executable file
·165 lines (152 loc) · 5.13 KB
/
Copy pathsession2json.py
File metadata and controls
executable file
·165 lines (152 loc) · 5.13 KB
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
#!/usr/bin/env python3
import json
#import os
import pickle
import sys
# add emo20q to path
path = "./emo20q"
if path not in sys.path:
sys.path.insert(0, path)
import emo20q
#from emo20q.gpdaquestioner import QuestionerAgent
from emo20q.qaagent import QAAgent
# it would be nice to just pickle the whole agent, but because of
# lambdas we need to reconstruct the agent from episodic buffer (short
# term memory) and lexical access (word knowledge)
from emo20q.gpdaquestioner import LexicalAccess
from emo20q.gpdaquestioner import SemanticKnowledge
from emo20q.gpdaquestioner import EpisodicBuffer
#from emo20q.qa import answer_emotion_question
from emo20q.episodicbufferqa import AgentAskingTurn
from emo20q.episodicbufferqa import IllocutionaryAct
def get_agent_from_session(sesh):
#with open(os.path.join(session_dir, sesh), "rb") as f:
with open(sesh, "rb") as f:
episodic_buffer, state_name, belief = pickle.load(f)
agent = QAAgent(episodicBuffer=episodic_buffer,
lexicalAccess=LexicalAccess(),
semanticKnowledge=SemanticKnowledge())
return agent
def print_turns(agent):
for t in agent.episodicBuffer:
if isinstance(t, AgentAskingTurn):
print(t) #print(t.q, t.a)
elif isinstance(t, IllocutionaryAct):
print(t)
else:
print(t) #(t.gloss, t.semantics, t.talker, t.text)
print()
def extract_json(agent):
""" make the json something like this:
{
"type": "Dialog",
"params": {
"emo20q-commit": "xyz",
"emo20q-web-commit": "xyz",
"timestamp": "date-time",
"sessionfilename": "",
}
"container": [
{ "type": "Utterance",
"text": "agent enters",
"gloss": null,
"is_agent": true},
{ "type": "Utterance",
"text": "ready",
"gloss": null,
"is_agent": false},
{ "type": "AgentAskingTurn",
"container": [
{ "type": "Utterance",
"text": "is it ...?",
"gloss": "e=...",
"is_agent": true}
{ "type": "Utterance",
"text": "yes",
"gloss": null,
"is_agent": false},
]
},
{"type": "Utterance",
'text': 'Dammit, that is disappointing... \nWell, what was the emotion that you picked?',
'gloss': 'fail,ask-emotion',
'is_agent': True},
{ "type" "Utterance",
'text': 'irritation',
'gloss': None,
'is_agent': False},
# ...
# note, the new user asking dialog is not as well structured into "turns"
{ "type": Utterance,
'text': "Now let's switch roles. I'll pick the emotion and you ask the questions.",
'gloss': None,
'is_agent': True},
{ "type": "Utterance",
"text": "ok",
"gloss": null,
"is_agent": false},
{ "type": "Utterance",
"text": "ok, I'm ready for questions",
"gloss": null,
"is_agent": true},
{ "type": "Utterance",
"text": "is it a positive emotion?",
"gloss": "unclassified-question",,
"is_agent": false},
{ "type": "Utterance",
"text": "yes",
"gloss": "reply-to-question",
"is_agent": true},
#...
]
}
"""
outdict = {}
outdict["container"] = []
for t in agent.episodicBuffer:
if isinstance(t, AgentAskingTurn):
outdict["container"].append({"type": "Question",
"text": t.q.text,
"gloss": t.q.gloss,
"is_agent": t.q.is_agent})
outdict["container"].append({"type": "Answer",
"text": t.q.text,
"gloss": t.q.gloss,
"is_agent": t.q.is_agent})
elif isinstance(t, IllocutionaryAct):
outdict["container"].append({"type": "IllocutionaryAct",
"params": t.args,
"is_agent": True})
else:
outdict["container"].append({"type": "Utterance",
"text": t.text,
"gloss": t.gloss,
"is_agent": t.is_agent})
return outdict
if sys.argv[0].startswith("--"):
if sys.argv[0].endswith("is-empty"):
sys.argv.pop(0)
for arg in sys.argv:
agent = get_agent_from_session(arg)
#print_turns(agent)
obj = extract_json(agent)
is_agent = list(map(lambda x: x.get("is_agent", True), obj['container']))
empty = True
for x in is_agent:
if not x:
empty = False
if all(list(is_agent)):
print(arg)
# if len(is_agent) > 20:
# import pdb
# pdb.set_trace()
exit()
for arg in sys.argv:
agent = get_agent_from_session(arg)
#print_turns(agent)
obj = extract_json(agent)
#is_agent = map(lambda x: x.get("is_agent", True), obj['container'])
#print(list(is_agent))
#print(obj['container'])
print(json.dumps(obj, indent=" "))
print()