gpt4 book ai didi

android - Chaquopy 无法下载 NLTK 资源

转载 作者:太空宇宙 更新时间:2023-11-03 20:51:33 26 4
gpt4 key购买 nike

我正在尝试使用 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/AndroidRuntime: FATAL EXCEPTION: main Process: process, PID: 22186 java.lang.RuntimeException: Unable to start activity 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这似乎是类似的情况,但没有解决我的问题。

最佳答案

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

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

由于模拟器错误,您可能需要在循环中调用 nltk.download,如 this answer 中所述。 .

关于android - Chaquopy 无法下载 NLTK 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56283660/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com