Android的自动检测语言 [英] android auto-language detection
问题描述
我试过这库,在对职位之一建议堆栈溢出,
我添加的lib的jar到我的构建路径,但我不能初始化DetectorFactory类语言的配置文件。
这是处理类的检测,如在他们的样品之一提示:
类LanguageDetector {
公共无效的init(字符串profileDirectory)抛出LangDetectException {
DetectorFactory.loadProfile(profileDirectory);
}
公共字符串检测(字符串文本)抛出LangDetectException {
探测器探测器= DetectorFactory.create();
detector.append(文本);
返回detector.detect();
}
公众的ArrayList<语言和GT; detectLangs(字符串文本)抛出LangDetectException {
探测器探测器= DetectorFactory.create();
detector.append(文本);
返回detector.getProbabilities();
}
}
所有的语言文件会保存在myProject的/型材。
试图将类实例崩溃我的应用程序没有任何有用的消息logcat的
调用类():
@覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
super.onActivityCreated(savedInstanceState);
上下文= this.getActivity()getApplicationContext()。
/ * LanguageDetector探测器= NULL;
尝试{
detector.init(/摇摆/配置文件);
}赶上(LangDetectException E){
// TODO自动生成catch块
e.printStackTrace();
} * /
新GetDataTask()执行(上下文); }
更改 LanguageDetector
的方法静态的:
类LanguageDetector {
公共静态无效的init(字符串profileDirectory)抛出LangDetectException {
DetectorFactory.loadProfile(profileDirectory);
}
公共静态字符串检测(字符串文本)抛出LangDetectException {
探测器探测器= DetectorFactory.create();
detector.append(文本);
返回detector.detect();
}
公共静态的ArrayList<语言和GT; detectLangs(字符串文本)抛出LangDetectException {
探测器探测器= DetectorFactory.create();
detector.append(文本);
返回detector.getProbabilities();
}
}
和使用方法如下:
{尝试
LanguageDetector.init(/摇摆/配置文件); //< - 你确定的配置文件是在这个位置?
}赶上(LangDetectException E){
// TODO自动生成catch块
e.printStackTrace();
}字符串detectedLanguage = NULL;
尝试{
detectedLanguage = LanguageDetector.detect(在德语模具IST EIN Beispiel。);
}赶上(LangDetectException E){
// TODO自动生成catch块
e.printStackTrace();
}如果(detectedLanguage!= NULL){
//这里实现你的逻辑
}
I tried this library, suggested in one of the posts on stack overflow,
I've added the lib's jar to my build-path, but I'm not able to initialize DetectorFactory class with the languages' profiles.
this is the class handling the detection, as suggested in one of their samples:
class LanguageDetector {
public void init(String profileDirectory) throws LangDetectException {
DetectorFactory.loadProfile(profileDirectory);
}
public String detect(String text) throws LangDetectException {
Detector detector = DetectorFactory.create();
detector.append(text);
return detector.detect();
}
public ArrayList<Language> detectLangs(String text) throws LangDetectException {
Detector detector = DetectorFactory.create();
detector.append(text);
return detector.getProbabilities();
}
}
all languages profiles are stored under myProject/profiles. trying to instantiate the class crashes my app without any useful message to logcat
calling the class ():
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = this.getActivity().getApplicationContext();
/* LanguageDetector detector = null;
try {
detector.init("/waggle/profiles");
} catch (LangDetectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
new GetDataTask().execute(context);
}
Change the methods in LanguageDetector
to static:
class LanguageDetector {
public static void init(String profileDirectory) throws LangDetectException {
DetectorFactory.loadProfile(profileDirectory);
}
public static String detect(String text) throws LangDetectException {
Detector detector = DetectorFactory.create();
detector.append(text);
return detector.detect();
}
public static ArrayList<Language> detectLangs(String text) throws LangDetectException {
Detector detector = DetectorFactory.create();
detector.append(text);
return detector.getProbabilities();
}
}
And use as follows:
try {
LanguageDetector.init("/waggle/profiles"); // <-- Are you sure the profiles are at this location???
} catch (LangDetectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String detectedLanguage = null;
try {
detectedLanguage = LanguageDetector.detect("Dies ist ein Beispiel in Deutsch.");
} catch (LangDetectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (detectedLanguage != null) {
// Implement your logic here
}
这篇关于Android的自动检测语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!