2

I am searching for a small example code to detect the language of a string in JAVA. For that i downloaded and imported the following GitHub Project: https://github.com/shuyo/language-detection

Unfortunately I am struggling reading the API and I don't know how to get my code to work. Help is very appreciated. Heres what i have so far. I get a NullPointerException because i dont know how to initialize the Detector properly. ny help is kindly appreciated.

import com.cybozu.labs.langdetect.*;

public class DetectLanguage {

    public static void main(String[] args) throws LangDetectException {

        String sample = "Comment vous appelez-vous?";   // french demo text
        Detector d = new Detector(null);                // initialize detector
        d.append(sample);
        System.out.println(d.detect());
    }
}
dcts
  • 1,479
  • 15
  • 34
  • Does it work? If not, what happens? – Kayaman Mar 08 '18 at 20:12
  • random click on the library, last updated 4 years ago... also I have no idea how that would be possible, probably this is some pseudo-smart guessing here – Eugene Mar 08 '18 at 20:18
  • @Kayaman the Initialization is not working. I get the following: Exception in thread "main" java.lang.NullPointerException at com.cybozu.labs.langdetect.Detector.(Detector.java:88) at selenium.updateLanguage.DetectLanguage.main(DetectLanguage.java:32) – dcts Mar 08 '18 at 20:24
  • 1
    You're passing a `null`, it wants a `DetectorFactory` parameter. At least make an effort to read the documentation and/or code. – Kayaman Mar 08 '18 at 20:35
  • as described in the previous comment, use the DetectorFactory. see their test https://github.com/shuyo/language-detection/blob/master/src/com/cybozu/labs/langdetect/DetectorTest.java – MarianP Mar 08 '18 at 20:36
  • 1
    hi @Kayaman. this is not a question about NPE, but why the NPE is happening. it would be good if you tried to be of help or ignore the question if you aren't willing to contribute meaningfully. – MarianP Mar 08 '18 at 20:38
  • @MarianP this is not a question at all. The duplicate explains why the (or any for that matter) `NPE` is happening. Reading the documentation is the responsibility of the person using the library. – Kayaman Mar 08 '18 at 20:41
  • Im sorry for not being able to understand the API. I am a java-newbie trying to just use this library, but i cant understand all aspects of it. If someone can provide me a sample code of how to detect the language of a given string this would be extremely helpful. I spend a lot of hours reading the API but i dont get it. Please help. – dcts Mar 09 '18 at 10:44
  • 1
    @Kayaman you completely miss the point of this website and it would be better if you switch to read only mode – MarianP Mar 09 '18 at 12:26
  • 1
    @MarianP not at all, as you can see from my points I've been a lot more helpful than you here. It's just that if you're new to programming and try to do something beyond your skills, it's not StackOverflow's job to tutor you. I uinderstand it can be frustrating, but "I'm new" is not an excuse. If you wish to tutor him about this library, by all means go ahead. This question just isn't suitable for SO. We don't provide tutoring with new libraries, let alone ones as old as this one. But hey, tell you what, I'll remove the dupe. Let's assume OP knows what an NPE is and how to fix it. – Kayaman Mar 09 '18 at 12:35
  • You pass ```null``` to the ```Detector(DetectorFactory factory)``` So you should have a _NullPointerException_, give the Singleton of _DetectorFactory_ and retry – thibsc Mar 09 '18 at 12:47
  • @MarianP you didn't provide a solution, you just gave him a link to a unit test. Thibaut on the other hand provided a solution (well, at least to the initial NPE). – Kayaman Mar 09 '18 at 13:22
  • I agree with @Kayaman, the code is very easy to read, It just take me 2 minutes to read the source and give a minimal and workable example, "newbie" is not an excuse but a little help kill nobody. – thibsc Mar 09 '18 at 14:12

1 Answers1

4

The Detector constructor signature is:

public Detector(DetectorFactory factory)

So take a look to the DetectorFactory, is a singleton without getInstance() method:
You should create your Detector like this:

Detector d = DetectorFactory.create();

But if you just doing that, is not enough...

com.cybozu.labs.langdetect.LangDetectException: need to load profiles

So the minimal and complete work example is:

try {
    String sample = "Comment vous appelez-vous?";
    // Prepare the profile before
    DetectorFactory.loadProfile("/language-detection/profiles");
    // Create the Detector
    Detector d = DetectorFactory.create();
    d.append(sample);

    System.out.println(d.detect()); // Ouput: "fr"
} catch (LangDetectException e) {
    e.printStackTrace();
}

And when you test these strings:

String sample = "Comment vous appelez-vous ?"; // "fr"
String sample = "Buongiorno come stai ?"; // "it"
String sample = "Hello how are you ?"; // "en"
thibsc
  • 3,747
  • 2
  • 18
  • 38
  • thank you so much!! Its working perfectly! I had some issues with the file-path but found a solution here (in case someone is going through the same struggle): https://stackoverflow.com/questions/42602790/java-language-detection – dcts Mar 09 '18 at 14:39