-
Notifications
You must be signed in to change notification settings - Fork 1
/
NLUTest.py
39 lines (34 loc) · 1.48 KB
/
NLUTest.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
import json
import re
from watson_developer_cloud import NaturalLanguageUnderstandingV1
import watson_developer_cloud.natural_language_understanding.features.v1 as \
features
## This command needs the user to put in his IBM Bluemix generated account username and password.
## When placed in the repository, the username will have "YOUR SERVICE USERNAME/PASSWORD" to prevent
## other users from utilizing my Bluemix account. For actual production, use your own username/password.
natural_language_understanding = NaturalLanguageUnderstandingV1(
version='2017-02-27',
username='YOUR SERVICE USERNAME',
password='YOUR SERVICE PASSWORD')
errors = 0
for i, line in enumerate(open('E:\\Documents\\CSCI_6907\\sample_test_set.txt')):
res = re.search('\d(?=\t)', line)
num = res.group(0)
actual_label = "negative"
if (num == "0"):
actual_label = "neutral"
elif (num == "1"):
actual_label = "positive"
elif (num == "2"):
actual_label = "negative"
else:
print("Error~~~")
response = natural_language_understanding.analyze(
text=line, features=[features.Sentiment()])
print(json.dumps(response, indent=2))
generated_label = response['sentiment']['document']['label']
if (generated_label != actual_label):
errors = errors + 1
print("Actual was: ", actual_label, ". Watson returned: ", generated_label)
accuracy = (1 - (errors / (i + 1))) * 100
print("Watson's Accuracy for this test set is: ", accuracy, "%")