系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 网络编程 > 其它综合 > 详细页面

Java实战之城市多音字处理

时间:2021-04-28来源:www.pcxitongcheng.com作者:电脑系统城

一、需求

对城市名称转化为拼音的时候,当遇到多音字城市的时候,转化拼音就不是我们想要的了。

使用  pinyin4j 无法直接解决这个问题。 。网上有很多维护多音字信息的,觉得麻烦。

如:

1
2
3
长沙 ====>"zhangsha" 
厦门===>"shamen" 
重庆===>"zhongqing"

 

二、导入 jpinyin

版本自选

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.github.stuxuhai/jpinyin -->
        <dependency>
            <groupId>com.github.stuxuhai</groupId>
            <artifactId>jpinyin</artifactId>
            <version>1.1.8</version>
        </dependency>

 

三、直接创建工具类(不需要其他操作)

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import com.github.stuxuhai.jpinyin.ChineseHelper;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
  
/**
 * @Description:
 * @Date: 2021/4/27 16:26
 * @Author: luch
 * @Version: 1.0
 **/
public class ChangeToPinYinJPinYin {
  
  
    /**
     * 转换为有声调的拼音字符串
     *
     * @param pinYinStr 汉字
     * @return 有声调的拼音字符串
     */
    public static String changeToMarkPinYin(String pinYinStr) {
  
        String tempStr = null;
  
        try {
            tempStr = PinyinHelper.convertToPinyinString(pinYinStr, " ", PinyinFormat.WITH_TONE_MARK);
  
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempStr;
  
    }
  
  
    /**
     * 转换为数字声调字符串
     *
     * @param pinYinStr 需转换的汉字
     * @return 转换完成的拼音字符串
     */
    public static String changeToNumberPinYin(String pinYinStr) {
  
        String tempStr = null;
  
        try {
            tempStr = PinyinHelper.convertToPinyinString(pinYinStr, " ", PinyinFormat.WITH_TONE_NUMBER);
        } catch (Exception e) {
            e.printStackTrace();
        }
  
        return tempStr;
  
    }
  
    /**
     * 转换为不带音调的拼音字符串
     *
     * @param pinYinStr 需转换的汉字
     * @return 拼音字符串
     */
    public static String changeToTonePinYin(String pinYinStr) {
  
        String tempStr = null;
  
        try {
            tempStr = PinyinHelper.convertToPinyinString(pinYinStr, "", PinyinFormat.WITHOUT_TONE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempStr;
  
    }
  
    /**
     * 转换为每个汉字对应拼音首字母字符串
     *
     * @param pinYinStr 需转换的汉字
     * @return 拼音字符串
     */
    public static String changeToGetShortPinYin(String pinYinStr) {
  
        String tempStr = null;
  
        try {
            tempStr = PinyinHelper.getShortPinyin(pinYinStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempStr;
  
    }
  
    /**
     * 检查汉字是否为多音字
     *
     * @param pinYinStr 需检查的汉字
     * @return true 多音字,false 不是多音字
     */
    public static boolean checkPinYin(char pinYinStr) {
  
        boolean check = false;
        try {
            check = PinyinHelper.hasMultiPinyin(pinYinStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }
  
    /**
     * 简体转换为繁体
     *
     * @param pinYinStr
     * @return
     */
    public static String changeToTraditional(String pinYinStr) {
  
        String tempStr = null;
        try {
            tempStr = ChineseHelper.convertToTraditionalChinese(pinYinStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempStr;
  
    }
  
    /**
     * 繁体转换为简体
     *
     * @param pinYinSt
     * @return
     */
    public static String changeToSimplified(String pinYinSt) {
  
        String tempStr = null;
  
        try {
            tempStr = ChineseHelper.convertToSimplifiedChinese(pinYinSt);
        } catch (Exception e) {
            e.printStackTrace();
        }
  
        return tempStr;
  
    }
  
  
  
}

 

四、直接测试代码

1
2
3
4
5
6
7
8
9
10
11
public static void main(String[] args) {
        String str = "长沙市";
        System.out.println("转换为有声调的拼音字符串:"+changeToMarkPinYin(str));
        System.out.println("转换为不带音调的拼音字符串:"+changeToTonePinYin(str));
  
        String strFanTi="誰是程序員";
        System.out.println("繁体转换为简体:"+changeToSimplified(strFanTi));
        System.out.println("重"+"重是否是多音字:"+checkPinYin('重'));
        System.out.println("厦"+"是否是多音字:"+checkPinYin('厦'));
        System.out.println("鼠"+"是否是多音字:"+checkPinYin('鼠'));
    }

输出结果,下面不同字的中间是否有空格,这个可以自己设置的

 

五、源码分析

Java技术迷

 

5.1 字典对应信息

分别是:从上到下分别是

1.繁体-简体对应表

2.多音字

3.带音标的拼音

 

5.2 6个处理类

 

5.3 处理模式

点开

1 com.github.stuxuhai.jpinyin.PinyinResource#getPinyinResource

有三个方法分别加载了这几个资源

然后通过

1 com.github.stuxuhai.jpinyin.ChineseHelper

 通过一个键值对获取。加载处理而已。

到此这篇关于Java实战之城市多音字处理的文章就介绍到这了,

分享到:

相关信息

  • C++ AVLTree高度平衡的二叉搜索树深入分析

    一、AVL树的概念 二、AVL树节点的定义 三、AVL树的插入 四、AVL树的旋转 1.左单旋 2.右单旋 3.左右双旋 4.右左双旋 五、进行验证 六、AVLTree的性能...

    2023-03-09

  • idea构建web项目的超级详细教程

    1、idea构建web项目 1、新建一个空项目 2、新建java模块,名为webDemo1 3、选择webDemo1右键,选择Add Framework Support 4、在WEB-INF下新建文件夹classes和lib 5、打开项目结构(Project Structure) 6、项目配置 7、模...

    2023-03-09

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载