NLTK - no module named corpus

After installing NLTK and NLTK-DATA with PIP, I start python , then I type import cmudict from nltk.corpus and it works. But when I wrote the script as follows:

from nltk.corpus import cmudict

d = cmudict.dict()

def nsyl(word):
    return [len(list(y for y in x if y[-1].isdigit())) for x in d[word.lower()]]

print nsyl("hello")

I have the following error:

Traceback (most recent call last):
File "nltk.py", line 1, in <module>
from nltk.corpus import cmudict
File "nltk.py", line 1, in <module>
from nltk.corpus import cmudict
ImportError: No module named corpus

How can i fix this?

Thanks in advance

+4
source share
2 answers

From your stacktrace:, File "nltk.py", line 1, in <module>you called the nltk.py file. When python is looking for a module, it first looks in the current directory, and you have "nltk.py". It will import this as nltk, and since your code does not define corpus, it cannot find it nltk.corpus.

, , nltkexperience.py. "nltk.pyc" , , ( ). .

+5

, , -, . Python, , , NLTK, , script.

Python 2.7, Python 3.3 Anaconda Python (2.7). Anaconda ( , ). , - pip , . Vim Python, / Anaconda. Vim Python, , .

, .

+1

Source: https://habr.com/ru/post/1547286/


All Articles