-
Notifications
You must be signed in to change notification settings - Fork 0
/
smalltalk.py
73 lines (51 loc) · 1.75 KB
/
smalltalk.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
import csv
from collections import defaultdict
from lib.dialogflow_api import api_call, simple_intent, parse_intent, POST, PUT
def load_all_existing_intents():
resp = api_call('intents')
return {
i['name']: i['id']
for i in resp
}
def load_existing_intent(id):
return api_call('intents', id=id)
def load_csv():
filename = 'Dialogflow_intents - Smalltalk_sportsfreund.csv'
intents = defaultdict(lambda: defaultdict(list))
with open(filename, 'r') as f:
reader = csv.reader(f)
next(reader) # Skip first line
for row in reader:
intent = row[1]
question = row[2]
answer = row[3]
if question:
intents[intent]['questions'].append(question)
if answer:
intents[intent]['answers'].append(answer)
return intents
def main():
intent_ids = load_all_existing_intents()
new_intents = load_csv()
for name, data in new_intents.items():
questions = data['questions']
answers = data['answers']
if name in intent_ids:
intent = load_existing_intent(intent_ids[name])
orig_name, orig_questions, orig_answers = parse_intent(intent)
all_questions = list(set(questions) | set(orig_questions))
all_answers = list(set(answers) | set(orig_answers))
api_call(
'intents',
id=intent_ids[name],
data=simple_intent(name, all_questions, all_answers),
method=PUT
)
else:
api_call(
'intents',
data=simple_intent(name, questions, answers),
method=POST
)
if __name__ == '__main__':
main()