便利です。ちょっと使ってみました。Yahoo API テキスト解析 - ルビ振り 2010/03/27

Yahoo APIいろいろ充実していますね。

ちょっといろいろフリガナにしたい文字列があって途方にくれていたのですが、Yahoo APIのテキスト解析はすばらしいです!!
人手でやるとかなり面倒だけど、コンピュータでやれるとあっという間でした。



形態素解析とか、その辺のノウハウがあれば、自前で辞書とか用意してやれるのかもしれないけど、API便利だなー。

それで、全く参考になりませんが、Javaです。
XPath使ってます。

package a;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestHUriganaHenkan {

public static void main(String[] args) throws IOException,
InterruptedException, SAXException, ParserConfigurationException,
XPathExpressionException {
a();
}

static String MAKE_URL(String sentence) throws UnsupportedEncodingException {
final String APP_ID = "<あなたのAPP_ID>";
final String SENTENCE = URLEncoder.encode(sentence, "utf-8");
final String GRADE = "1";
final String URL = String
.format(
"http://jlp.yahooapis.jp/FuriganaService/V1/furigana?appid=%s&grade=%s&sentence=%s",
APP_ID, GRADE, SENTENCE);
return URL;
}

static void a() throws IOException, InterruptedException, SAXException,
ParserConfigurationException, XPathExpressionException {
String s = "変換したい文字";
String surl = MAKE_URL(s);
byte[] result = get_html(surl);
String sresult = new String(result, "utf-8");
ByteArrayInputStream input = new ByteArrayInputStream(sresult
.getBytes("utf-8"));
Document document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(input);
aa(document);

}

static void aa(Document doc) throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//Word/Furigana");

// Object result = expr.evaluate(doc, XPathConstants.STRING);
NodeList nodeList = (NodeList) expr.evaluate(doc,
XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
System.out.print(node.getTextContent() + " ");

}
// System.out.println(result.getNodeValue());

}

public static byte[] get_html(String surl) throws MalformedURLException,
IOException {
URLConnection connection = new URL(surl).openConnection();
connection.connect();
BufferedInputStream in = new BufferedInputStream(
(InputStream) connection.getInputStream());
return get(in);
}

public static byte[] get(InputStream in) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
int len = 0;
byte[] bs = new byte[8192];
while ((len = in.read(bs)) != -1) {
outputStream.write(bs, 0, len);
outputStream.flush();
}
outputStream.close();
} finally {
in.close();
}
return outputStream.toByteArray();
}

}

: