-1

printenv WNHOME echo $WNHOME both give me the correct answer but the java program does not.

I'm trying to execute the example program featured in the JWI (the MIT Java Wordnet Interface), the first one featured in the 'User's Manual', as expressed in this question. However, when running the code I keep getting the error java.net.MalformedURLException. Here is a discussion which seems to be dealing with a similar issue however I've attempted the solutions therein proposed to no avail.

The code looks like this:

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.net.URL;
import java.nio.file.Paths;

import edu.mit.jwi.*;
import edu.mit.jwi.item.IIndexWord;
import edu.mit.jwi.item.ILexFile;
import edu.mit.jwi.item.ISenseKey;
import edu.mit.jwi.item.IWord;
import edu.mit.jwi.item.IWordID;
import edu.mit.jwi.item.POS;

public class MITJavaWordNetInterface 
{
public static void main(String[] args) throws IOException
{
    // construct the URL to the Wordnet dictionary directory
    String wnhome = System.getenv("WNHOME");
    String path = wnhome + File.separator + "dict";
    System.out.println("Path is '" + path + "'"); 
    URL url = new URL ("file", null , path );
    //final URL url = Paths.get(wnhome, "dict").toUri().toURL();

    // construct the dictionary object and open it
    IDictionary dict = new Dictionary ( url ) ;
    dict . open () ;

    // 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 ) ;
    System . out . println ("Id = " + wordID ) ;
    System . out . println (" Lemma = " + word . getLemma () ) ;
    System . out . println (" Gloss = " + word . getSynset () . getGloss () ) ;      
}       
}

There was a prerequisite step of setting the system environment variable WNHOME to the location of the root of my Wordnet istallation, which I've duly completed. My WNHOME variable is /usr/local/WordNet-3.0. What else could be the cause of this error? How can it be resolved?

I tried changing the URL to the following (equally ineffective).

final URL url = Paths.get(wnhome, "dict").toUri().toURL();

The results of System.out.println("Path is '" + path + "'"); is:

Path is 'null/dict'

The full error is this:

Exception in thread "main" java.io.IOException: Dictionary directory does not exist: null/dict
    at edu.mit.jwi.data.FileProvider.open(FileProvider.java:306)
    at edu.mit.jwi.DataSourceDictionary.open(DataSourceDictionary.java:92)
    at edu.mit.jwi.CachingDictionary.open(CachingDictionary.java:133)
    at MITJavaWordNetInterface.main(MITJavaWordNetInterface.java:28)
7
  • Let's see. You are trying to create a URL with the protocol being " file " (with spaces) - so the URL would look like " file ://path/to/file/goes/here" (also with spaces). Why are you trying to do that? Commented Jan 29, 2015 at 7:41
  • That was how the example was formulated, I suppose it's incorrect? how to resolve it? Commented Jan 29, 2015 at 7:43
  • Add the full stacktrace please ant show what is the content of path Commented Jan 29, 2015 at 7:44
  • Presumably you want the protocol to be "file" without spaces. (You do know how URLs work right?). Just remove the spaces... Commented Jan 29, 2015 at 7:44
  • " file " is not a valid protocol. "file" is. You have way too many white spaces everywhere. Commented Jan 29, 2015 at 7:45

1 Answer 1

0

You seem to have copied-and-pasted this code from somewhere.

The code as you are trying to run it has lots of extraneous spaces. Either they were inserted by whatever you used to copy-and-paste, or they were added by the compositing software.

Some of the extra spaces are harmless, but there are (at least) 3 places where they are harmful:

String wnhome = System.getenv(" WNHOME ");
String path = wnhome + File.separator + " dict ";
URL url = new URL (" file ", null , path ) ;

Remove the extraneous spaces in the 3 string literals, and the code is likely to behave (more or less) correctly.


Lesson: it is a bad idea to copy code from textbooks ... when you don't understand what the code is doing and/or the programming language it is written in.


If that doesn't fix the problem, then print out what the path component are:

System.out.println("Path is '" + path + "'"); 

and show us the complete stacktrace and the complete exception message.


the result was Path is 'null/dict'

That means that wnhome is null, which means that either you have not set the environment variable correctly, or the environment variable name you are looking up is incorrect.

Sign up to request clarification or add additional context in comments.

4 Comments

I just tried that, I edited the OP to reflect my changes, but still it's disfunctional.
your solution did not work. one would think in order to proffer explicit lessons a prerequisite might be offering actionable solutions.
1) The changes I suggested are NECESSARY whether or not they completely solve your problem. 2) The lesson would be correct even if the changes I suggested were wrong ... which they aren't.
Nonetheless, that is the problem. Perhaps you are executing the code in a different context. Perhaps you haven't recompiled ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.