对话服务(Conversation service)集成了机器学习,天然语言分析和对话工具用以在你的app和用户之间创建对话。html
在使用服务实例前,你须要提供用户名和密码来得到权限。建立一个服务实例后,选择服务认证来查看你的用户名和密码。更多信息参见https://www.ibm.com/watson/de...node
应用程序也能够经过建立令牌的方式来取得认证,这样就不用每次都得到凭证。在bluemix上写一个用户代理来为你的客户端得到令牌,这样就能够直接调用服务。更多信息参见https://www.ibm.com/watson/de...json
同时,注意版本信息。当前最新版本是2017-02-03.api
from watson_developer_cloud import ConversationV1 conversation = ConversationV1( username='{username}', password='{password}', version='2017-02-03' )
工做区列表(List workspaces)安全
列举出和服务实例相关联的工做区app
建立工做区(Create workspaces)机器学习
基于JSON输入建立一个工做区,你必须提供JSON格式的数据来定义新的工做区内容ide
删除工做区(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...
文档转换服务用以把一个单独的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" } }