-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendallmessages.py
More file actions
133 lines (103 loc) · 4.71 KB
/
sendallmessages.py
File metadata and controls
133 lines (103 loc) · 4.71 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# For manipulating json response data
import json
# For sending requests to Watson API
import requests
# For accessing Watson API
import watson_developer_cloud
# For creating new files and directories
import os
# For adding small delays
import time
# For accessing the config files (so to avoid having credentials in the code)
import configparser
# Access the customLogger class
from logger import Logger
# For accessing Watson API exception library
from watson_developer_cloud import WatsonApiException
# Access custom ChangeMessageString class
from changemessage import ChangeMessageString
# Access custom GetMessagesForAllIntents class
from getallmessages import GetMessagesForAllIntents #
config = configparser.ConfigParser()
config.read('config.conf')
# Send messages, using the message files stored "input" directory
class SendMessages:
def __init__(self, changemessage):
self.changemessage = changemessage
def sendMessages(self):
username = config['DEFAULT']['watson_username']
password = config['DEFAULT']['watson_password']
version = config['DEFAULT']['watson_version']
url = config['DEFAULT']['watson_url']
space_id = os.environ.get('SPACE_ID')
directory = './input/' + space_id + '/'
time.sleep(5)
if space_id == None:
exit("Missing environment variable: SPACE_ID")
if not os.path.exists('logs/' + space_id):
os.makedirs('logs/' + space_id)
if not os.path.exists('report/' + space_id):
os.makedirs('report/' + space_id)
# Loop round each filename in directory
for filename in os.listdir(directory):
intent = os.path.splitext(filename)[0]
f = open('report/' + space_id + '/' + self.changemessage + '-' + intent
+ '.json', 'a+')
flog = open('logs/' + space_id + '/' + self.changemessage + '-' + intent
+ '.log', 'a+')
fsummary = open('report/' + space_id + '/' + self.changemessage
+ 'summary-results.txt', 'a+')
# Open file and read line by line
with open(directory + filename) as fp:
cnt = 0
totalpass = 0
# Set watson developer cloud credentials
assistant = \
watson_developer_cloud.AssistantV1(username=username,
password=password, version=version, url=url)
# Execute send message, using each line as message to send
for line in fp:
time.sleep(0.5)
line = \
ChangeMessageString.changeMessage(line,
self.changemessage)
# Set response from Watson API to be detailed
assistant.set_detailed_response(True)
# Send message to the specified workspace
response = \
assistant.message(workspace_id=space_id
, page_limit=1000,
input={'text': line.strip()}).get_result()
resp = json.dumps(response).count('intent": "'
+ intent + '"')
# Return pass/failresult for matching intent, and add response to report
if resp == 1:
Logger.log_pass(response)
f.write(json.dumps(response) + ',\n')
totalpass += \
json.dumps(response).count('intent": "'
+ intent + '"')
cnt += 1
# Extract successful intent matches that scored confidence lower than 9.8
resp_dict = json.loads(response)
resp_dict['intents']
confidence = resp_dict['confidence']
if confidence < 9.8:
flog.write(json.dumps(response) + ',\n')
else:
Logger.log_fail(response)
f.write(json.dumps(response) + ',\n')
cnt += 1
# Write summaries of passed tests to report summary file
summaryline = space_id + ',' + intent + ',' + str(totalpass) + ',' + str(cnt) + '\n'
fsummary.write(summaryline)
# Close all the open files
f.close()
flog.close()
fsummary.close()
# if __name__== "__main__":
# main()s
GetMessagesForAllIntents.getMessagesForAllIntents()
SendMessages('baseline').sendMessages()