0

我正在使用 IBM 的 Natural Language Understanding API 在输入文本中返回有关标记的信息。

这是我onActivityResult用来提出语法功能请求的代码:

authenticator = IamAuthenticator("my_api_key")
naturalLanguageUnderstanding = NaturalLanguageUnderstanding("2020-08-05", authenticator)
naturalLanguageUnderstanding.serviceUrl = "my_url"
syntax = SyntaxOptions.Builder().sentences(true).build()
features = Features.Builder().syntax(syntax).build()
parameters = AnalyzeOptions.Builder().text(mainIngr[0]).features(features).build()
posResponse = naturalLanguageUnderstanding.analyze(parameters).execute().result
for (token: TokenResult in posResponse.syntax.tokens) {
    if (token.partOfSpeech == "NOUN") {
        print(token.text + ": " + token.partOfSpeech + ", ")
    }
}

对于上述变量,我的活动顶部也有此代码:

private lateinit var authenticator: IamAuthenticator
private lateinit var naturalLanguageUnderstanding: NaturalLanguageUnderstanding
private lateinit var syntax: SyntaxOptions
private lateinit var features: Features
private lateinit var parameters: AnalyzeOptions
private lateinit var posResponse: AnalysisResults

当我运行我的程序时,它在这一行崩溃: posResponse = naturalLanguageUnderstanding.analyze(parameters).execute().result

这是我得到的错误: android.os.NetworkOnMainThreadException

我该如何解决?我需要用作输入的文本位于我的onActivityResult.

4

0 回答 0