1. TinyPinyin
TinyPinyin is a lightweight andHigh efficiencyPinyin conversion library. It works for Android and Java projects.
TinyPinyin is a lightweight and efficient pinyin conversion library, especially suitable for mobile devices and scenarios where high performance is required. It adopts a relatively simple implementation method and has better performance.
- rely
<dependency> <groupId></groupId> <artifactId>tinypinyin</artifactId> <version>2.0.3</version> </dependency>
- Example
import ; public class ChineseToPinyin { public static void main(String[] args) { String chinese = "Hello World!"; ("Chinese: " + chinese); ("Pinyin: " + convertToPinyin(chinese)); } public static String convertToPinyin(String chinese) { return (chinese, ""); } }
2. HanLP
HanLP is a powerful natural language processing library that supports Chinese word segmentation, part-of-speech annotation, naming entity recognition and other functions, and also supports pinyin conversion.
HanLP is a powerful natural language processing library that supports a variety of NLP tasks, including pinyin conversion. Although powerful, its overall performance may be affected by other features in the library, and may have high loading time and memory footprint. However, HanLP's pinyin conversion part is also very efficient, suitable for scenarios where multifunctional NLP processing is required.
- rely
<dependency> <groupId></groupId> <artifactId>hanlp</artifactId> <version>1.7.8</version> </dependency>
- Example
import ; public class ChineseToPinyin { public static void main(String[] args) { String chinese = "Hello World!"; ("Chinese: " + chinese); ("Pinyin: " + convertToPinyin(chinese)); } public static String convertToPinyin(String chinese) { return (chinese, " ", false); } }
3. pinyin4j
Pinyin4j is a classic pinyin conversion library with comprehensive functions, but its performance may not be as good as TinyPinyin. For large-scale text processing, Pinyin4j may perform slightly worse.
- rely
<dependency> <groupId></groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency>
- Example
import .; import .; import .; import .; import .; public class ChineseToPinyin { public static void main(String[] args) { String chinese = "Hello World!"; ("Chinese: " + chinese); ("Pinyin: " + convertToPinyin(chinese)); } public static String convertToPinyin(String chinese) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); (); (HanyuPinyinToneType.WITHOUT_TONE); StringBuilder pinyin = new StringBuilder(); for (char c : ()) { try { if ((c).matches("[\\u4E00-\\u9FA5]+")) { // Check if the character is a Chinese character String[] pinyinArray = (c, format); if (pinyinArray != null) { (pinyinArray[0]); // Append the first pinyin result } } else { (c); // Append non-Chinese characters as is } } catch (BadHanyuPinyinOutputFormatCombination e) { (); } } return (); } }
4. Google Translate API
If you need more accurate pinyin conversion and don't mind using web services, you can use the Google Translate API. However, this approach requires network requests and may require API Key. Apply for API Key and fees may be incurred.
The Google Translate API is an online service whose performance is mainly affected by network speed and API response time. Although it is more accurate, it is usually less efficient than the local library because of network requests.The pseudo-code is as follows:
import ; import ; import ; public class ChineseToPinyin { public static void main(String[] args) { String chinese = "Hello World!"; ("Chinese: " + chinese); ("Pinyin: " + convertToPinyin(chinese)); } public static String convertToPinyin(String chinese) { Translate translate = ().getService(); Translation translation = ( chinese, ("zh-CN"), ("text") ); return (); } }
Simple benchmarking
import ; import ; import .; import .; import .; import .; import .; public class PinyinBenchmark { public static void main(String[] args) { String chinese = "Welcome to the People's *!"; long startTime, endTime; // TinyPinyin startTime = (); for (int i = 0; i < 10000; i++) { (chinese, ""); } endTime = (); ("TinyPinyin: " + (endTime - startTime) + " ms"); // HanLP startTime = (); for (int i = 0; i < 10000; i++) { (chinese, " ", false); } endTime = (); ("HanLP: " + (endTime - startTime) + " ms"); // Pinyin4j startTime = (); for (int i = 0; i < 10000; i++) { convertUsingPinyin4j(chinese); } endTime = (); ("Pinyin4j: " + (endTime - startTime) + " ms"); } public static String convertUsingPinyin4j(String chinese) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); (); (HanyuPinyinToneType.WITHOUT_TONE); StringBuilder pinyin = new StringBuilder(); for (char c : ()) { try { if ((c).matches("[\\u4E00-\\u9FA5]+")) { String[] pinyinArray = (c, format); if (pinyinArray != null) { (pinyinArray[0]); } } else { (c); } } catch (BadHanyuPinyinOutputFormatCombination e) { (); } } return (); } }
Summarize
This is the article about the simple implementation of JAVA converting Chinese to Pinyin. For more related content on converting JAVA into Pinyin, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!