Search code examples
androidandroid-studiodictionarynlpwordnet

Android Studio WordNet Dictionary


I tried using the JWI library for WordNet but I always seem to have an IOException despite catching it. The dictionary won't open.

    wnhome = "C:/Program Files (x86)/WordNet/2.1";
    path = wnhome + File.separator + "dict";
    try {
        url = new URL("file", null, path);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    IDictionary dict = new Dictionary(url);
    try {
        dict.open();
    } catch (IOException ex) {
        textView.setText("Failed");
    }

    textView.setText("" + dict.isOpen());
    // look up first sense of the word "dog"
            IIndexWord idxWord = dict.getIndexWord("dog", POS.NOUN);
            IWordID wordID = idxWord.getWordIDs().get(0);
            IWord word = dict.getWord(wordID);
            textView.setText("Id = " + wordID);
            textView.setText("Lemma = " + word.getLemma());
            textView.setText("Gloss = " + word.getSynset().getGloss());

Weird side of this is these codes perfectly runs on Netbeans.


Solution

  • On your computer, this works fine. The URL directs to the location on your computer where it is installed.

    However

    If you try to run this on Android, iOS (some cross-platform tools allows ios dev in Java) on either a real device or in an emulator, you reference the internal system of a different device, thus it can't find the file and throws an exception. This exception being a FileNotFoundException. When I write this you haven't added your stacktrace yet, but I can guarantee it is a FileNotFOundException. YOu reference your computer, not the Android device. You have two options to solve it:

    • Download the Wordnet dictionary on app start (and of course add android-specific loading)
    • Make calls to a server that has the Wordnet dictionary available
    • (This is only valid if Wordnet has an online API) Use the Wordnet API.

    If I ran that code on my computer I would get a FNFE, as I don't have Wordnet on my computer. If wordnet is in any other install, same exception.

    You wrote you get an IOException, but FileNotFOundException leads to an IOException. A stacktrace could look like:

    caused by IOException:
        at
        at
        so on
    caused by FileNotFoundException
        at
        ......
    

    FNFE leads to IOException. FNFE is btw a type of IOException (FileNotFoundException extends IOException)

    TL:DR;

    Your Android device can't access your computer's wordnet install, therefore it throws a FNFE which leads to an IOException (top-level issue, just like a NullPointer can show a RuntimeException at the top of the stacktrace and then have the NPE piece further down)

    You need to get access to a Wordnet install on your Android device, either by using a server or downloading it after the app is installed