Watson使用指南(二)

Watson服務--對話(conversation)

簡介

對話服務(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...

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"
  }
}

原文地址:https://www.ibm.com/watson/de...

相關文章
相關標籤/搜索