ImportError: cannot import name PunktWordTokenizer - python

I was trying to use PunktWordTokenizer and it was occurred an error as below.
from nltk.tokenize.punkt import PunktWordTokenizer
And this gave the following error message.
Traceback (most recent call last): File "file", line 5, in <module>
from nltk.tokenize.punkt import PunktWordTokenizer ImportError: cannot import name PunktWordTokenizer
I've checked that nltk is installed and that PunkWordTokenzer is also installed using nltk.download(). Need some help for this.

PunktWordTokenizer was previously exposed to user but not any more. You can rather use WordPunctTokenizer.
from nltk.tokenize import WordPunctTokenizer
WordPunctTokenizer().tokenize(“text to tokenize”)
The difference is :
PunktWordTokenizer splits on punctuation, but keeps it with the word. Where as WordPunctTokenizer splits all punctuations into separate tokens.
For example, given Input: This’s a test
PunktWordTokenizer: [‘This’, “‘s”, ‘a’, ‘test’]
WordPunctTokenizer: [‘This’, “‘”, ‘s’, ‘a’, ‘test’]

There appears to be a regression related to PunktWordTokenizer in 3.0.2. The issue was not present in 3.0.1, rolling back to that version or earlier fixes the issue.
>>> import nltk
>>> nltk.__version__
'3.0.2'
>>> from nltk.tokenize import PunktWordTokenizer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name PunktWordTokenizer
For solving this Try pip install -U nltk to upgrade your NLTK version.

Related

I don't know how to run a program from terminal involving NLTK. All the examples are run from the interpreter but don't use actual programs

I am trying to run a program that uses NLTK. In the NLTK book it uses the interpreter to run individual scripts such as this
>>> import nltk
My program is similar but I want to run it from a file
import nltk
from nltk.tokenize import sent_tokenize
mytext = "Hello Adam how are you? I hope everything is good"
print(sent_tokenize(mytext))`
I get this error message
Traceback (most recent call last):
File "C:\Users\Beowulf\PycharmProjects\pythonProject1\nltk.py", line 1, in <module>
import nltk
File "C:\Users\Beowulf\PycharmProjects\pythonProject1\nltk.py", line 2, in <module>
from nltk.tokenize import sent_tokenize
ModuleNotFoundError: No module named 'nltk.tokenize'; 'nltk' is not a package
I am using PyCharm
I want to save the file and run the file from the command line.

No module named ‘torchvision.models.utils‘

When I use the environment of pytorch=1.10.0, torchvision=0.11.1 to run the code, I run to the statement from torchvision.models.utils import load_state_dict_from_url. The following error will appear when:
>>> from torchvision.models.utils import load_state_dict_from_url
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torchvision.models.utils'
After consulting torchvision's code repository, there is a solution:
Note that this syntax is only for higher versions of PyTorch.
The original code from .utils import load_state_dict_from_url is not applicable.
you connot import load_state_dict_from_url from .utils.
change .utils to torch.hub can fix the problem.
from torch.hub import load_state_dict_from_url
This worked for me.

Python: Error importing textblob lib

Using windows 10
I've install textblob using "py -m pip install textblob".
I can import textblob, or from textblob import blob,word
But i cant: from textblobl import Textblob.
The error i get is:
Traceback (most recent call last):
File "", line 1, in
from textblob import Textblob
ImportError: cannot import name 'Textblob'
Thanks.
You can try whit this:
from textblob import TextBlob
you can try change your file name TextBlob.py

Python nltk ImportError

I was trying to use nltk for python and then confronted with this problem.
>>>from nltk.corpus import sinica_tree
and then the error occured:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name sinica_tree
I've checked that nltk is installed and that sinica_tree is also installed using nltk.download(). Need some help for this.
According to the documentation it looks like it should be:
from nltk.corpus import sinica_treebank
See: http://www.nltk.org/nltk_data/
>>>import nltk
>>>nltk.download()
---------------------------------------------------------------------------
d) Download l) List u) Update c) Config h) Help q) Quit
---------------------------------------------------------------------------
Downloader> d
Download which package (l=list; x=cancel)?
Identifier> treebank
then try to import
from nltk.corpus import sinica_treebank

Import Error, When I import nltk.corpus.framenet in NLTK Python

I have to use framenet in nltk.corpus. So, I downloaded that corpus by using the nltk.download(). And the framenet directory is now C:\nltk_data\corpora\framenet_v15...
But when I import that framenet, I can't. I can't find the reason.
I want to some works as explained in here; http://nltk.org/howto/framenet.html
>>> import nltk.corpus.framenet
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import nltk.corpus.framenet
ImportError: No module named framenet
Please, help me. Thanks.
In your link it import as so:
from nltk.corpus import framenet
Have you tried that?
EDIT: Version 3.0 and above of NLTK has framenet in the nltk.corpus.reader package, so it should be:
from nltk.corpus.reader import framenet

Categories

Resources