Skip to content Skip to sidebar Skip to footer

How To Add Nltk Corpora To A Google Cloud Function?

I am trying to run Google cloud function which involves the use of NLTK. I had added textblob == 0.15.3 nltk == 3.4.3 to the requirement.txt. But every time I run the script it cr

Solution 1:

This is how I get the nltk_data through my Travis Pipeline:

# To install the core NLTK package
    pip install nltk   

    # Installs only the extra packages you need. You could also use 'all' instead. 
    python -m nltk.downloader punkt averaged_perceptron_tagger wordnet    

Then you can copy the folder into your function folder, and zip it up:

mkdir -p function/nltk_data/
    cp -a ~/nltk_data/. function/nltk_data/
    cp -a path/to/your/code/. function/

Be sure to set the NLTK_DATA environment variable. As my folder structure was

    - nltk_data/
    - main.py
    - requirements.txt

I just needed to set NLTK_DATA=nltk_data, and then python can find the files.

Hope this helps!

Solution 2:

There are two ways to specify dependencies for Cloud Functions written in Python: using the pip package manager's requirements.txt file or packaging local dependencies alongside your function. There you can find instructions. Also check this link for possible solution.

Post a Comment for "How To Add Nltk Corpora To A Google Cloud Function?"