Chaquopy problems with nltk and download

六眼飞鱼酱① 提交于 2021-02-04 08:08:48

问题


According to Chaquopy Not able to download Resource i'm not sure if the problem got solved.

So here is question in the nltk context. After including one of the nltk.download line:

nltk.download('popular')
or
nltk.download('punkt')
or
nltk.download('all')

I get this stack trace:

2020-08-26 13:33:45.742 19765-19765/com.pro.useyournotes E/ExceptionTag: com.chaquo.python.PyException: BadZipFile: File is not a zip file
    com.chaquo.python.PyException: BadZipFile: File is not a zip file
        at <python>.zipfile._RealGetContents(zipfile.py:1335)
        at <python>.zipfile.__init__(zipfile.py:1268)
        at <python>.nltk.data.__init__(data.py:936)
        at <python>.nltk.compat._decorator(compat.py:41)
        at <python>.nltk.data.__init__(data.py:396)
        at <python>.nltk.compat._decorator(compat.py:41)
        at <python>.nltk.data.find(data.py:544)
        at <python>.nltk.data.find(data.py:557)
        at <python>.nltk.tag.perceptron.__init__(perceptron.py:168)
        at <python>.nltk.tag._get_tagger(__init__.py:106)
        at <python>.nltk.tag.pos_tag_sents(__init__.py:178)
        at <python>.uyn_pre_processing.pre_processing(uyn_pre_processing.py:88)
        at <python>.uyn_analysis_workflow.analyse_new_data(uyn_analysis_workflow.py:62)
        at <python>.uyn_main.main(uyn_main.py:266)
        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.pro.useyournotes.MainActivity.getPythonHello(MainActivity.kt:70)
        at com.pro.useyournotes.MainActivity.onCreate(MainActivity.kt:59)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

The code where this error occurs is:

    tagged_words=nltk.pos_tag_sents(tokenized_sentences)

at <python>.uyn_pre_processing.pre_processing(uyn_pre_processing.py:88)

I also don't know where the nltk-files are placed. Earlier when i just programmed on the python side i onlyremember using the import nltk command. Hopefully some already found a solution for using nltk.


回答1:


I was able to reproduce something similar on the emulator. In my case the root cause was that the download failed with a DECRYPTION_FAILED_OR_BAD_RECORD_MAC error, leaving behind an incomplete ZIP file.

This appears to be a low-level problem with the emulator which isn't specific to Python. If you can confirm you have the same problem (by seeing DECRYPTION_FAILED_OR_BAD_RECORD_MAC in the nltk.download logcat output), then please add a star on the Android issue tracker here, to help encourage them to fix it.

You can work around this by calling nltk.download repeatedly in a loop until it returns true. To save time, you should probably only download the data files you need. You can find out what these are by simply calling the corresponding function and looking at the error message, e.g.:

>>> nltk.pos_tag_sents([["hello", "world"]])
...
LookupError: 
**********************************************************************
  Resource [93maveraged_perceptron_tagger[0m not found.
  Please use the NLTK Downloader to obtain the resource:
 
  [31m>>> import nltk
  >>> nltk.download('averaged_perceptron_tagger')

Then you can add this to your code:

while not nltk.download('averaged_perceptron_tagger'):
    print("Retrying download")

This succeeded after a few iterations, and I was then able to call nltk.pos_tag_sents successfully.



来源:https://stackoverflow.com/questions/63598027/chaquopy-problems-with-nltk-and-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!