简体   繁体   中英

Sample code of IBM Watson Discovery News

After reading the Watson API reference and playing around the Watson Discovery News Demo, I came out with the code below trying to replicate the function that the IBM Watson Discovery News demo [Sentiment Analysis] can perform. But unfortunately my code can't get the same responses as the demo returns.

import json
from watson_developer_cloud import DiscoveryV1

# --- Authentication ----
discovery = DiscoveryV1(
    username="581xxxx-5c55-xxxx-983f-027xxxxxxxxx",
    password="xxxxxxxxxx",
    version="2017-11-07"
)

qopts = {"query": "\"tesla\"",
  "filter": "language:(english|en),crawl_date>2017-10-14T12:00:00-
  0700,crawl_date<2017-12-14T12:00:00-0800",
  "aggregations": [
      "term(host).term(enriched_text.sentiment.document.label)",
      "term(enriched_text.sentiment.document.label)"
  ]
}

my_query = discovery.query('system', 'news-en', qopts)
print(json.dumps(my_query, indent=2))

Can someone kindly help me by showing a sample python code that can perform the query as the IBM demo does? For example, a simple code that can return the same responses as the demo [Sentiment Analysis] given the query "Tesla" ? Thanks in advance.

I think you need to change your qopts to look like this:

qopts = {"query": "\"tesla\"",
  "filter": "language:(english|en),crawl_date>2017-10-14T12:00:00-
  0700,crawl_date<2017-12-14T12:00:00-0800",
  "aggregations": "[term(host).term(enriched_text.sentiment.document.label),term(enriched_text.sentiment.document.label)]"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM