当前位置: 首页 > news >正文

kettle通过java步骤获取汉字首拼

kettle通过java步骤获取汉字首拼

用途描述

一组数据,需要获取汉字首拼后,输出;

实现效果

在这里插入图片描述

添加jar包

pinyin4j-2.5.0.jar

自定义常量数据

在这里插入图片描述
在这里插入图片描述

Java代码

在这里插入图片描述

完整代码:

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;String nameField;
String pyField;public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{// Let's look up parameters only once for performance reason.	// 	if (first) {	nameField = "name";//getParameter("name");	pyField = "py" ; // getParameter("py");	first=false;	}// First, get a row from the default input hop//Object[] r = getRow();// If the row object is null, we are done processing.//if (r == null) {setOutputDone();return false;	}// It is always safest to call createOutputRow() to ensure that your output row's Object[] is large// enough to handle any new fields you are creating in this step.//Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());String name = get(Fields.In, nameField).getString(r);	// Set the value in the output field	//String py = toFirstChar(name);	get(Fields.Out, pyField).setValue(outputRow, py);	// putRow will send the row on to the default output hop.	//putRow(data.outputRowMeta, outputRow);return true;
}/*** 获取字符串拼音的第一个字母*/
public static String toFirstChar(String chinese){String pinyinStr = "";char[] newChar = chinese.toCharArray();  //转为单个字符HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);for (int i = 0; i < newChar.length; i++) {if (newChar[i] > 128) {try {pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}}else{pinyinStr += newChar[i];}}return pinyinStr;
}/*** 汉字转为拼音*/
public static String toPinyin(String chinese){String pinyinStr = "";char[] newChar = chinese.toCharArray();HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);for (int i = 0; i < newChar.length; i++) {if (newChar[i] > 128) {try {pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}}else{pinyinStr += newChar[i];}}return pinyinStr;
}

–END–

http://www.lryc.cn/news/156560.html

相关文章:

  • Conformer: Local Features Coupling Global Representationsfor Visual Recognition
  • java8-Stream流常用API
  • React 任务调度
  • 小白开始学习C++
  • SpringMVC入门的注解、参数传递、返回值和页面跳转---超详细教学
  • 【复习socket】每天40min,我们一起用70天稳扎稳打学完《JavaEE初阶》——28/70 第二十八天
  • vue2踩坑之项目:生成二维码使用vue-print-nb打印二维码
  • 【iVX】十五分钟制作一款小游戏,iVX真有怎么神?
  • SpringMVC常用注解、参数传递、返回值
  • 新公司第一次上架新APP需要提前准备哪些材料?
  • 『C语言进阶』指针进阶(一)
  • 2605. 从两个数字数组里生成最小数字(Java)
  • 深度解析 PostgreSQL Protocol v3.0(一)
  • Mysql中having语句与where语句的用法与区别
  • 基于qt软件的网上聊天室软件
  • 本是同根生-双数据库集群keepalived virtual_route_id冲突导致连接故障
  • 『力扣每日一题06』字符串中的第一个唯一字符
  • selenium鼠标操作方法
  • 医者无疆 | AI赋能大医精诚,医疗制药的进阶与突破
  • 使用 Hue 玩转 Amazon EMR(SparkSQL, Phoenix) 和 Amazon Redshift
  • Unity中神秘的Transform和transform(小写)的关系
  • 【LeetCode-中等题】78. 子集
  • 学习设计模式之代理模式,但是宝可梦
  • 自学Python01-创建文件写入内容
  • Qt —UDP通信QUdpSocket 简介 +案例
  • 五大类注解和方法注解详解
  • 机器人中的数值优化(十)——线性共轭梯度法
  • 数据结构与算法之贪心动态规划
  • 【网络编程】网络基础概念
  • 连接虚拟机报错 Could not connect to ‘192.168.xxx.xxx‘ (port 22): Connection failed.