使用InfoSphere Streams的自定义Java 运算符和ICU4J实现实时音译2014-08-21 IBM Bharath Kumar Devara集成 Java 音译模块和 InfoSphere Streams 的自定义 Java 运算符
简介
在成长型市场区域中,任何解决方案提供商面临的首要挑战是可用数据的方言和语言学的不一致性。由于成长型市场区域中拥有包括英语在内的多种官方语言,所以地区的语言符号逐渐嵌入到了英语符号中。因此,您首先需要执行音译来实现数据中的一致性,然后再继续执行处理/文本分析。如果使用预定的语言,那么数据音译会为您提供更统一、更一致的结果。本文将介绍使用 InfoSphere Streams 的自定义 Java 运算符和 ICU4J 库执行实时音译时所涉及的步骤。IBM InfoSphere Streams 提供了执行实时分析流程的功能,提供了各种工具包和适配器,允许您实时连接到各种资源并从中交换数据,在数据上执行操作。实时音译的高级实现架构如图 1 所示。
图 1. 实时音译高级解决方案图

先决条件
业务先决条件:应当具有利用 InfoSphere Streams 设计和运行 Streams Processing Language (SPL) 应用程序作业的基本技能,并能利用 Java 编程的中级技能。源语言必须使用 UTF-8、UTF-16 格式进行编码。软件先决条件:InfoSphere Streams(2.0 或更高版本),以及 ICU4J 库。
创建音译自定义 Java 运算符
执行以下步骤,创建音译自定义 Java 运算符。按照 Streams 信息中心 中的描述搭建用于 Java 运算符开发的 Streams Studio 环境。搭建好环境之后,使用 Java 运算符中的 ICU4J 库编写音译逻辑。ICU4J 库的 jar 文件应导入到项目工作区中。SPL 中的原始 Java 运算符的结构如清单 1 所示。
清单 1. InfoSphere Streams 中 Java 运算符的格式
public synchronized void initialize(OperatorContext context);public void process(StreamingInput<Tuple> inputStream, Tuple tuple);public void processPunctuation(StreamingInput<Tuple> inputStream,StreamingData.Punctuation marker);public void allPortsReady();public void shutdown();
运算符的逻辑应位于流程函数内。清单 2 显示了一个样例代码。
清单 2. 使用 Java 运算符执行音译的样例代码
public String toBaseCharacters(final String sText) {if (sText == null || sText.length() == 0)return sText;final char[] chars = sText.toCharArray();final int iSize = chars.length;final StringBuilder sb = new StringBuilder(iSize);for (int i = 0; i < iSize; i++) {String sLetter = new String(new char[] { chars[i] });sLetter = Normalizer.normalize(sLetter, Normalizer.NFKD);try {byte[] bLetter = sLetter.getBytes("UTF-8");sb.append((char) bLetter[0]);} catch (UnsupportedEncodingException e) {}}return sb.toString();}public final synchronized void process(final StreamingInput input,final Tuple tuple) throws Exception{try{OperatorContext ctxt =getOperatorContext();Transliterator t=Transliterator.getInstance(ctxt.getParameterValues("sourceLanguage").get(0)+"-"+ctxt.getParameterValues("destLanguage").get(0)); StreamingOutput<OutputTuple> output = getOutput(0);OutputTuple outputTuple = output.newTuple();booleanreject= false;//read the source tupleString value =tuple.getString("inp");if ((value == null)) {throw(new Exception("Input is null"));} else {outputTuple.setString("TransliteratedText",toBaseCharacters(t.transliterate(value.toString())));}output.submit(outputTuple);}catch(Exception e){}.....