使用包 language-detector
对应java代码
package LanguageProfile; /* * 1 af 南非荷兰语 2 an 阿拉贡语 3 ar 阿拉伯语 4 ast 阿斯图里亚斯语 5 be 白俄罗斯语 6 br 布兰顿语 7 ca 加泰罗尼亚语 8 bg 保加利亚语 9 bn 孟加拉语 10 cs 捷克语 11 cy 威尔士语 12 da 丹麦语 13 de 德语 14 el 希腊语 15 en 英语 16 es 西班牙语 17 et 爱沙尼亚语 18 eu 巴斯克语 19 fa 波斯语 20 fi 芬兰语 21 fr 法语 22 ga 爱尔兰语 23 gl 加利西亚语 24 gu 古吉拉特语 25 he 希伯来语 26 hi 印地语 27 hr 克罗地亚语 28 ht 海地语 29 hu 匈牙利语 30 id 印尼语 31 is 冰岛语 32 it 意大利语 33 ja 日语 34 km 高棉语 35 kn 卡纳达语 36 ko 韩国语 37 lt 立陶宛语 38 lv 拉脱维亚语 39 mk 马其顿语 40 ml 马拉雅拉姆语 41 mr 马拉语 42 ms 马来语 43 mt 马耳他语 44 ne 尼泊尔语 45 nl 荷兰语 46 no 挪威语 47 oc 奥克语 48 pa 旁遮普语 49 pl 波兰语 50 pt 葡萄牙语 51 ro 罗马尼亚语 52 ru 俄语 53 sk 斯洛伐克语 54 sl 斯洛文尼亚语 55 so 索马里语 56 sq 阿尔巴尼亚语 57 sr 塞尔维亚语 58 sv 瑞典语 59 sw 斯瓦希里语 60 ta 泰米尔语 61 te 泰卢固语 62 th 泰语 63 tl 他加禄语 64 tr 土耳其语 65 uk 乌克兰语 66 ur 乌尔都语 67 vi 越南语 68 wa 瓦隆语 69 yi 意第绪语 70 zh-cn 简体中文 71 zh-tw 繁体中文 * */ import com.google.common.base.Optional; import com.optimaize.langdetect.LanguageDetector; import com.optimaize.langdetect.LanguageDetectorBuilder; import com.optimaize.langdetect.i18n.LdLocale; import com.optimaize.langdetect.ngram.NgramExtractors; import com.optimaize.langdetect.profiles.LanguageProfile; import com.optimaize.langdetect.profiles.LanguageProfileReader; import com.optimaize.langdetect.text.CommonTextObjectFactories; import com.optimaize.langdetect.text.TextObject; import com.optimaize.langdetect.text.TextObjectFactory; import java.io.IOException; import java.util.List; public class Language { public static List<LanguageProfile> languageProfiles; public static LanguageDetector languageDetector; public static TextObjectFactory textObjectFactory; public static void InitLanguage() throws IOException { //加载所有内置语种 languageProfiles = new LanguageProfileReader().readAllBuiltIn(); // 创建识别器 languageDetector = LanguageDetectorBuilder.create(NgramExtractors.standard()).withProfiles(languageProfiles).build(); // 创建文本对象工厂 textObjectFactory = CommonTextObjectFactories.forDetectingOnLargeText(); } public static void main(String[] args) throws IOException { Language.InitLanguage(); if(Language.IsJp("โซรินอยด์ เพิ่มแรงดันน้ำมัน")){ //System.out.println("是日语"); } else{ // System.out.println("不是日语"); } } public static String getStrTip(String str){ TextObject textObject = textObjectFactory.forText(str); Optional<LdLocale> lang = languageDetector.detect(textObject); String result ="错误"; if (!lang.isPresent()) { return result;//无法识别对应的内容 } return lang.get().getLanguage(); } public static boolean IsJp(String str) throws IOException { // 识别 TextObject textObject = textObjectFactory.forText(str); Optional<LdLocale> lang = languageDetector.detect(textObject); if (!lang.isPresent()) { //无法识别对应的内容 return false; } System.out.println(lang.get().getLanguage()); if(lang.get().toString().equals("ar")){ return true; } return false; } }