3

I have imported nltk library in my main method of the Python code of the chaquopy Android app. It is asking me to implement nltk.download('punkt') for my processing. So I wanted to know: in what directory does the code look for the packages, so that I can download a copy of them in the desired folder for my code to work right?

Edit: I implemented the solution provided by @mhsmith, but nltk.download('punkt') is still giving Lookup Error. Screenshots are attached:

The first line is the download_dir in which nltk.download('punkt') is downloading data

The first line is the download_dir in which nltk.download('punkt') is downloading data

This is the error I am getting even after implementing solution by @mhsmith

This is the error I am getting even after implementing solution by @mhsmith

This is the snippet of my code

This is the snippet of my code

mhsmith
  • 6,675
  • 3
  • 41
  • 58

1 Answers1

1

Chaquopy 4.0.0 and newer

These versions set the HOME environment variable to your app's files directory, and nltk will automatically create an nltk_data subdirectory there. So no special action is required.


Chaquopy 3.3.2 and older

I think the cleanest solution would be to create a separate directory for your downloads, like this:

from com.chaquo.python import Python
download_dir = "{}/nltk".format(Python.getPlatform().getApplication().getFilesDir())
if not os.path.exists(download_dir):
    os.mkdir(download_dir)
nltk.download(..., download_dir=download_dir)

(The getPlatform method requires Chaquopy 3.2.0 or later.)

From the NLTK documentation, it looks like you'll have to set the NLTK_DATA environment variable to this directory. This should probably be done before you import nltk.

mhsmith
  • 6,675
  • 3
  • 41
  • 58
  • I have inserted the snippets of the code in the folowing answer to the above question. please clarify the issue – utkarsh verma Jun 18 '18 at 12:18
  • @utkarshverma If your app is based off the "hello" example I guess you're using Chaquopy 3.1.0. Please edit your build.gradle to upgrade to version 3.2.0. – mhsmith Jun 18 '18 at 14:09
  • I have edited the question with the doubts. Additionally I have one more doubt that what does setting environment variable in system do if the app is running on mobile. I mean, if I set the download_dir path in the system as shown in snippet, the error of directory doesnot exist occurs – utkarsh verma Jun 18 '18 at 21:36
  • The app is giving some new errors as attached in Edit. Any help would be great – utkarsh verma Jun 19 '18 at 07:56
  • @utkarshverma I've added a link in my answer explaining how to set an environment variable. This should probably be done *before* you `import nltk`. – mhsmith Jun 19 '18 at 11:37
  • But how would adding environment variable in PC help when the app would run on mobile app – utkarsh verma Jun 19 '18 at 11:48
  • It's not on the PC. Apps have their own environment variables as well, and that link tells you how to set them from Python code. – mhsmith Jun 19 '18 at 13:16