Search code examples
javapythonandroid-studionltkchaquopy

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

I have error in importing wordnet using nltk.corpus. I have used the numpy library and it works fine, but gives me error when try to import wordnet. Note: I have already installed the nltk, wordnet in Gradle.

python {
  pip {
    install "numpy"
    install "nltk"
    install "wordnet"
  }
}

The Logcat shows me the following error


 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


Solution

  • As I already said in my previous answer:

    • The wordnet pip package apparently has nothing to do with NLTK, so you can remove it.
    • Because of an emulator bug, you may need to call nltk.download in a loop, as described here.