0

My code reads

package com.fyp.jwi;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import edu.mit.jwi.Dictionary;
import edu.mit.jwi.IDictionary;
import edu.mit.jwi.item.IIndexWord;
import edu.mit.jwi.item.IWord;
import edu.mit.jwi.item.IWordID;
import edu.mit.jwi.item.POS;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ---->  String wnhome = "file:///android_asset/";   <----
     String path = wnhome + File.separator + " dict ";
     URL url = null;
     try{ url = new URL("file", null, path); } 
          catch(MalformedURLException e){ try {
            e.printStackTrace();
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } }
          if(url == null) return;

     // construct the dictionary object and open it
     IDictionary dict = new Dictionary ( url);
     try {
        dict.open ();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

     // 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 ());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

The code which I have highlighted using the '-->' was originally

String wnhome = System.getenv("WNHOME");

Where WNHOME is an environment variable that refers to the directory where certain files to help a Java wordnet api are located on your computer. This works fine when I compile the code. I need a standalone Java program on my pc.

I have attached an image to make things clearer enter image description here

Now I know how to set and use environment variables on windows. How to do it on android? And am I bundling these files the right way? Is there another way to do it?

OR is there a workaround that doesn't involve setting an environment variable? Anyone familiar with this or the JWI help me out ?

demongolem
  • 9,474
  • 36
  • 90
  • 105
Srini
  • 1,619
  • 1
  • 19
  • 34
  • in your code --> wnhome + File.separator <-- but ther is already a file separator at the end in wnhome – Pararth Feb 08 '14 at 06:03
  • so you're saying I should remove the '/' at the end of "file:///android_asset/" ? – Srini Feb 08 '14 at 06:04
  • Android has the concept of environment variables, but they are unlikely to help you, as an Environment variable on the Android device pointing to a location on your development machine is going to be useless as you can't access that storage from there. Build the files of interest in the APK, or put them on the device storage, and inform the app of their location in a more Java-esque way. – Chris Stratton Feb 08 '14 at 06:16
  • yes.. or remove the "+ File.separator" in the next line – Pararth Feb 08 '14 at 06:17
  • @ChrisStratton any advice on how to go about that ? should i ad the dict folder to the sd card and then add a read storage permission the in the manifest ? – Srini Feb 08 '14 at 06:28
  • @user2450263 that still forces my app t crash :( – Srini Feb 08 '14 at 06:30
  • Either the assets or the external storage is a general possibility, with various tradeoffs. But there's little benefit to using an environment variable, since your app would have to set it as well as read it (as a third-party developer, you won't get to set the pre-set environment variables which your app inherits from zygote) – Chris Stratton Feb 08 '14 at 06:31
  • so the current way I use to refer to the assets folder is correct ?? the data files in the assets folder will now be bundled witht he apk ? – Srini Feb 08 '14 at 06:33
  • 1
    @SrinivasSuresh Did you manage to load the WordNet files in Android eventually? – almightyGOSU Feb 07 '16 at 07:39
  • Nope, i learnt to web scrape and wrote an api to scrape wordnet . https://github.com/thewickedaxe/WonetLib . I beg you and implore you to nt use this out of the box. I wrote it as I was just learning java, consequently the code quality is shite. But it works – Srini Feb 16 '16 at 23:06

0 Answers0