0

我在 Android 中使用 JWI 与 WordNet 交互。以下是我的相关代码

点击按钮

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;
}
...
}

字典搜索.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)

我已经研究了很多天。我现在很难过。任何帮助,将不胜感激。

笔记:

  • 我从 XDictionary 的开源代码中获取了DictSearch.java的一部分
  • 我解压缩的字典文件的位置是:\app\src\main\assets\dict
  • 我也将压缩的 WordNet 放在SDCARD0/odnvt_resources/中,因为我一直对放置文件和访问它们的位置感到困惑

先感谢您

4

1 回答 1

1

我刚刚开始研究 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());
    }
于 2017-02-03T09:02:12.013 回答