-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwitai.py
80 lines (65 loc) · 2.95 KB
/
witai.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
from wit import Wit
import datetime
import logging
import requests
import os
from functools import cmp_to_key
def cmp(a, b):
if (a['confidence'] == b['confidence']): return 0
elif (a['confidence'] < b['confidence']): return 1
else: return -1
class WitBot:
def __init__(self):
self.access_token = os.environ['WIT_AI_ACCESS_TOKEN']
self.client = Wit(self.access_token)
self.client.logger.info("WitBot initiated")
def query(self, msg):
resp = self.client.message(msg)
cmp_key = cmp_to_key(cmp)
resp['intents'].sort(key=cmp_key)
self.client.logger.info("WitBot - querying {} response: ".format(msg))
self.client.logger.info(str(resp['intents']))
if (len(resp['intents']) == 0):
return (False, "Unable to understand message. Type $help for guidance.")
else:
if (resp['intents'][0]['confidence'] > 0.8):
return (True, (resp['intents'][0]['name'], resp['intents'][0]['confidence']))
else:
if (len(resp['intents']) > 1):
return (False, "No recipient indicated.\nDo you mean to send to *@{}* (_{} sure_) or {} ({} sure)?".format(resp['intents'][0]['name'], str(int(100.0*resp['intents'][0]['confidence'])) + "%", resp['intents'][1]['name'], str(int(100.0*resp['intents'][1]['confidence'])) + "%"))
else:
return (False, "No recipient indicated.\nDo you mean to send to *@{}* (_{} sure_)?".format(resp['intents'][0]['name'], str(int(100.0*resp['intents'][0]['confidence'])) + "%"))
def _query(self, msg):
resp = self.client.message(msg)
cmp_key = cmp_to_key(cmp)
resp['intents'].sort(key=cmp_key)
return resp['intents']
def create_new_intent(self, intent_name):
headers = {
"Authorization": "Bearer {}".format(self.access_token),
"Content-Type": "application/json"
}
data = {
"name": intent_name
}
dt = datetime.datetime.now().strftime("%Y%m%d")
self.client.logger.info("WitBot - creating new intent {}".format(intent_name))
x = requests.post('https://api.wit.ai/intents?v={}'.format(dt), json=data, headers=headers)
self.client.logger.info("WitBot - SUCCESS")
return
def train_intent(self, intent_name, text):
headers = {
"Authorization": "Bearer {}".format(self.access_token),
"Content-Type": "application/json"
}
data = [{
"text": text,
"intent": intent_name,
"entities": [],
"traits": []
}]
dt = datetime.datetime.now().strftime("%Y%m%d")
self.client.logger.info("WitBot - training intent {} with text {}".format(intent_name, text))
x = requests.post('https://api.wit.ai/utterances?v={}'.format(dt), json=data, headers=headers)
self.client.logger.info("WitBot - SUCCESS")
return