1

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.

1 Answers1

0

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Thanks for the reply. I created a folder in the Android project where it contains the dictionary files. I want to link that folder to the URL in my source code. How do I do that? – Junn Dobit Paras Aug 19 '17 at 10:08
  • 1
    You should keep in in the assets folder. Check [this](https://stackoverflow.com/questions/9544737/read-file-from-assets) on reading files from assets – Zoe Aug 19 '17 at 11:14
  • @JunnDobitParas were you able to open the dictionary? I need some help – makkhay gurung Sep 18 '18 at 06:11