- 2024-02-19 15:58:04
- 6109 热度
- 0 评论
Pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换。拼音输出格式可以定制。
Support Chinese character (both Simplified and Tranditional) to most popular Pinyin systems, including Hanyu Pinyin, Tongyong Pinyin, Wade-Giles, MPS2, Yale and Gwoyeu Romatzyh. Support multiple pronounciations and customized output.
pinyin4j是一个支持将中文转换到拼音的Java开源类库。
POM引入:
<dependency> <groupId>net.sourceforge.pinyin4j</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency>
项目地址:
https://sourceforge.net/projects/pinyin4j/
实用示例,如果一个汉字是多音字,那么将采取组装所有可能的组合:
package com.javacui.lucene.pinyin; import java.util.HashSet; import java.util.Set; import com.alibaba.fastjson.JSON; 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.HanyuPinyinVCharType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; /** * 打印中文拼音的所有组合 */ public class Pinyin4j { public static void main(String[] args) { String cnStr = "长沙市重庆市"; System.out.println(makePinyin(cnStr)); } /** * 字符串凑集转换字符串(逗号分隔) */ public static String makeStringByStringSet(Set<String> stringSet) { StringBuilder str = new StringBuilder(); int i = 0; for (String s : stringSet) { if (i == stringSet.size() - 1) { str.append(s); } else { str.append(s + ","); } i++; } return str.toString().toLowerCase(); } /** * 获取拼音凑集 */ public static Set<String> getPinyin(String src) { if (src != null && !src.trim().equalsIgnoreCase("")) { char[] srcChar; srcChar = src.toCharArray(); // 汉语拼音格局输出类 HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat(); // 输出设置,大小写,音标体式格式等 hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); // HanyuPinyinToneType.WITH_TONE_NUMBER 用数字表示声调,例如:liu2 // HanyuPinyinToneType.WITHOUT_TONE 无声调表示,例如:liu // HanyuPinyinToneType.WITH_TONE_MARK 用声调符号表示,例如:liú hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V); String[][] temp = new String[src.length()][]; for (int i = 0; i < srcChar.length; i++) { char c = srcChar[i]; // 是中文或者a-z或者A-Z转换拼音(根据需求来) if (String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")) { try { temp[i] = PinyinHelper.toHanyuPinyinStringArray(srcChar[i], hanYuPinOutputFormat); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } else if (((int) c >= 65 && (int) c <= 90) || ((int) c >= 97 && (int) c <= 122)) { temp[i] = new String[] { String.valueOf(srcChar[i]) }; } else { temp[i] = new String[] { "" }; } } System.out.println("中文单词分解:"+JSON.toJSONString(temp)); String[] pingyinArray = Exchange(temp); Set<String> pinyinSet = new HashSet<String>(); for (int i = 0; i < pingyinArray.length; i++) { pinyinSet.add(pingyinArray[i]); } return pinyinSet; } return null; } /** * 递归 */ public static String[] Exchange(String[][] strJaggedArray) { String[][] temp = DoExchange(strJaggedArray); return temp[0]; } /** * 递归 */ private static String[][] DoExchange(String[][] strJaggedArray) { int len = strJaggedArray.length; if (len >= 2) { int len1 = strJaggedArray[0].length; int len2 = strJaggedArray[1].length; int newlen = len1 * len2; String[] temp = new String[newlen]; int Index = 0; for (int i = 0; i < len1; i++) { for (int j = 0; j < len2; j++) { temp[Index] = strJaggedArray[0][i] + strJaggedArray[1][j]; Index++; } } String[][] newArray = new String[len - 1][]; for (int i = 2; i < len; i++) { newArray[i - 1] = strJaggedArray[i]; } newArray[0] = temp; return DoExchange(newArray); } else { return strJaggedArray; } } public static String makePinyin(String str){ Set<String> set = getPinyin(str); System.out.println("中文单词组合:"+JSON.toJSONString(set)); return makeStringByStringSet(set); } }
请根据实际场景,编写解析代码。
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Spring Cloud(82)
- Java(82)
- Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- MVC(22)
- JDBC(22)
- IDEA(22)
- mongoDB(22)
- Web(21)
- CLI(20)
- SpringMVC(19)
- Alibaba(19)
- Docker(17)
- SpringBoot(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- ORA(15)
- JPA(15)
- Apache(15)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Pro(13)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Data(12)
- JSON(12)
- OAuth2(12)
- Myeclipse(11)
- stream(11)
- int(11)
- not(10)
- Bug(10)
- ast(9)
- maven(9)
- Map(9)
- Hystrix(9)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- HTML(7)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- windows(7)
- too(7)
- RabbitMQ(6)
- star(6)
- and(6)
- Excel(6)
- pushlet(6)
- Log4J(6)
- apt(6)
- Freemarker(6)
- read(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- Server(6)
- nginx(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- JWT(5)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- script(5)
- Syntaxhighlighter(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)