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,拼音,chinese,format,System,startTime,import,endTime
From: https://blog.51cto.com/u_16271212/11944674