No module named 'nltk.lm' in Google colaboratory

1.5k Views Asked by At

I'm trying to import the NLTK language modeling module (nltk.lm) in a Google colaboratory notebook without success. I've tried by installing everything from nltk, still without success.

What mistake or omission could I be making?

Thanks in advance.

enter image description here. enter image description here

1

There are 1 best solutions below

1
On BEST ANSWER

Google Colab has nltk v3.2.5 installed, but nltk.lm (Language Modeling package) was added in v3.4.

In your Google Colab run:

!pip install -U nltk

In the output you will see it downloads a new version, and uninstalls the old one:

...
Downloading nltk-3.6.5-py3-none-any.whl (1.5 MB)
...
Successfully uninstalled nltk-3.2.5
...
You must restart the runtime in order to use newly installed versions.

Click the Restart runtime button shown in the end of the output.

Now it should work!

You can double check the nltk version using this code:

import nltk
print('The nltk version is {}.'.format(nltk.__version__))

You need v3.4 or later to use nltk.lm.