最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JAVA將中文轉換為拼音簡單實現(xiàn)方法

 更新時間:2024年10月15日 09:29:04   作者:天天都要早睡  
拼音轉換是中文處理的常見需求,TinyPinyin、HanLP、pinyin4j是常用的本地拼音轉換庫,各有特點,開發(fā)者可根據(jù)具體需求選擇合適的拼音轉換工具,需要的朋友可以參考下

一、TinyPinyin

TinyPinyin 是一個輕量級且高效的拼音轉換庫。它適用于Android和Java項目。

TinyPinyin 是一個輕量級且高效的拼音轉換庫,特別適用于移動設備和需要高性能的場景。它采用了較為簡單的實現(xiàn)方式,性能較好。

  • 依賴
<dependency>
    <groupId>com.github.promeg</groupId>
    <artifactId>tinypinyin</artifactId>
    <version>2.0.3</version>
</dependency>
  • 示例
import com.github.promeg.pinyinhelper.Pinyin;

public class ChineseToPinyin {
    public static void main(String[] args) {
        String chinese = "你好,世界!";
        System.out.println("Chinese: " + chinese);
        System.out.println("Pinyin: " + convertToPinyin(chinese));
    }

    public static String convertToPinyin(String chinese) {
        return Pinyin.toPinyin(chinese, "");
    }
}

二、HanLP

HanLP 是一個功能強大的自然語言處理庫,支持中文分詞、詞性標注、命名實體識別等功能,同時也支持拼音轉換。

HanLP 是一個功能強大的自然語言處理庫,支持多種NLP任務,包括拼音轉換。雖然功能強大,但其整體性能可能會受到庫中其他功能的影響,加載時間和內存占用可能較高。不過,HanLP的拼音轉換部分效率也不錯,適合需要多功能NLP處理的場景。

  • 依賴
<dependency>
    <groupId>com.hankcs</groupId>
    <artifactId>hanlp</artifactId>
    <version>1.7.8</version>
</dependency>
  • 示例
import com.hankcs.hanlp.HanLP;

public class ChineseToPinyin {
    public static void main(String[] args) {
        String chinese = "你好,世界!";
        System.out.println("Chinese: " + chinese);
        System.out.println("Pinyin: " + convertToPinyin(chinese));
    }

    public static String convertToPinyin(String chinese) {
        return HanLP.convertToPinyinString(chinese, " ", false);
    }
}

三、pinyin4j

Pinyin4j 是一個經(jīng)典的拼音轉換庫,功能全面,但其性能可能不如TinyPinyin。對于大規(guī)模文本處理,Pinyin4j的性能可能稍遜一籌。

  • 依賴
<dependency>
    <groupId>com.belerweb</groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.0</version>
</dependency>
  • 示例
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;

public class ChineseToPinyin {
    public static void main(String[] args) {
        String chinese = "你好,世界!";
        System.out.println("Chinese: " + chinese);
        System.out.println("Pinyin: " + convertToPinyin(chinese));
    }

    public static String convertToPinyin(String chinese) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        StringBuilder pinyin = new StringBuilder();

        for (char c : chinese.toCharArray()) {
            try {
                if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) { // Check if the character is a Chinese character
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if (pinyinArray != null) {
                        pinyin.append(pinyinArray[0]); // Append the first pinyin result
                    }
                } else {
                    pinyin.append(c); // Append non-Chinese characters as is
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        }

        return pinyin.toString();
    }
}

四、Google Translate API

如果需要更加準確的拼音轉換并且不介意使用網(wǎng)絡服務,可以使用Google Translate API。不過,這種方法需要進行網(wǎng)絡請求,并且可能需要API Key。需要申請API Key,并且可能會產(chǎn)生費用。

Google Translate API 是一個在線服務,性能主要受網(wǎng)絡速度和API響應時間的影響。雖然準確性較高,但因為需要進行網(wǎng)絡請求,效率通常不如本地庫。偽代碼如下:

import com.google.cloud.translate.Translate;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;

public class ChineseToPinyin {
    public static void main(String[] args) {
        String chinese = "你好,世界!";
        System.out.println("Chinese: " + chinese);
        System.out.println("Pinyin: " + convertToPinyin(chinese));
    }

    public static String convertToPinyin(String chinese) {
        Translate translate = TranslateOptions.getDefaultInstance().getService();
        Translation translation = translate.translate(
            chinese,
            Translate.TranslateOption.targetLanguage("zh-CN"),
            Translate.TranslateOption.format("text")
        );
        return translation.getTranslatedText();
    }
}

簡單基準測試

import com.github.promeg.pinyinhelper.Pinyin;
import com.hankcs.hanlp.HanLP;
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;

public class PinyinBenchmark {
    public static void main(String[] args) {
        String chinese = "中華人民共和國,歡迎您!";

        long startTime, endTime;

        // TinyPinyin
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            Pinyin.toPinyin(chinese, "");
        }
        endTime = System.currentTimeMillis();
        System.out.println("TinyPinyin: " + (endTime - startTime) + " ms");

        // HanLP
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            HanLP.convertToPinyinString(chinese, " ", false);
        }
        endTime = System.currentTimeMillis();
        System.out.println("HanLP: " + (endTime - startTime) + " ms");

        // Pinyin4j
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            convertUsingPinyin4j(chinese);
        }
        endTime = System.currentTimeMillis();
        System.out.println("Pinyin4j: " + (endTime - startTime) + " ms");
    }

    public static String convertUsingPinyin4j(String chinese) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        StringBuilder pinyin = new StringBuilder();

        for (char c : chinese.toCharArray()) {
            try {
                if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if (pinyinArray != null) {
                        pinyin.append(pinyinArray[0]);
                    }
                } else {
                    pinyin.append(c);
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        }

        return pinyin.toString();
    }
}

總結 

到此這篇關于JAVA將中文轉換為拼音簡單實現(xiàn)的文章就介紹到這了,更多相關JAVA中文轉換為拼音內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 探究Java中Integer緩沖區(qū)底層原理

    探究Java中Integer緩沖區(qū)底層原理

    本文將會給大家講一講Integer這個包裝類的底層原理。在現(xiàn)在的就業(yè)環(huán)境下,我們需要知其然,還要知其所以然,才能更好地滿足就業(yè)需求,感興趣的小伙伴可以參考閱讀
    2023-05-05
  • maven依賴的version聲明控制方式

    maven依賴的version聲明控制方式

    這篇文章主要介紹了maven依賴的version聲明控制方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Idea中Springboot熱部署無效問題解決

    Idea中Springboot熱部署無效問題解決

    這篇文章主要介紹了Idea中Springboot熱部署無效問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • java實現(xiàn)多數(shù)據(jù)源切換方式

    java實現(xiàn)多數(shù)據(jù)源切換方式

    本文介紹實現(xiàn)多數(shù)據(jù)源切換的四步方法:導入依賴、配置文件、啟動類注解、使用@DS標記mapper和服務層,通過注解實現(xiàn)數(shù)據(jù)源動態(tài)切換,適用于實際開發(fā)中的多數(shù)據(jù)源場景
    2025-08-08
  • SpringBoot?整合?ShardingSphere4.1.1實現(xiàn)分庫分表功能

    SpringBoot?整合?ShardingSphere4.1.1實現(xiàn)分庫分表功能

    ShardingSphere是一套開源的分布式數(shù)據(jù)庫中間件解決方案組成的生態(tài)圈,它由Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar(計劃中)這3款相互獨立的產(chǎn)品組成,本文給大家介紹SpringBoot?整合?ShardingSphere4.1.1實現(xiàn)分庫分表,感興趣的朋友一起看看吧
    2023-12-12
  • 淺析Java中如何優(yōu)雅地處理null值

    淺析Java中如何優(yōu)雅地處理null值

    這篇文章主要為大家詳細介紹了如何結合 Lambda 表達式和 Optional,讓 Java 更優(yōu)雅地處理 null 值,感興趣的小伙伴可以跟隨小編一起學習一下
    2025-04-04
  • Java多線程 ReentrantLock互斥鎖詳解

    Java多線程 ReentrantLock互斥鎖詳解

    這篇文章主要介紹了Java多線程 ReentrantLock互斥鎖詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • 詳解Spring boot上配置與使用mybatis plus

    詳解Spring boot上配置與使用mybatis plus

    這篇文章主要介紹了詳解Spring boot上配置與使用mybatis plus,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • 在SpringBoot中使用MongoDB完成數(shù)據(jù)存儲

    在SpringBoot中使用MongoDB完成數(shù)據(jù)存儲

    本文主要介紹了在SpringBoot中如惡化使用MongoDB完成數(shù)據(jù)存儲,接下來這篇我們將圍繞MongoDB進行,MongoDB是一個開源的,面向文檔的NoSQL數(shù)據(jù)庫管理系統(tǒng),使用類似JSON的BSON(二進制JSON)格式來存儲數(shù)據(jù),具有靈活的數(shù)據(jù)模型和強大的查詢功能,需要的朋友可以參考下
    2023-11-11
  • Java同時處理多個數(shù)據(jù)的常見方法

    Java同時處理多個數(shù)據(jù)的常見方法

    在Java中,同時處理多個數(shù)據(jù)通常涉及多線程、并發(fā)編程或異步編程,這里我將提供一個使用多線程的示例,因為多線程是處理多個數(shù)據(jù)并行的常見方式,文中有詳細的代碼示例供大家參考,需要的朋友可以參考下
    2024-11-11

最新評論

南岸区| 安阳县| 南部县| SHOW| 张家界市| 富裕县| 吉水县| 衡南县| 钦州市| 乐业县| 门源| 乌海市| 建宁县| 滨海县| 洞头县| 南京市| 安溪县| 饶阳县| 府谷县| 秀山| 蛟河市| 涿鹿县| 秦安县| 沙湾县| 卢龙县| 谢通门县| 永宁县| 子长县| 拉孜县| 盐池县| 固镇县| 吴堡县| 海宁市| 丹巴县| 明星| 驻马店市| 二连浩特市| 当涂县| 德昌县| 景宁| 罗定市|