language-detection项目源码发布在Google code 上,可以在如下地址看到项目介绍和资源下载:
https://code.google.com/archive/p/language-detection/
Java使用示例代码:
import com.cybozu.labs.langdetect.Detector;
import com.cybozu.labs.langdetect.DetectorFactory;
import com.cybozu.labs.langdetect.LangDetectException;
public class LangDetectTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
init("E:\\workspace\\langdetect\\profiles2");
} catch (LangDetectException e) {
e.printStackTrace();
}
// 打印出支持的语言列表
System.out.println(DetectorFactory.getLangList());
// 设置语言优先级
// HashMap priorMap = new HashMap();
// priorMap.put("zh-cn", 1000D);
// priorMap.put("en", 0.10D);
Detector detect;
try {
detect = DetectorFactory.create();
// detect.setPriorMap(priorMap);
// 设置文本
detect.append(T3);
// detect.append(T2);
// 解析,得到语言
String lang = detect.detect();
System.out.println(lang);
// 打印出解析的语言比例,如:[zh-cn:0.5714248723691703, en:0.4285738367405386]
System.out.println(detect.getProbabilities());
} catch (LangDetectException e) {
if (e.getMessage() != null && "no features in text".equals(e.getMessage().trim())) {
System.out.println("无法识别该语言!");
}
e.printStackTrace();
}
}
/**
* 初始化
*
* @param path
*/
private static void init(String path) throws LangDetectException {
// 设置语法文件目录
DetectorFactory.loadProfile(path);
// (可选)同一内容每次检查结果可能存在变化,这里设置随机数种子为0,使每次结果保持不变
DetectorFactory.setSeed(0);
}
static String T1 = "Hello! 欢迎来到我家,我家住在北京市昌平区!欢迎下次再来我家玩,慢走!";
static String T2 = "Hello! 欢迎你来这!";
static String T3 = "Hello! 欢迎来到我家, 我家住在";
static String T4 = "Votre abonnement Shine Voice va bientôt expirer, veuillez repondre par Y pour le renouveler ou ne rien faire pour l'annuler.";
static String T5 = "Votre abonnement.";
static String T6 = "欢迎来到我家";
static String T7 = "Hello! Votre.";
static String AA = "=============================================";
}
更新信息可以到官网上查看API,或在wiki上查看帮助文档。
wiki:
https://code.google.com/archive/p/language-detection/wikis
---------------------
作者:毛衣
来源:CSDN
原文:https://blog.csdn.net/qq354960984/article/details/51306165
版权声明:本文为博主原创文章,转载请附上博文链接!