使用 Chaquopy 在 android studio 中导入(从 nltk.corpus import wordnet)时出错

Error in importing (from nltk.corpus import wordnet) in android studio using Chaquopy

import numpy as np
import nltk
nltk.download('wordnet')
from nltk.corpus import wordnet as wn

def main(a):

    b = wn.synsets(a)
    return b

我在使用 nltk.corpus 导入 wordnet 时出错。我使用了 numpy 库,它工作正常,但在尝试导入 wordnet 时出现错误。 注:我已经在Gradle.

安装了nltk、wordnet
python {
  pip {
    install "numpy"
    install "nltk"
    install "wordnet"
  }
}

Logcat 显示以下错误


 Resource [93mwordnet[0m not found.
      Please use the NLTK Downloader to obtain the resource:
    
      [31m>>> import nltk
      >>> nltk.download('wordnet')
      [0m
      For more information see: https://www.nltk.org/data.html
    
      Attempted to load [93mcorpora/wordnet[0m
    
      Searched in:
        - '/data/user/0/com.example.nlp/files/nltk_data'
        - '/usr/local/nltk_data'
        - '/usr/local/share/nltk_data'
        - '/usr/local/lib/nltk_data'
        - '/usr/share/nltk_data'
        - '/usr/local/share/nltk_data'
        - '/usr/lib/nltk_data'
        - '/usr/local/lib/nltk_data'
    **********************************************************************
    
        at <python>.nltk.data.find(data.py:585)
        at <python>.nltk.corpus.util.__load(util.py:80)
        at <python>.nltk.corpus.util.__load(util.py:85)
        at <python>.nltk.corpus.util.__getattr__(util.py:120)
        at <python>.hello.main(hello.py:10)
        at <python>.chaquopy_java.call(chaquopy_java.pyx:285)
        at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrows(chaquopy_java.pyx:257)
        at com.chaquo.python.PyObject.callAttrThrows(Native Method)
        at com.chaquo.python.PyObject.callAttr(PyObject.java:209)
        at com.example.nlp.MainActivity.onCreate(MainActivity.java:26)
        at android.app.Activity.performCreate(Activity.java:6915)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
            ... 9 more
2020-10-17 18:05:18.975 2101-2101/com.example.nlp I/Process: Sending signal. PID: 2101 SIG: 9

正如我在 my previous answer 中所说:

  • wordnet pip包显然与NLTK无关,所以你可以删除它。
  • 由于模拟器错误,您可能需要循环调用 nltk.download,如 所述。