NLP4J [002] Try parsing Japanese using Yahoo! Developer Network Japanese Parsing Analysis (V1) in Java

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!

Return to Index

Introduction of NLP4J-[000] Natural Language Processing Index in Java

Project URL

https://www.nlp4j.org/ NLP4J_N_128.png


Recommended Posts

NLP4J [002] Try parsing Japanese using Yahoo! Developer Network Japanese Parsing Analysis (V1) in Java
NLP4J [001a] Morphological analysis in Java (using Yahoo! Developer Network Japanese morphological analysis)
NLP4J [004] Try text analysis using natural language processing and parsing statistical processing in Java
Try using the COTOHA API parsing in Java
NLP4J [001b] Morphological analysis in Java (using kuromoji)
NLP4J [003] Try text analysis using natural language processing and part-speech statistical processing in Java
Try using the Stream API in Java
Try using JSON format API in Java
Try using Sourcetrail (win version) in Java code
Try using GCP's Cloud Vision API in Java
Try using Sourcetrail (macOS version) in Java code
Try global hooking in Java using the JNativeHook library
Implement Thread in Java and try using anonymous class, lambda
Translate using Microsoft Translator Text API in Java (Japanese → English)
Try scraping using java [Notes]
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Topic Analysis (LDA) in Java
2 Implement simple parsing in Java
Try using gRPC in Ruby