1 2 3 4 5 6 7 8 9 | // API 版本,参考 API 手册 String version = "2018-03-19"; // 密钥,此处代码隐藏了实际的密钥值,应该填写你开通这个服务后分配的密钥 String api_key = "…………………………………" ; // API 调用地址,系统分配 String end_point = "https://gateway-syd.watsonplatform.net/natural-language-understanding/api"; IamOptions options = new IamOptions.Builder().apiKey(api_key).build(); |
1 2 | NaturalLanguageUnderstanding service = new NaturalLanguageUnderstanding(version,options); service.setEndPoint(end_point); |
1 2 3 4 5 6 7 8 9 10 11 12 13 | // 设置情绪(Emotion)参数 EmotionOptions emotionOptions = new EmotionOptions.Builder().build(); // 设置情感(Sentiment)参数 SentimentOptions sentimentOptions = new SentimentOptions.Builder().build(); // 设置概念(Concepts)参数 ConceptsOptions conceptsOptions = new ConceptsOptions.Builder().limit(10).build(); // 设置关键字(Keywords)参数 KeywordsOptions keywordsOptions = new KeywordsOptions.Builder() //.emotion(true).sentiment(true) .limit(10).build(); |
1 2 3 4 5 6 | Features features = new Features.Builder() .concepts(conceptsOptions) .emotion(emotionOptions) .keywords(keywordsOptions) .sentiment(sentimentOptions) .build(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // 参数 AnalyzeOptions parameters = new AnalyzeOptions.Builder() .text(text) .features(features) .build(); // 结果 AnalysisResults result = service.analyze(parameters).execute(); Double anger1 = result.getEmotion().getDocument().getEmotion().getAnger(); Double joy1 = result.getEmotion().getDocument().getEmotion().getJoy(); String sentiment1 = result.getSentiment().getDocument().getLabel(); Double sentiment1Value = result.getSentiment().getDocument().getScore(); String categories1 = result.getCategories().get(0).getLabel(); String keyword1 = result.getKeywords().get(0).getText(); |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |