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

Feature/connection twitter api nlpapi #19

Merged
merged 6 commits 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
22 changes: 11 additions & 11 deletions demo.iml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@
<orderEntry type="library" name="Maven: org.twitter4j:twitter4j-core:4.0.7" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:ibm-watson:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:assistant:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:common:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:compare-comply:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:discovery:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:language-translator:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:natural-language-classifier:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:natural-language-understanding:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:personality-insights:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:speech-to-text:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:text-to-speech:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:tone-analyzer:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:visual-recognition:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.cloud:sdk-core:8.1.0" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.14.9" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okio:okio:1.17.2" level="project" />
Expand All @@ -154,16 +165,5 @@
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.6" level="project" />
<orderEntry type="library" name="Maven: io.reactivex.rxjava2:rxjava:2.2.19" level="project" />
<orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.3" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:common:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:compare-comply:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:discovery:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:language-translator:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:natural-language-classifier:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:natural-language-understanding:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:personality-insights:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:speech-to-text:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:text-to-speech:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:tone-analyzer:8.5.0" level="project" />
<orderEntry type="library" name="Maven: com.ibm.watson:visual-recognition:8.5.0" level="project" />
</component>
</module>
53 changes: 53 additions & 0 deletions src/main/java/com/example/demo/controller/TwitterController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMethod;
import twitter4j.*;

import java.io.IOException;

@RestController
@RequestMapping(path = "/twitter")
public class TwitterController {

@GetMapping("/search_keyword")
public QueryResult search_keyowrd(@RequestParam(name = "searchword", defaultValue = "チンチン") String searchword) throws TwitterException {
// 初期化
Twitter twitter = new TwitterFactory().getInstance();
Query query = new Query(searchword);

QueryResult result = twitter.search(query);

// for debug. show tweet results on CLI
for (Status status : result.getTweets()) {
System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
}

return result;
}

@RequestMapping(path = "/search_user", method = RequestMethod.GET)
public QueryResult search_user(@RequestParam(name = "username", defaultValue = "CNN") String username,
@RequestParam(name = "tweet_id", defaultValue = "1000000") long tweet_id
) throws TwitterException {
// 初期化
Twitter twitter = new TwitterFactory().getInstance();
Query query = new Query();

//ユーザーネームで指定.sinceId以降のtweetから15個取ってくる.最新のツイートから時系列順に取ってくるっぽい。
query.setQuery("from:" + username);
query.setSinceId(tweet_id);

QueryResult result = twitter.search(query);

//for debug on CLI.
for (Status status : result.getTweets()) {
System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
}

//TODO: ここで取ってきたLatestTweetIdを保存しておいて、次回からそのidのツイート以降のツイートを拾ってくればOK

return result;
}
}
79 changes: 60 additions & 19 deletions src/main/java/com/example/demo/controller/ViewController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
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;
Expand Down Expand Up @@ -33,26 +37,32 @@ public String read(@PathVariable("id") String id) {
return list.toString();
}

@RequestMapping(path = "/search_keyword", method = RequestMethod.GET)
public String search_keyowrd(@RequestParam(name = "searchword", defaultValue = "チンチン") String searchword) throws TwitterException {
// 初期化
Twitter twitter = new TwitterFactory().getInstance();
Query query = new Query(searchword);

QueryResult result = twitter.search(query);
//TODO: Controllerのままか、返り値の型の調整どうするか考える(フロントとの兼ね合い。)
@RequestMapping(path = "/twitter_to_NLU", method = RequestMethod.GET)
public List<String> collect_NLU_keywords_from_tweets(
@RequestParam(name = "username", defaultValue = "CNN") String username,
@RequestParam(name = "tweet_id", defaultValue = "1000000") long tweet_id
) throws TwitterException {
QueryResult result = search_user(username, tweet_id);
ArrayList<String> NLU_results = new ArrayList<>();

// for debug. show tweet results on CLI
for (Status status : result.getTweets()) {
System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
String tmp = NLU(status.getText());
if(tmp != null) {
System.out.println(tmp);
NLU_results.add(tmp);
}
}

return "a";
return NLU_results;
}

@RequestMapping(path = "/search_user", method = RequestMethod.GET)
public String search_user(@RequestParam(name = "username", defaultValue = "CNN") String username,
@RequestParam(name = "tweet_id", defaultValue = "1000000") long tweet_id
) throws TwitterException {



// コントローラは関数として呼び出すのはキツイっぽいのでとりま関数として取り出してる。。
//TODO: TwitterControllerやNLUControllerから共通部分を分離して別クラスとして保持。
private QueryResult search_user(String username, long tweet_id) throws TwitterException {
// 初期化
Twitter twitter = new TwitterFactory().getInstance();
Query query = new Query();
Expand All @@ -63,13 +73,44 @@ public String search_user(@RequestParam(name = "username", defaultValue = "CNN")

QueryResult result = twitter.search(query);

//for debug on CLI.
for (Status status : result.getTweets()) {
System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
return result;
}

private String NLU(String text) {
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");

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]);
}
}

//TODO: ここで取ってきたLatestTweetIdを保存しておいて、次回からそのidのツイート以降のツイートを拾ってくればOK

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



}
Binary file modified target/classes/com/example/demo/DemoApplication.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/controller/ViewController.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/entity/View.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/repository/ViewRepository.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/service/ViewService.class
Binary file not shown.
Binary file modified target/test-classes/com/example/demo/DemoApplicationTests.class
Binary file not shown.