import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import javax.xml.xpath.*;
import java.util.ArrayList;
public class YahooMorph {
private String appid;
private final String path = "http://jlp.yahooapis.jp/MAService/V1/parse?";
public class Morpheme {
private String surface;
private String reading;
private String pos;
private String baseform;
public Morpheme(String surface, String reading, String pos, String baseform){
this.surface = surface;
this.reading = reading;
this.pos = pos;
this.baseform = baseform;
}
public String getSurface(){ return surface; }
public String getReading(){ return reading; }
public String getPos(){ return pos; }
public String getBaseform(){ return baseform; }
}
public YahooMorph(String appid){
this.appid = appid;
}
public ArrayList<Morpheme> analyse(String text){
ArrayList<Morpheme> morph_list = new ArrayList<Morpheme>();
try{
URL url = new URL(path + "appid=" + appid + "&results=ma&response=surface,reading,pos,baseform&sentence=" + URLEncoder.encode(text, "utf-8"));
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("GET");
http.connect();
InputStream input = url.openStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(input);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
NodeList surfaces = (NodeList)xpath.evaluate("//ma_result/word_list/word/surface/text()", doc, XPathConstants.NODESET);
NodeList readings = (NodeList)xpath.evaluate("//ma_result/word_list/word/reading/text()", doc, XPathConstants.NODESET);
NodeList poses = (NodeList)xpath.evaluate("//ma_result/word_list/word/pos/text()", doc, XPathConstants.NODESET);
NodeList baseforms = (NodeList)xpath.evaluate("//ma_result/word_list/word/baseform/text()", doc, XPathConstants.NODESET);
for(int i=0; i < surfaces.getLength(); i++){
morph_list.add(new Morpheme(surfaces.item(i).getNodeValue(),
readings.item(i).getNodeValue(),
poses.item(i).getNodeValue(),
baseforms.item(i).getNodeValue()));
}
http.disconnect();
} catch(Exception e){
System.out.println(e);
System.exit(1);
}
return morph_list;
}
public static void main(String[] args) {
String appid = "xxxx";
YahooMorph yahooMorph = new YahooMorph(appid);
String text = "明日,晴れることを願っている。";
ArrayList<Morpheme> result = yahooMorph.analyse(text);
System.out.println("=== Example 1 ===");
for(int i=0; i < result.size(); i++){
Morpheme m = result.get(i);
System.out.println(m.getSurface()
+ "\t" + m.getReading()
+ "\t" + m.getPos()
+ "\t" + m.getBaseform());
}
text = "JavaもC言語もどちらも大切でしょう。";
result = yahooMorph.analyse(text);
System.out.println("=== Example 2===");
for(int i=0; i < result.size(); i++){
Morpheme m = result.get(i);
System.out.println(m.getSurface()
+ "\t" + m.getReading()
+ "\t" + m.getPos()
+ "\t" + m.getBaseform());
}
}
}