Question

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);

    }
Was it helpful?

Solution

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top