Преглед на файлове

通用工具包加入拼音转化类

gaodm преди 6 години
родител
ревизия
2e528ce5ec
променени са 3 файла, в които са добавени 80 реда и са изтрити 2 реда
  1. 7 0
      common/pom.xml
  2. 1 2
      common/src/main/java/com/diagbot/util/BeanUtil.java
  3. 72 0
      common/src/main/java/com/diagbot/util/Cn2SpellUtil.java

+ 7 - 0
common/pom.xml

@@ -103,6 +103,13 @@
             <scope>compile</scope>
         </dependency>
 
+        <dependency>
+            <groupId>com.belerweb</groupId>
+            <artifactId>pinyin4j</artifactId>
+            <version>2.5.1</version>
+            <scope>compile</scope>
+        </dependency>
+
         <!--<dependency>-->
             <!--<groupId>com.fasterxml.jackson.core</groupId>-->
             <!--<artifactId>jackson-core</artifactId>-->

+ 1 - 2
common/src/main/java/com/diagbot/util/BeanUtil.java

@@ -10,8 +10,7 @@ import java.util.List;
 /**
  * @Description: 对象转换工具类
  * @author: gaodm
- * @date: 2017/12/28 10:16
- * @version: V1.0
+ * @time: 2018/12/14 14:21
  */
 public class BeanUtil {
     /**

+ 72 - 0
common/src/main/java/com/diagbot/util/Cn2SpellUtil.java

@@ -0,0 +1,72 @@
+package com.diagbot.util;
+
+import net.sourceforge.pinyin4j.PinyinHelper;
+import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
+import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
+import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
+
+/**
+ * @Description: 汉字转拼音
+ * @author: gaodm
+ * @time: 2018/12/14 14:20
+ */
+public class Cn2SpellUtil {
+
+    /**
+     * 汉字转换位汉语拼音首字母,英文字符不变
+     *
+     * @param chines 汉字
+     * @return 拼音
+     */
+    public static String converterToFirstSpell(String chines) {
+        String pinyinName = "";
+        char[] nameChar = chines.toCharArray();
+        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
+        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
+        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
+        for (int i = 0; i < nameChar.length; i++) {
+            if (nameChar[i] > 128) {
+                try {
+                    pinyinName +=
+                            PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            } else {
+                pinyinName += nameChar[i];
+            }
+        }
+        return pinyinName;
+    }
+
+    /**
+     * 汉字转换位汉语拼音,英文字符不变
+     *
+     * @param chines 汉字
+     * @return 拼音
+     */
+    public static String converterToSpell(String chines) {
+        String pinyinName = "";
+        char[] nameChar = chines.toCharArray();
+        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
+        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
+        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
+        for (int i = 0; i < nameChar.length; i++) {
+            if (nameChar[i] > 128) {
+                try {
+                    pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            } else {
+                pinyinName += nameChar[i];
+            }
+        }
+        return pinyinName;
+    }
+
+    public static void main(String[] args) {
+        System.out.println(converterToFirstSpell("欢迎来到Java世界"));
+        System.out.println(converterToSpell("欢迎来到Java世界"));
+    }
+}