【问题标题】:Android Studio WordNet DictionaryAndroid Studio WordNet 词典
【发布时间】:2017-08-19 07:25:06
【问题描述】:

我尝试使用 WordNet 的 JWI 库,但我似乎总是遇到 IOException,尽管我发现了它。字典打不开。

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

奇怪的一面是这些代码在 Netbeans 上完美运行。

【问题讨论】:

  • 分享错误logcat..

标签: android android-studio dictionary nlp wordnet


【解决方案1】:

在您的计算机上,这可以正常工作。 URL 指向您计算机上的安装位置。

但是

如果您尝试在真实设备或模拟器上的 Android、iOS(一些跨平台工具允许 ios dev in Java)上运行此程序,您引用的是不同设备的内部系统,因此它找不到文件并引发异常。此异常是 FileNotFoundException。当我写这篇文章时,你还没有添加你的堆栈跟踪,但我可以保证它是一个 FileNotFOundException。您引用的是您的计算机,而不是 Android 设备。您有两种选择来解决它:

  • 在应用启动时下载 Wordnet 字典(当然还要添加特定于 android 的加载)
  • 调用具有可用 Wordnet 词典的服务器
  • (仅当 Wordnet 具有在线 API 时才有效)使用 Wordnet API。

如果我在我的计算机上运行该代码,我会得到一个 FNFE,因为我的计算机上没有 Wordnet。如果 wordnet 在任何其他安装中,同样的例外。

你写你得到一个 IOException,但是 FileNotFOundException 导致一个 IOException。堆栈跟踪可能如下所示:

caused by IOException:
    at
    at
    so on
caused by FileNotFoundException
    at
    ......

FNFE 导致 IOException。 FNFE 是 IOException 的一种(FileNotFoundException 扩展了 IOException)

TL:DR;

您的 Android 设备无法访问您计算机的 wordnet 安装,因此它会引发一个 FNFE,从而导致 IOException(顶级问题,就像 NullPointer 可以在堆栈跟踪顶部显示 RuntimeException 然后具有 NPE进一步向下)

您需要在 Android 设备上安装 Wordnet,方法是使用服务器或在安装应用后下载它

【讨论】:

  • 感谢您的回复。我在 Android 项目中创建了一个文件夹,其中包含字典文件。我想将该文件夹链接到我的源代码中的 URL。我该怎么做?
  • 你应该保存在 assets 文件夹中。检查this 从资产中读取文件
  • @JunnDobitParas 你能打开字典吗?我需要一些帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-07
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
相关资源
最近更新 更多