This topic describes how to call the RaSC service, prepared in Work with various network protocols, through ProtocolBuffers. The client will be implemented in Java. Complete Work with various network protocols to start the RaSC service in advance.
First, download and unzip the RaSC client sample package.
$ wget https://alaginrc.nict.go.jp/rasc/resources/rasc-client-1.0.0.zip
$ unzip rasc-client-1.0.0.zip
The package contains the following files:
rasc-client-1.0.0/
+ lib/
+ ... ((Required libraries)
+ ClientProtcolBuffers/
+ build.xml
+ src/ (Source files)
+ ClientProtcolBuffers.java
ClientProtocolBuffers.java is a sample code for ProtocolBuffers clients implemented in Java.
ClientProtcolBuffers/src/ClientProtcolBuffers.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.net.MalformedURLException;
import java.net.URL;
import jp.go.nict.langrid.client.impl.protobuf.PbClientFactory;
import jp.go.nict.wisdom.wrapper.api.TextAnalysisService;
public class ClientProtcolBuffers {
/**
* ProtocolBuffers client sample.
*
* @param args args[0] String passed to the user program
*/
public static void main(String[] args) {
try {
if(args.length <= 0){
System.out.println("Specify in the argument the string to be passed to the user program.");
return;
}
TextAnalysisService s = new PbClientFactory().create(TextAnalysisService.class,
new URL("http://localhost:8080/___WAR_NAME___/pbServices/___SERVICE_NAME___"));
System.out.println(args[0]);
System.out.println("--- result ---");
System.out.println(s.analyze(args[0]));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
Use PbClientFactory#create() to create TextAnalysisService. The client calls TextAnalysisService#analyze(). Specify http://localhost:8080/___WAR_NAME___/pbServices/___SERVICE_NAME___ as an endpoint (address to call a service). These must be set according to ___WAR_NAME___ and ___SERVICE_NAME___ set in Work with various network protocols.
Call analyze() to call the user program. It is assumed that args[0] is given to the user program as the string to be processed.
cf. jp.go.nict.wisdom.wrapper.api.TextAnalysisService Interface class
1 2 3 4 5 6 | package jp.go.nict.wisdom.wrapper.api;
public interface TextAnalysisService {
String analyze(String text) throws Exception;
String[] analyzeArray(String[] text) throws Exception;
}
|
build.xml for the ProtocolBuffers client is as follows.
ClientProtcolBuffers/build.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?xml version="1.0" encoding="utf-8"?>
<project name="rasc_client" default="client">
<target name="init">
<mkdir dir="build" />
</target>
<target name="clean" depends="init">
<delete dir="build" />
</target>
<target name="client" depends="init">
<mkdir dir="build/client" />
<javac destdir="build/client" encoding="UTF-8"
includeantruntime="false" debug="true">
<src path="./src" />
<classpath>
<fileset dir="../lib" includes="*.jar" />
</classpath>
</javac>
<jar jarfile="build/clientProtocolBuffers.jar">
<fileset dir="build/client" includes="**/*.class" />
<fileset dir="./src" includes="**/*.java" />
<manifest>
<attribute name="Main-Class" value="ClientProtcolBuffers" />
<attribute name="Class-Path"
value=". ../../lib/jp.go.nict.langrid.client.ws_1_2.protobuf.jar
../../lib/wrapper-api.jar ../../lib/jp.go.nict.langrid.client.jar
../../lib/protobuf-java-2.2.0.jar ../../lib/jp.go.nict.langrid.commons.jar
../../lib/jp.go.nict.langrid.commons.cs.jar ../../lib/jp.go.nict.langrid.commons.protobufrpc.jar
../../lib/jp.go.nict.langrid.commons.beanutils.jar ../../lib/jp.go.nict.langrid.language.jar" />
</manifest>
</jar>
</target>
</project>
|
ant creates the build folder, compiles the Java source, and packages class files as build/clientProtocolBuffers.jar and clientProtocolBuffers.jar. You can do this by the following commands:
$ cd rasc-client-1.0.0/ClientProtcolBuffers/
$ ant
Run the ProtocolBuffers client as follows. You can specify a character string to be passed to the user program in the argument.
$ cd build/
$ java -jar clientProtocolBuffers.jar "He runs the company."
He runs the company.
--- result ---
ROOT ROOT ROOT ROOT -1 ROOT ROOT runs run VBZ VB 1
runs run VBZ VB 1 verb_arg12 ARG1 He he PRP PRP 0
runs run VBZ VB 1 verb_arg12 ARG2 company company NN NN 3
the the DT DT 2 det_arg1 ARG1 company company NN NN 3