Last active
June 3, 2017 15:12
-
-
Save krhoyt/e96cf57aa00926b2d930df2870a0ade7 to your computer and use it in GitHub Desktop.
Watson Conversation System Entities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
import sys | |
# Read configuration options | |
with open( '../bluemix/config.json' ) as data_file: | |
config = json.load( data_file ) | |
# URL for Conversation API call | |
url = '{0}{1}{2}{3}{4}'.format( | |
'https://gateway.watsonplatform.net', | |
'/conversation/api/v1/workspaces/', | |
config['workspace'], | |
'/message?', | |
'version=2017-05-26' | |
) | |
# JSON | |
headers = { | |
'Content-Type': 'application/json' | |
} | |
# Optional command line | |
if len( sys.argv ) > 1: | |
text = sys.argv[1] | |
else: | |
text = 'It was three days ago.' | |
# Example phrase | |
data = { | |
'input': { | |
'text': text | |
} | |
} | |
# Call Conversation API | |
req = requests.post( | |
url, | |
auth = ( | |
config['username'], | |
config['password'] | |
), | |
headers = headers, | |
data = json.dumps( data ) | |
) | |
res = req.json() | |
# Print any found system entities (date, location, etc.) | |
# https://www.ibm.com/watson/developercloud/doc/conversation/system-entities.html | |
for entity in res['entities']: | |
parts = entity['entity'].split( '-' ) | |
print '{0}\t{1}\t{2}\t{3}%'.format( | |
parts[1], | |
text[entity['location'][0]:entity['location'][1]], | |
entity['value'], | |
int( entity['confidence'] * 100 ) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment