Chaquopy无法下载资源

时间:2019-05-23 22:25:38

标签: android python chaquopy

我正在尝试使用android上的chaquopy工具实时下载nltk所需的软件包。

我正在尝试将模型下载到特定的数据目录中,如下所示:

from android.os import Bundle
from android.support.v7.app import AppCompatActivity
from activity import R
from com.chaquo.python import Python
from java import jvoid, Override, static_proxy
import nltk
import os

class MainActivity(static_proxy(AppCompatActivity)):
    @Override(jvoid, [Bundle])
    def onCreate(self, state):
        AppCompatActivity.onCreate(self, state)
        self.setContentView(R.layout.activity_main)
        download_dir = "{}/nltk".format(Python.getPlatform().getApplication().getFilesDir())
        if not os.path.exists(download_dir):
            os.mkdir(download_dir)
        os.environ['NLTK_DATA'] = download_dir
        print('Download Dir: {}'.format(download_dir))
        #print(nltk.__version__)
        #dl = nltk.downloader.Downloader('http://nltk.org/nltk_data/')
        #dl.download('punkt', download_dir=download_dir)
        nltk.download('punkt')
        self.findViewById(R.id.text_box_1).setText(str(nltk.word_tokenize("Hello Python")))

我收到以下错误,因为客户端无法找到或下载所需的软件包:

  

E / Android运行时:致命异常:主要       处理:处理,PID:22186       java.lang.RuntimeException:无法启动活动ComponentInfo {/utils.Utils.MainActivity}:   com.chaquo.python.PyException:LookupError:

  Resource [93mpunkt[0m not found.
  Please use the NLTK Downloader to obtain the resource:

  [31m>>> import nltk
  >>> nltk.download('punkt')
  [0m
  For more information see: https://www.nltk.org/data.html

  Attempted to load [93mtokenizers/punkt/PY3/english.pickle[0m

  Searched in:
    - '/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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:81)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1935)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7116)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)
 Caused by: com.chaquo.python.PyException: LookupError: 
**********************************************************************
  Resource [93mpunkt[0m not found.
  Please use the NLTK Downloader to obtain the resource:

  [31m>>> import nltk
  >>> nltk.download('punkt')
  [0m
  For more information see: https://www.nltk.org/data.html

  Attempted to load [93mtokenizers/punkt/PY3/english.pickle[0m

  Searched in:
    - '/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:701)
    at <python>.nltk.data._open(data.py:995)
    at <python>.nltk.data.load(data.py:870)
    at <python>.nltk.tokenize.sent_tokenize(__init__.py:104)
    at <python>.nltk.tokenize.word_tokenize(__init__.py:143)
    at <python>.utils.Utils.onCreate(Utils.py:23)
    at <python>.chaquopy_java.call(chaquopy_java.pyx:283)
    at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrows(chaquopy_java.pyx:255)
    at com.chaquo.python.PyObject.callAttrThrows(Native Method)
    at com.chaquo.python.PyObject._chaquopyCall(PyObject.java:222)
    at utils.Utils.MainActivity.onCreate(MainActivity.java:24)
    at android.app.Activity.performCreate(Activity.java:7698)
    at android.app.Activity.performCreate(Activity.java:7687)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3096)
        ... 11 more

我知道线程What directory does chaquopy code search for the Python packages which are imported in the Python code of the Android app code似乎是类似情况,但不能解决我的问题。

1 个答案:

答案 0 :(得分:0)

尝试更新为Chaquopy 4.0.0 or newer。这些版本将HOME环境变量设置为应用程序的文件目录,并且NLTK将在该目录中自动创建一个nltk_data子目录。这样就不再需要使用NLTK_DATA环境变量或download_dir参数,并且应该删除所有代码。

(如果您已经在使用Chaquopy 4.0.0或更高版本,请在您的Chaquopy和NLTK版本中添加注释。)