Return to Index: [001] Morphological analysis> [002] Parsing> [003] Statistical processing of part of speech
■ Addition
Yahoo! Parsing API V1 has ended, so this article is for reference only. For more information [Important] Notice of Japanese dependency analysis API specification change in text analysis Web API --Yahoo! Developer Network https://developer.yahoo.co.jp/changelog/web_apiapi.html Please refer to the.
Let's try Japanese parsing (dependency analysis) in Java using NLP4J.
From the text "The car suddenly stopped" when the parsing was possible ・ "The car stopped →" ・ "Suddenly → stopped" You can extract information such as.
Maven
<dependency>
<groupId>org.nlp4j</groupId>
<artifactId>nlp4j</artifactId>
<version>1.0.0.0</version>
</dependency>
Code1
import nlp4j.Keyword;
import nlp4j.KeywordWithDependency;
import nlp4j.impl.DefaultNlpServiceResponse;
import nlp4j.yhoo_jp.YJpDaService;
public class HelloNLP4JDA {
//Natural text
String text = "The car stopped suddenly.";
//Dependency analysis
YJpDaService service = new YJpDaService();
//Get the results of the dependency analysis
DefaultNlpServiceResponse response = service.process(text);
for (Keyword kwd : response.getKeywords()) {
if (kwd instanceof KeywordWithDependency) {
//Output the result of dependency analysis
System.err.println(((KeywordWithDependency) kwd).toStringAsDependencyTree());
}
}
}
Output1 You can output like this. It's easy!
-sequence=6,lex=null,str=。
-sequence=5,lex=null,str=Ta
-sequence=4,lex=null,str=Stopped
-sequence=2,lex=null,str=But
-sequence=1,lex=null,str=car
-sequence=3,lex=null,str=suddenly
Code2 You can examine detailed results by manipulating the keyword object.
public static void main(String[] args) throws Exception {
//Natural text
String text = "The car stopped suddenly.";
//Dependency analysis
YJpDaService service = new YJpDaService();
//Get the results of the dependency analysis
DefaultNlpServiceResponse response = service.process(text);
for (Keyword kwd : response.getKeywords()) {
if (kwd instanceof KeywordWithDependency) {
//Output the result of dependency analysis
print((KeywordWithDependency) kwd, 0);
}
}
}
static void print(KeywordWithDependency kwd, int depth) {
System.err.println(depth + ":" + kwd.getStr());
for (KeywordWithDependency kwd2 : kwd.getChildren()) {
print(kwd2, depth + 1);
}
}
Output2
0:。
1:Ta
2:Stopped
3:But
4:car
3:suddenly
It's easy!
Introduction of NLP4J-[000] Natural Language Processing Index in Java
https://www.nlp4j.org/
Recommended Posts