我尝试了this库,在其中一篇关于堆栈溢出的帖子中提出,
我已将lib的jar添加到我的构建路径中,但我无法使用语言的配置文件初始化DetectorFactory类。
这是处理检测的类,如其中一个样本所示:
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();
}
}
所有语言配置文件都存储在myProject / profiles下。 尝试实例化该类会使我的应用程序崩溃而没有任何有用的消息到logcat
调用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);
}
答案 0 :(得分:0)
将LanguageDetector
中的方法更改为静态:
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();
}
}
使用如下:
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
}