2016-03-10 39 views
0

我使用JWI与WordNet的接口,在Android.Following是我的相关代码

ON按钮单击

public class ODFragment extends Fragment { 
... 
//global vars 
String searchWord = "dog"; 
String wordDefinition = null; 
DictSearch dict; 

@Override 
public View onCreateView(LayoutInflater inflater, 
         ViewGroup container, Bundle savedInstanceState) { 
... 
     View.OnClickListener defineListener = new View.OnClickListener() 
    { 

     @Override 
     public void onClick(View v) { //TODO 

      ... 
      //query offline dictionary 
      dict = new DictSearch(rootView.getContext().getAssets()); 

      if(!dict.exists()) 
      { 
       Toast.makeText(rootView.getContext(), "Dictionary files not found!", Toast.LENGTH_SHORT).show(); 
       return; 
      } 
      else { 
       Toast.makeText(rootView.getContext(), "Dictionary files found!", Toast.LENGTH_SHORT).show(); 
       wordDefinition = dict.getTopGlosses(searchWord); //FAULTY 


       if (wordDefinition.equals("")) { 
        Toast.makeText(rootView.getContext(), "No definition found", Toast.LENGTH_LONG).show(); 
       } else { 
        //show in dialogue 
        PostCaptureDialogue postCaptureDialogue = new PostCaptureDialogue(); //use this dialogue for the time being 
        postCaptureDialogue.setDimensions("Definition of '" + searchWord + "': " + wordDefinition); 
        postCaptureDialogue.show(getFragmentManager(), "post_textual_query_dialogue"); 
       } 

      } 


     } 
    }; 
    button_define.setOnClickListener(defineListener); 



    return rootView; 
} 
... 
} 

DictSearch.Java

public class DictSearch { 

static POS[] POS_ARR = { POS.NOUN, POS.VERB, POS.ADJECTIVE, POS.ADVERB }; 
StringBuffer m; 
IDictionary dict; 
boolean created; 

AssetManager assets; 


public DictSearch(AssetManager assets_param) { 
    created = true; 
    m = new StringBuffer(5000); 
    assets = assets_param; 
    buildDict(); 

} 


private void buildDict() { 

    StringBuffer fpath = new StringBuffer(400); 
    fpath.append(Environment.getExternalStorageDirectory().toString() 
      + "/odnvt_resources"); 

    File f = new File(fpath.toString()); 

    dict = new Dictionary(f); 
    try { 
     dict.open(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     created = false; 
    } 
} 


private String shorten(String s) { 
    if(s.length() == 4) 
     return s.substring(0, 1); 
    else 
     return s.substring(0, 3); 
} 

public boolean exists() { 
    return created; 
} 


public String getTopGlosses(String search_word) { 

    //buildDict(); 

    int i = 1; 
    for (POS p : POS_ARR) { 
     IIndexWord idxWord = dict.getIndexWord(search_word, p); //FAULT HERE 
     //IIndexWord idxWord = dict.getIndexWord("dog", POS.NOUN); //dbg 

     if (idxWord == null) 
      continue; 
     List<IWordID> wordIDs = idxWord.getWordIDs(); 
     IWordID wordID = wordIDs.get(0); 
     IWord iword = dict.getWord(wordID); 
     m.append(String.format(Locale.getDefault(), "%d. (%s) %s\n", 
       i, shorten(iword.getPOS().toString()), iword.getSynset() 
         .getGloss())); 
     ++i; 
    } 
    return m.toString(); 
} 

}

在点击按钮时,我得到以下错误:

错误跟踪

E/AndroidRuntime: FATAL EXCEPTION: main 
                    Process: com.abdulwasae.odnvt_1, PID: 20652 
                    edu.mit.jwi.data.IHasLifecycle$ObjectClosedException 
                     at edu.mit.jwi.CachingDictionary.checkOpen(CachingDictionary.java:112) 
                     at edu.mit.jwi.CachingDictionary.getIndexWord(CachingDictionary.java:191) 
                     at com.abdulwasae.odnvt_1.DictSearch.getTopGlosses(DictSearch.java:126) 
                     at com.abdulwasae.odnvt_1.ODFragment$2.onClick(ODFragment.java:238) 
                     at android.view.View.performClick(View.java:4856) 
                     at android.view.View$PerformClick.run(View.java:19956) 
                     at android.os.Handler.handleCallback(Handler.java:739) 
                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                     at android.os.Looper.loop(Looper.java:211) 
                     at android.app.ActivityThread.main(ActivityThread.java:5373) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at java.lang.reflect.Method.invoke(Method.java:372) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 

我一直在寻找到这了很多天。我现在很难过。任何帮助,将不胜感激。

注:

  • 我已DictSearch.java的部分从我解压词典文件XDictionary
  • 位置的开源代码:\程序\ SRC \主\资产\字典
  • 我也有发生在SDCARD0/odnvt_resources/压缩的WordNet的,因为我已经有混乱在何处放置文件和访问它们

预先感谢您

回答

0

我刚刚开始研究Wordnet库并遇到同样的错误。看起来字典对象对我来说是封闭的。所以我只需要做dict.open()就行了。

我使用MIT的Java WORDNET库(http://projects.csail.mit.edu/jwi/)和示例代码是在这里:

public static void main(String[] args) throws IOException { 
     String path = "C:\\Users\\abc\\Desktop\\wn3\\dict"; 
     URL url = new URL("file", null, path); 

     IDictionary dict = new Dictionary(url); 
     dict.open(); 
     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()); 
    } 
相关问题