Nltk is installed but nltk_utils returns ModuleNotFoundError

Question:

I am using virtual env, I have installed nltk module with pip3, when I am trying to import nltk_utils I am getting a ModuleNotFoundError

>>> import nltk
>>> import nltk_utils
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'nltk_utils'

I have tried without virtualenv too but no luck

OS : Ubuntu

Python Version : 3.9.5

GCC : 10.3.0

Asked By: Danwand N S

||

Answers:

nltk_utils is nothing that comes shipped with nltk. Did you mean nltk.util, which is described here?

Otherwise nltk_utils is used in some examples using nltk where it is a custom file that contains useful functions in interacting with nltk (E.g. in this chatbot example) so if you are following some tutorial or similar, check if they mention somewhere what nltk_utils should contain

Answered By: FlyingTeller

Adding to the answer of user FlyingTeller:
I came here having the same problem, and i followed the exact same tutorial as linked by user FlyingTeller. The referenced import "nltk_utils" is a custom file made in the scope of the tutorial.

Solving the problem:
You can find "nltk_utils" at the github of the tutorial creator, here:
https://github.com/patrickloeber/pytorch-chatbot/blob/master/nltk_utils.py
(for more explanation about that file, check the video that is linked in the tutorial).

Update:
You also need the file "model.py", which is found at the above linked github, too.
After that, you may still face errors, in my case i needed to move the "# train model" part into main and also cast the labels to int. The adjusted code looks as follows:

...
if __name__ == '__main__':
    # Train the model
    for epoch in range(num_epochs):
        for (words, labels) in train_loader:
            words = words.to(device)
            labels = labels.type(torch.LongTensor) # <- Fix from here: https://stackoverflow.com/a/71149364/18456868
            labels = labels.to(device)

            # Forward pass
            outputs = model(words)
...

After that, i got it working:
Output of script after about 3 minutes of training

Answered By: Haxel0rd

Your issue arises possibly because you did not take the nltk_utils.py file into your directory so ensure that you have done that; I come with this reseasoning because I usually write the utility methods like tokenize, stem or bag_of_words etc into that file for modularisation

Answered By: Boy Nandi