プログラマメモ2 - programmer no memo2

再度、wikipedia apiを使ってみたい。- Java + axis2 2010/09/01

プログラマメモ2: wikipedia apiを使ってみたい。- Java + axis2

Javaです。二年前の記事ですが、もう一度チャレンジです。
僕の環境はmac osxでJava1.6です。Eclipseベースで行ってます。

workspaceに、空のJavaプロジェクトを作成して、libフォルダを作成して、Axis2を解凍しておいておきます。
今回使ったのは、1.5.1です。



Apache Axis2 -


ついでにaxis2のlibにあるjarをとりあえず全部、buildpathにaddしておきます。
ほんとは、必要なものだけで選んでおきたいけど...



プロジェクト内にworkフォルダを用意してそこでWSDLからJavaのソースコード生成します.

ターミナル(windowsならコマンドプロンプト?)で、下の命令を実行

../lib/axis2-1.5.1/bin/wsdl2java.sh -uri http://dev.wikipedia-lab.org/WikipediaOntologyAPIv3/Service.asmx?WSDL -p wikipediaapi


-p指定で、wikipediaapiとします。
今回はパッケージをwikipediaapiとしてみました。
あと環境変数JAVA_HOMEは設定してないといけないです。

あと、ワーニングがでてきますが、気にしないで、前にすすみます。



eclipseでみると下のような感じ

できたソースをeclipseのビルドパスに含めてしまいます。
で、下の感じ。

これで生成できたので、あとは利用するだけです。
ちょっとしたtipsですが、生成されたソースフォルダと自分で作成するソースのフォルダをeclipseで別々にしておくと、
いろいろあとから変更しやすくなります。

サンプルコード

package wikipediaapi;

import java.rmi.RemoteException;

import org.apache.axis2.databinding.ADBException;
import org.apache.axis2.databinding.types.UnsignedInt;

import wikipediaapi.ServiceStub.GetThesaurusDS;
import wikipediaapi.ServiceStub.GetThesaurusDSResponse;
import wikipediaapi.ServiceStub.GetTopCandidateIDFromKeyword;
import wikipediaapi.ServiceStub.GetTopCandidateIDFromKeywordResponse;

public class Test {

public static void main(String[] args) throws RemoteException, ADBException {

String[] keywords = { "侍", "apple", "google", "o_o!" };
for (String k : keywords) {
System.out.println(b(a(k)));
}
}

static int a(String skeyword) throws RemoteException {
ServiceStub serviceStub = new ServiceStub();

GetTopCandidateIDFromKeyword keyword = new GetTopCandidateIDFromKeyword();
keyword.setKeyword(skeyword);
keyword.setLanguage("Japanese");

GetTopCandidateIDFromKeywordResponse response = serviceStub
.getTopCandidateIDFromKeyword(keyword);

return response.getGetTopCandidateIDFromKeywordResult().intValue();
}

static String b(int i) throws RemoteException {

ServiceStub serviceStub = new ServiceStub();

GetThesaurusDS getThesaurusDS = new GetThesaurusDS();
getThesaurusDS.setLanguage("Japanese");
getThesaurusDS.setIFrom(new UnsignedInt(i));

GetThesaurusDSResponse response = serviceStub
.getThesaurusDS(getThesaurusDS);

return "" + response.localGetThesaurusDSResult.localExtraElement;
}

}

wikipedia apiを使ってみたい。- JGraphで表示 その3 2008/05/07



表示を少し変えてみました。

package util;

import static util.WikipedialabUtils.getThesaurusDS4NetworkView;
import static util.WikipedialabUtils.getTopCandidateIDFromKeyword;

import java.awt.Color;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.xml.sax.SAXException;

import util.WikipedialabUtils.MNode;

public class TestT {

static JGraph graph() {
GraphModel model = new DefaultGraphModel();
JGraph graph = new JGraph(model);

graph.getGraphLayoutCache().setFactory(new DefaultCellViewFactory());
graph.setCloneable(true);
graph.setInvokesStopCellEditing(true);

graph.setJumpToDefaultPort(true);
return graph;
}

static DefaultGraphCell createDefaultGraphCell(String name, int px, int py, int w, int h, Color color){
DefaultGraphCell cell = new DefaultGraphCell(name);

GraphConstants.setBounds(cell.getAttributes(),
new Rectangle2D.Double(px,py,w,h));
GraphConstants.setGradientColor(cell.getAttributes(), color);
GraphConstants.setOpaque(cell.getAttributes(), true);

cell.addPort();
return cell;
}

static Point[] points(int n, final int cx, final int cy, final int R) {
final double d = 360.;
final double d2 = d / n;

Point[] points = new Point[n];
for (int i = 0; i < n; i++) {
int x = (int) (cx + (R * Math.sin(Math.toRadians((d2 * i)))));
int y = (int) (cy + (R * Math.cos(Math.toRadians((d2 * i)))));
points[i] = new Point(x, y);
}
return points;
}

static void c(JGraph graph, MNode parent, DefaultGraphCell cellParent, int cx, int cy, int R, List<DefaultGraphCell> list, Color color) {

int n = parent.child.size();

Collection<MNode> collection = parent.child.values();


list.add(cellParent);
int i = 0;
Point[] points = points(n, cx, cy, R);
Point[] points2 = points(n, cx, cy, R + 100);
for (MNode node : collection) {
Point point = points[i];
DefaultGraphCell cell = createDefaultGraphCell(node.name, point.x, point.y, 80, 20, color);

Point point2 = points2[i];
c(graph, node, cell, point2.x, point2.y, R - 100, list, Color.lightGray);

list.add(cell);

DefaultEdge edge = new DefaultEdge();
edge.setSource(cellParent.getChildAt(0));
edge.setTarget(cell.getChildAt(0));

int arrow = GraphConstants.ARROW_NONE;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);

list.add(edge);
i++;
}

}

static void e(JGraph graph, MNode parent){
int cx = 500;
int cy = 500;

int R = 150;
List<DefaultGraphCell> list = new ArrayList<DefaultGraphCell>();
DefaultGraphCell cellParent = createDefaultGraphCell(parent.name, cx, cy, 80, 20, Color.orange);
c(graph, parent, cellParent, cx, cy, R, list, Color.orange);
graph.getGraphLayoutCache().insert(list.toArray());
}

public static void main(String[] args) throws UnsupportedEncodingException, XPathExpressionException, SAXException, IOException, ParserConfigurationException {
JGraph graph = graph();

String skey = "apple";

int i = getTopCandidateIDFromKeyword(WikipedialabUtils.JAPANESE, skey);
String s = getThesaurusDS4NetworkView(WikipedialabUtils.JAPANESE, i);

MNode node = WikipedialabUtils.createDS4NetworkView(skey, i, s);

e(graph, node);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(graph));
frame.pack();
frame.setVisible(true);
}
}

wikipedia apiを使ってみたい。- JGraphで表示 その2 2008/05/04






表示をかえてみました。対象語を中心に、関連後が円上にひろがってさらにそこから、ひろがった語から、関連する語がひろがるっという感じかな。

GetThesaurusDS4NetworkViewを使っています。


package util;

import static util.WikipedialabUtils.getThesaurusDS4NetworkView;
import static util.WikipedialabUtils.getTopCandidateIDFromKeyword;

import java.awt.Color;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.xml.sax.SAXException;

import util.WikipedialabUtils.MNode;

public class TestT {

static JGraph graph() {
GraphModel model = new DefaultGraphModel();
JGraph graph = new JGraph(model);

graph.getGraphLayoutCache().setFactory(new DefaultCellViewFactory());

// Control-drag should clone selection
graph.setCloneable(true);

// Enable edit without final RETURN keystroke
graph.setInvokesStopCellEditing(true);

// When over a cell, jump to its default port (we only have one, anyway)
graph.setJumpToDefaultPort(true);
return graph;
}

static DefaultGraphCell createDefaultGraphCell(String name, int px, int py, int w, int h, Color color){
DefaultGraphCell cell = new DefaultGraphCell(name);

GraphConstants.setBounds(cell.getAttributes(),
new Rectangle2D.Double(px,py,w,h));
GraphConstants.setGradientColor(cell.getAttributes(), color);
GraphConstants.setOpaque(cell.getAttributes(), true);

cell.addPort();
return cell;
}

static Point[] points(int n, final int cx, final int cy, final int R) {
final double d = 360.;
final double d2 = d / n;

Point[] points = new Point[n];
for (int i = 0; i < n; i++) {
int x = (int) (cx + (R * Math.sin(Math.toRadians((d2 * i)))));
int y = (int) (cy + (R * Math.cos(Math.toRadians((d2 * i)))));
points[i] = new Point(x, y);
}
return points;
}

static void c(JGraph graph, MNode parent, DefaultGraphCell cellParent, int cx, int cy, int R, List<DefaultGraphCell> list, Color color) {

int n = parent.child.size();

Collection<MNode> collection = parent.child.values();


list.add(cellParent);
int i = 0;
Point[] points = points(n, cx, cy, R);
for (MNode node : collection) {
Point point = points[i++];
DefaultGraphCell cell = createDefaultGraphCell(node.name, point.x, point.y, 80, 20, color);

c(graph, node, cell, point.x, point.y, R - 100, list, Color.lightGray);

list.add(cell);

DefaultEdge edge = new DefaultEdge();
edge.setSource(cellParent.getChildAt(0));
edge.setTarget(cell.getChildAt(0));

int arrow = GraphConstants.ARROW_NONE;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);

list.add(edge);
}




}

static void e(JGraph graph, MNode parent){
int cx = 500;
int cy = 500;

int R = 200;
List<DefaultGraphCell> list = new ArrayList<DefaultGraphCell>();
DefaultGraphCell cellParent = createDefaultGraphCell(parent.name, cx, cy, 80, 20, Color.orange);
c(graph, parent, cellParent, cx, cy, R, list, Color.orange);
graph.getGraphLayoutCache().insert(list.toArray());
}

public static void main(String[] args) throws UnsupportedEncodingException, XPathExpressionException, SAXException, IOException, ParserConfigurationException {
JGraph graph = graph();

String skey = "apple";

int i = getTopCandidateIDFromKeyword(WikipedialabUtils.JAPANESE, skey);
String s = getThesaurusDS4NetworkView(WikipedialabUtils.JAPANESE, i);

MNode node = WikipedialabUtils.createDS4NetworkView(skey, i, s);

e(graph, node);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(graph));
frame.pack();
frame.setVisible(true);
}
}

wikipedia apiを使ってみたい。- JGraphで表示 2008/05/04



なんとなく可視化に挑戦。
中心におかれた語の関連後を周りに配置しています。

メインページ - Wikipedia-Lab
wikipedia-labのapiを使っています。

前、記事を書いたものをベースにしています。
プログラマメモ2: wikipedia apiを使ってみたい。- Java + axis2

表示するために使ったライブラリは、
Java Graph Visualization and Layout

package util;

import static util.WikipedialabUtils.getThesaurusDS4NetworkView;
import static util.WikipedialabUtils.getTopCandidateIDFromKeyword;

import java.awt.Color;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.xml.sax.SAXException;

import util.WikipedialabUtils.MNode;

public class TestT {

static JGraph graph() {
GraphModel model = new DefaultGraphModel();
JGraph graph = new JGraph(model);

graph.getGraphLayoutCache().setFactory(new DefaultCellViewFactory());

// Control-drag should clone selection
graph.setCloneable(true);

// Enable edit without final RETURN keystroke
graph.setInvokesStopCellEditing(true);

// When over a cell, jump to its default port (we only have one, anyway)
graph.setJumpToDefaultPort(true);
return graph;
}

static DefaultGraphCell createDefaultGraphCell(String name, int px, int py, int w, int h){
DefaultGraphCell cell = new DefaultGraphCell(name);

GraphConstants.setBounds(cell.getAttributes(),
new Rectangle2D.Double(px,py,w,h));
GraphConstants.setGradientColor(cell.getAttributes(), Color.orange);
GraphConstants.setOpaque(cell.getAttributes(), true);

cell.addPort();
return cell;
}

static Point[] points(int n, final int cx, final int cy, final int R) {
final double d = 360.;
final double d2 = d / n;

Point[] points = new Point[n];
for (int i = 0; i < n; i++) {
int x = (int) (cx + (R * Math.sin(Math.toRadians((d2 * i)))));
int y = (int) (cy + (R * Math.cos(Math.toRadians((d2 * i)))));
points[i] = new Point(x, y);
}
return points;
}

static void c(JGraph graph, MNode parent) {

int n = parent.child.size();

int cx = 200;
int cy = 200;

int R = 200;

List<DefaultGraphCell> list = new ArrayList<DefaultGraphCell>();
Collection<MNode> collection = parent.child.values();

DefaultGraphCell cellParent = createDefaultGraphCell(parent.name, cx, cy, 80, 20);
list.add(cellParent);
int i = 0;
Point[] points = points(n, cx, cy, R);
for (MNode node : collection) {
Point point = points[i++];
DefaultGraphCell cell = createDefaultGraphCell(node.name, point.x, point.y, 80, 20);


list.add(cell);

DefaultEdge edge = new DefaultEdge();
edge.setSource(cellParent.getChildAt(0));
edge.setTarget(cell.getChildAt(0));

int arrow = GraphConstants.ARROW_NONE;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);

list.add(edge);
}


graph.getGraphLayoutCache().insert(list.toArray());

}

public static void main(String[] args) throws UnsupportedEncodingException, XPathExpressionException, SAXException, IOException, ParserConfigurationException {
JGraph graph = graph();

String skey = "apple";

int i = getTopCandidateIDFromKeyword(WikipedialabUtils.JAPANESE, skey);
String s = getThesaurusDS4NetworkView(WikipedialabUtils.JAPANESE, i);

MNode node = WikipedialabUtils.createDS4NetworkView(skey, i, s);

c(graph, node);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(graph));
frame.pack();
frame.setVisible(true);
}
}


wikipedia apiからデータを取得する部分
package util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.apache.axis2.databinding.types.UnsignedInt;
import org.tempuri.ServiceStub;
import org.tempuri.ServiceStub.GetThesaurusDS;
import org.tempuri.ServiceStub.GetThesaurusDS300;
import org.tempuri.ServiceStub.GetThesaurusDS300Response;
import org.tempuri.ServiceStub.GetThesaurusDS4NetworkView;
import org.tempuri.ServiceStub.GetThesaurusDS4NetworkViewResponse;
import org.tempuri.ServiceStub.GetThesaurusDSResponse;
import org.tempuri.ServiceStub.GetTopCandidateIDFromKeyword;
import org.tempuri.ServiceStub.GetTopCandidateIDFromKeywordResponse;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public abstract class WikipedialabUtils {

static public final String JAPANESE = "Japanese";

static int getTopCandidateIDFromKeyword(String lang, String skeyword)
throws RemoteException {
ServiceStub serviceStub = new ServiceStub();

GetTopCandidateIDFromKeyword keyword = new GetTopCandidateIDFromKeyword();
keyword.setKeyword(skeyword);
keyword.setLanguage(lang);

GetTopCandidateIDFromKeywordResponse response = serviceStub
.GetTopCandidateIDFromKeyword(keyword);

return response.getGetTopCandidateIDFromKeywordResult().intValue();
}

static String getThesaurusDS(String lang, int i) throws RemoteException {

ServiceStub serviceStub = new ServiceStub();

GetThesaurusDS getThesaurusDS = new GetThesaurusDS();
getThesaurusDS.setLanguage(lang);
getThesaurusDS.setIFrom(new UnsignedInt(i));

GetThesaurusDSResponse response = serviceStub
.GetThesaurusDS(getThesaurusDS);

return "" + response.getGetThesaurusDSResult().getExtraElement();
}

static String getThesaurusDS300(String lang, int i) throws RemoteException {

ServiceStub serviceStub = new ServiceStub();

GetThesaurusDS300 getThesaurusDS300 = new GetThesaurusDS300();
getThesaurusDS300.setLanguage(lang);
getThesaurusDS300.setIFrom(new UnsignedInt(i));

GetThesaurusDS300Response response = serviceStub
.GetThesaurusDS300(getThesaurusDS300);

return "" + response.getGetThesaurusDS300Result().getExtraElement();
}

static String getThesaurusDS4NetworkView(String lang, int i)
throws RemoteException {

ServiceStub serviceStub = new ServiceStub();

GetThesaurusDS4NetworkView getThesaurusDS4NetworkView = new GetThesaurusDS4NetworkView();
getThesaurusDS4NetworkView.setLanguage(lang);
getThesaurusDS4NetworkView.setIFrom(new UnsignedInt(i));

GetThesaurusDS4NetworkViewResponse response = serviceStub
.GetThesaurusDS4NetworkView(getThesaurusDS4NetworkView);

return ""
+ response.getGetThesaurusDS4NetworkViewResult()
.getExtraElement();
}

static MNode createDS4NetworkView(String name, int n, String sxml)
throws UnsupportedEncodingException, SAXException, IOException,
ParserConfigurationException, XPathExpressionException {
Document document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(
new ByteArrayInputStream(sxml.getBytes("utf-8")));
List<Table> list = new ArrayList<Table>();
XPath path = XPathFactory.newInstance().newXPath();

NodeList nodeList = (NodeList) path.evaluate("/diffgram/NewDataSet",
document, XPathConstants.NODESET);

MNode parent = new MNode(name, n);

for (int i = 0; i < nodeList.getLength(); i++) {

Node node = nodeList.item(i);
NodeList nodeList2 = node.getChildNodes();

NodeList o = (NodeList)path.evaluate("Center", nodeList2, XPathConstants.NODESET);
for (int j = 0; j < o.getLength(); j++) {
Node node2 = o.item(j);

MNode mn = new MNode();
mn.n = new Integer(path.evaluate("l_to", node2));
mn.name = path.evaluate("name", node2);
mn.weight = new Double(path.evaluate("l_weight", node2));
mn.parent = parent;
parent.add(mn);
}

NodeList o2 = (NodeList)path.evaluate("*[local-name()!='Center']", nodeList2, XPathConstants.NODESET);
for (int j = 0; j < o2.getLength(); j++) {
Node node2 = o2.item(j);
MNode mn = new MNode();
int from = new Integer(path.evaluate("l_from", node2));
mn.n = new Integer(path.evaluate("l_to", node2));
mn.name = path.evaluate("name", node2);
mn.weight = new Double(path.evaluate("l_weight", node2));

MNode p = parent.child(from);
if(p != null){
mn.parent = p;
p.add(mn);
} else {
System.err.println("error parent[" + from + "] not exist.");
}
}

}

return parent;
}

static class Table {

int l_from;
int l_to;
double l_weight;
String name;

public String toString() {
return "l_from:[" + l_from + "] l_to:[" + l_to + "] l_weight:["
+ l_weight + "] name:[" + name + "]";
}
}

static class MNode {
String name;
int n;
double weight;
MNode parent;
Map<Integer, MNode> child = new HashMap<Integer, MNode>();

public MNode(){

}
public MNode(String name, int n){
this.name = name;
this.n = n;
}
public void add(MNode node){
child.put(node.n, node);
}
public MNode child(int n){
return child.get(n);
}

public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof MNode))
return false;
MNode node = (MNode) o;
if (node.n == node.n)
return true;
return false;
}

public String toString(){
return "name:[" + name + "] n:[" + n + "] parent:["
+ (parent != null ? parent.n : -1) + "]";
}

public String toString(int c){
StringBuilder sb = new StringBuilder();
for(int i=0;i<(c*2);i++){
sb.append(" ");
}
sb.append(toString()).append("\n");

Collection<MNode> collection = child.values();
for (MNode node : collection) {
sb.append(node.toString(c + 1));
}
return new String(sb);
}
}

}

wikipedia apiを使ってみたい。- Java + axis2 2008/05/01

動機


Wikipediaシソーラス - Wikipedia-Labがちょっとおもしろいと思ったので、ちょっと実験。

このWikipedia-LabのWikipediaシソーラスは、リンク構造に着目したWikipediaマイニングから開発されているそうです。


Wikipediaシソーラスビジュアライザー - Wikipedia-Labがかっこよかったです。

Javaです。WSDLをaxis2でjavaソースにします。



紹介されているサンプルが.netのものしかなかったのですが(20080502現在)、axis2を使って、wsdlからjavaコードにして、使ってみます。


変換するためにbin/wsdl2javaを使います。
src/org/tempuri/ServiceCallbackHandler.java
src/org/tempuri/ServiceStub.java

が生成されます。

以下、キーワードから、シソーラスを取り出すための簡単なコードです。
まず、GetTopCandidateIDFromKeywordで、番号をidをゲットして、それを使いGetThesaurusDSをゲットするという感じの流れです。

このコードではxmlを単純に標準出力しています。
package org.tempuri;

import java.rmi.RemoteException;

import org.apache.axis2.databinding.ADBException;
import org.apache.axis2.databinding.types.UnsignedInt;
import org.tempuri.ServiceStub.GetThesaurusDS;
import org.tempuri.ServiceStub.GetThesaurusDSResponse;
import org.tempuri.ServiceStub.GetTopCandidateIDFromKeyword;
import org.tempuri.ServiceStub.GetTopCandidateIDFromKeywordResponse;

public class Test {

public static void main(String[] args) throws RemoteException, ADBException {

String[] keywords = { "侍", "apple", "google", "o_o!" };
for (String k : keywords) {
System.out.println(b(a(k)));
}
}

static int a(String skeyword) throws RemoteException {
ServiceStub serviceStub = new ServiceStub();

GetTopCandidateIDFromKeyword keyword = new GetTopCandidateIDFromKeyword();
keyword.setKeyword(skeyword);
keyword.setLanguage("Japanese");

GetTopCandidateIDFromKeywordResponse response = serviceStub
.GetTopCandidateIDFromKeyword(keyword);

return response.getGetTopCandidateIDFromKeywordResult().intValue();
}

static String b(int i) throws RemoteException {

ServiceStub serviceStub = new ServiceStub();

GetThesaurusDS getThesaurusDS = new GetThesaurusDS();
getThesaurusDS.setLanguage("Japanese");
getThesaurusDS.setIFrom(new UnsignedInt(i));

GetThesaurusDSResponse response = serviceStub
.GetThesaurusDS(getThesaurusDS);

return "" + response.localGetThesaurusDSResult.localExtraElement;
}

}