Skip to content

Commit

Permalink
Merge pull request #14 from shortintern2020-A-labyrinth/watsonNLU
Browse files Browse the repository at this point in the history
watsonAPIを用いた自然言語カテゴリ抽出実装
  • Loading branch information
Highmt authored Aug 25, 2020
2 parents 5a77b58 + fcda51f commit 2b18bcb
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
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();
}
}

0 comments on commit 2b18bcb

Please sign in to comment.