IBM Watson Alchemy新闻iOS SDK Swift

时间:2017-02-10 13:49:35

标签: swift3 ibm-cloud ibm-watson alchemyapi

使用Bluemix上的Alchemy News服务的IBM Watson iOS SDK返回一个字符串结果,需要解析以提取url和clean title等字段。参考:https://github.com/watson-developer-cloud/swift-sdk

我将字符串拉入数组并使用一些字符串方法在swift3中解析它,但这很普通,会产生不可预测的结果

是否有更优雅的方法可以访问特定字段,例如我传递给UITableViewCell的url和clean标题,以便选择并转到url链接。

sample code:
let alchemyDataNews = AlchemyDataNews(apiKey: apiKey)
let failure = { (error: Error) in print(error) }
let start = "now-14d" // 7 day ago
let end = "now" // today
let query = ["count": "15",
                            "dedup": "true",
                            "q.enriched.url.title": "[IBM]",
                            "return":  "enriched.url.url,enriched.url.title" "enriched.url.title,enriched.url.entities.entity.text,enriched.url.entities.entity.type"]

此外,我注意到搜索字符串[IBM]的前缀为0,即0 [IBM],并且还看到了" A"。这些前缀是什么意思,它们在哪里记录

1 个答案:

答案 0 :(得分:2)

以下是从返回的有效内容访问字段的一种方法。

    alchemyDataNews.getNews(from: "now-4d", to: "now", query: queryDict, failure: failWithError) { news in

        for doc in (news.result?.docs)! {
            var cleanedTitle = doc.source?.enriched?.url?.cleanedTitle
            var author = doc.source?.enriched?.url?.author
            var title = doc.source?.enriched?.url?.title
        }}

此外,这里有一个很好的API参考链接,用于炼金术数据,其中包含所有请求参数和过滤器。

https://www.ibm.com/watson/developercloud/alchemydata-news/api/v1/