Watson服务--对话(conversation)
简介
对话服务(Conversation service)集成了机器学习,自然语言分析和对话工具用以在你的app和用户之间建立对话。
认证
在使用服务实例前,你需要提供用户名和密码来获得权限。创建一个服务实例后,选择服务认证来查看你的用户名和密码。更多信息参见https://www.ibm.com/watson/de...
应用程序也可以通过创建令牌的方式来取得认证,这样就不用每次都获得凭证。在bluemix上写一个用户代理来为你的客户端获得令牌,这样就可以直接调用服务。更多信息参见https://www.ibm.com/watson/de...
同时,注意版本信息。当前最新版本是2017-02-03.
from watson_developer_cloud import ConversationV1
conversation = ConversationV1(
username='{username}',
password='{password}',
version='2017-02-03'
)
方法
工作区列表(List workspaces)
列举出和服务实例相关联的工作区
创建工作区(Create workspaces)
基于JSON输入创建一个工作区,你必须提供JSON格式的数据来定义新的工作区内容
删除工作区(Delete workspaces)
从服务实例中删除一个工作区
获取工作区信息(Get information about a workspace)
得到关于一个工作区的全部信息,包括名称,描述,语言,创建和更新日期,工作区编号。如果导出参数为真,数据将以JSON格式返回工作区所有内容
更新工作区(Update workspaces)
更新一个已存在工作区,同样的,你也必须提供JSON格式的数据。注意,旧的内容将被销毁,并且更新意味着完全替代。
发送信息(Send message)
为用户输入提供响应
message(workspace_id, message_input=None, context=None, entities=None, intents=None, output=None, alternate_intents=False)
import json
from watson_developer_cloud import ConversationV1
conversation = ConversationV1(
username='{username}',
password='{password}',
version='2017-02-03'
)
# Replace with the context obtained from the initial request
context = {}
workspace_id = '25dfa8a0-0263-471b-8980-317e68c30488'
response = conversation.message(
workspace_id=workspace_id,
message_input={'text': 'Turn on the lights'},
context=context
)
print(json.dumps(response, indent=2))
返回结果
{
"input": {
"text": "Turn on the lights"
},
"context": {
"conversation_id": "f1ab5f76-f41b-47b4-a8dc-e1c32b925b79",
"system": {
"dialog_stack": [
{
"dialog_node": "root"
}
],
"dialog_turn_counter": 2,
"dialog_request_counter": 2
},
"defaultCounter": 0
},
"entities": [
{
"entity": "appliance",
"location": [12, 18],
"value": "light"
}
],
"intents": [
{
"intent": "turn_on",
"confidence": 0.99
}
],
"output": {
"log_messages": [],
"text": [
"Ok. Turning on the light."//这里就是返回的信息,很智能吧
],
"nodes_visited": [
"node_1_1467221909631",
"node_2_1467232480480"
]
}
}
原文地址:https://www.ibm.com/watson/de...
Watson服务--文档转换(Document conversion)
文档转换服务用以把一个单独的HTML,PDF,或者微软文档格式的文件转化成一个简单的HTML或是纯文本,或者是一系列JSON格式的响应单元,以便用于其他的Watson服务。注意,仔细检查结果以确保它包含了您或您组织安全标准要求的所有的元素和数据
认证
同之前文章中提到的方法一样,创建服务实例获得用户名和密码。
当前最新版本是2015-12-15
import json
from watson_developer_cloud import DocumentConversionV1
document_conversion = DocumentConversionV1(
username='{username}',
password='{password}',
version='2015-12-15'
)
方法
转换文本(Convert a document)
将一个文本(doc)转化成响应单元,HTML或者文本(text)
convert_document(document, config, media_type=None)
import json
from watson_developer_cloud import DocumentConversionV1
document_conversion = DocumentConversionV1(
username='{username}',
password='{password}',
version='2015-12-15'
)
config = {
'conversion_target': 'ANSWER_UNITS',//设置转换的目标格式
# Use a custom configuration.
'word': {
'heading': {
'fonts': [
{'level': 1, 'min_size': 24},
{'level': 2, 'min_size': 16, 'max_size': 24}
]
}
}
}
with open(('sample-docx.docx'), 'r') as document:
response = document_conversion.convert_document(document=document, config=config)
print(json.dumps(response, indent=2))
返回结果会依据目标格式的不同而不同
normalized_text会转换成纯文本
normalized_html会转换成html
answer_units会转换成JSON格式的数据
例子的返回结果:
{
"source_document_id": "",
"timestamp": "2015-10-12T20:16:15.535Z",
"media_type_detected": "application/pdf",
"metadata": [{
"name": "publicationdate",
"content": "2015-07-18"
}],
"answer_units": [{
"id": "de93c979-414b-4967-afd5-21eafeaedf69",
"type": "regular",
"title": "Title from your document 1",
"content": [{
"media_type": "text/plain",
"text": "Text from your document 2"
}]
}, {
"id": "f3702667-9133-4e9d-a639-fbfc70822b9c",
"type": "regular",
"title": "Title from your document 3",
"content" :[{
"media_type": "text/plain",
"text": ""
}]
}],
"warnings": []
}
编号文档(Index a document)
为检索及排序服务准备一个文档是增强信息检索的一部分,之后,把内容添加到你的Solr序号中,你就可以检索它,更多细节参见https://www.ibm.com/watson/de...
index_document(config, document=None, metadata=None, media_type=None)
import json
from watson_developer_cloud import DocumentConversionV1
document_conversion = DocumentConversionV1(
username='{username}',
password='{password}',
version='2015-12-15'
)
config = {
'convert_document': {
'normalized_html': {
'exclude_tags_completely': ['script', 'sup']
}
},
'retrieve_and_rank': {
'dry_run': 'false',
'service_instance_id': '692b4b66-bd13-42e6-9cf3-f7e77f8200e5',
'cluster_id': 'sc1ca23733_faa8_49ce_b3b6_dc3e193264c6',
'search_collection': 'example_collection',
'fields': {
'mappings': [{
'from': 'Author',
'to': 'Created By'
}, {
'from': 'Date Created',
'to': 'Created On'
}, {
'from': 'Continent',
'to': 'Region'
}],
'include': ['Created By', 'Created On'],
'exclude': ['Region']
}
}
}
metadata = {
'metadata': [
{'name': 'Creator', 'value': 'Some person'},
{'name': 'Subject', 'value': 'Application programming interfaces'}
]
}
with open(('sample-docx.docx'), 'r') as document:
response = document_conversion.index_document(config=config, document=document, metadata=metadata)
print(json.dumps(response, indent=2))
返回结果:
{
"converted_document": {
"media_type_detected": "text/html",
"metadata": [{
"name": "publicationdate",
"content": "2015-07-18"
}],
"answer_units": [{
"id": "de93c979-414b-4967-afd5-21eafeaedf69",
"type": "body",
"title": "no-title",
"direction": "ltr",
"content": [{
"media_type": "text/html",
"text": "<h3><p>What is Watson?</p></h3><p>Watson is an artificially intelligent computer system capable of answering questions</p>"
},
{
"media_type": "text/plain",
"text": "What is Watson? Watson is an artificially intelligent computer system capable of answering questions"
}]
}],
"warnings": []
},
"solr_document": {
"Created By": "Some person",
"Subject": "Application programming interfaces",
"body": "What is Watson? Watson is an artificially intelligent computer system capable of answering questions",
"contentHtml": "<h3><p>What is Watson?</p></h3><p>Watson is an artificially intelligent computer system capable of answering questions</p>",
"publicationdate": "2015-12-04",
"title": "no-title"
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。