Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watsonAPIを用いた自然言語カテゴリ抽出実装 #14

Merged
merged 1 commit into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
</repository>
</repositories>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -77,6 +78,12 @@
<artifactId>twitter4j-core</artifactId>
<version>[4.0,)</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ibm.watson/ibm-watson -->
<dependency>
<groupId>com.ibm.watson</groupId>
<artifactId>ibm-watson</artifactId>
<version>8.5.0</version>
</dependency>
</dependencies>

<build>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/demo/DemoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DemoApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
q

@Autowired
JdbcTemplate jdbcTemplate;

Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/example/demo/controller/NLUController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.example.demo.controller;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ibm.cloud.sdk.core.security.IamAuthenticator;
import com.ibm.watson.natural_language_understanding.v1.NaturalLanguageUnderstanding;
import com.ibm.watson.natural_language_understanding.v1.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController
@RequestMapping(path = "/NLU")
public class NLUController {
private static final Logger LOG = LoggerFactory.getLogger(ViewController.class);

@Autowired
JdbcTemplate jdbcTemplate;

@RequestMapping(path = "/test", method = RequestMethod.GET)
public String index() {
IamAuthenticator authenticator = new IamAuthenticator("fVfaYMA7tCh4zInWBhTBb5t69xQryK7ObKl42nampynG");
NaturalLanguageUnderstanding naturalLanguageUnderstanding = new NaturalLanguageUnderstanding("2019-07-12", authenticator);
naturalLanguageUnderstanding.setServiceUrl("https://api.jp-tok.natural-language-understanding.watson.cloud.ibm.com/instances/ac3df365-93f6-4255-beb8-620d66041251");
String text = "ユーチューバー。2006年からYouTubeやってます。YouTube&SNSフォロワー計2000万人突破。再生回数計120億回突破。UUUM株式会社最高顧問&ファウンダー。インスタTikTokもフォロー是非 ! DM読んでませんので、お仕事、その他コラボ依頼や動画SNS絡みのことは事務所まで";

CategoriesOptions categories= new CategoriesOptions.Builder()
.limit(3)
.build();

Features features = new Features.Builder()
.categories(categories)
.build();

AnalyzeOptions parameters = new AnalyzeOptions.Builder()
.text(text)
.features(features)
.build();

AnalysisResults response = naturalLanguageUnderstanding
.analyze(parameters)
.execute()
.getResult();
List<String> resultCategories = new ArrayList<>();
for (CategoriesResult r : response.getCategories()) {
if (r.getScore() > 0.7) {
String[] splitword = r.getLabel().split("/");
resultCategories.add(splitword[splitword.length - 1]);
}
}


// return resultCategories.toString();
return response.getCategories().get(0).getLabel();
}
}