Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: can't set attribute from nltk.book import *

after installing nltk i import nltk and then use nltk.download() but when i try to use this "from nltk.book import *" it shows attribute error. from nltk.corpus import * and from nltk import * works fine

i am new to natural language processing so i dont know much about this please help

from nltk.book import * * Introductory Examples for the NLTK Book *

Loading text1, ..., text9 and sent1, ..., sent9

Type the name of the text or sentence to view it.

Type: 'texts()' or 'sents()' to list the materials.

Traceback (most recent call last):

File "", line 1, in

from nltk.book import *

File "C:\Program Files (x86)\Python 3.5\lib\site-packages\nltk\book.py", line 19, in

text1 = Text(gutenberg.words('melville-moby_dick.txt'))

File "C:\Program Files (x86)\Python 3.5\lib\site-packages\nltk\text.py", line 295, in init

tokens = list(tokens)

File "C:\Program Files (x86)\Python 3.5\lib\site-

packages\nltk\corpus\reader\util.py", line 233, in len

for tok in self.iterate_from(self._toknum[-1]): pass

File "C:\Program Files (x86)\Python 3.5\lib\site-packages\nltk\corpus\reader\util.py", line 291, in iterate_from

tokens = self.read_block(self._stream)

File "C:\Program Files (x86)\Python 3.5\lib\site-packages\nltk\corpus\reader\plaintext.py", line 117, in _read_word_block words.extend(self._word_tokenizer.tokenize(stream.readline()))

File "C:\Program Files (x86)\Python 3.5\lib\site-packages\nltk\tokenize\regexp.py", line 126, in tokenize self._check_regexp()

File "C:\Program Files (x86)\Python 3.5\lib\site-packages\nltk\tokenize\regexp.py", line 121, in _check_regexp self._regexp = compile_regexp_to_noncapturing(self._pattern, self._flags)

File "C:\Program Files (x86)\Python 3.5\lib\site-packages\nltk\internals.py", line 56, in compile_regexp_to_noncapturing return sre_compile.compile(convert_regexp_to_noncapturing_parsed(sre_parse.parse(pattern)), flags=flags)

File "C:\Program Files (x86)\Python 3.5\lib\site-packages\nltk\internals.py", line 52, in convert_regexp_to_noncapturing_parsed parsed_pattern.pattern.groups = 1

AttributeError: can't set attribute

like image 698
Ankit Kumar Namdeo Avatar asked Oct 04 '15 06:10

Ankit Kumar Namdeo


1 Answers

I am not sure if you worked our your issue. Just in case, same issue also reported here: https://github.com/nltk/nltk/issues/1135

Solution: https://github.com/nltk/nltk/issues/1106

"I was able to fix this problem by going into the internals.py file in the nltk directory and removing the line parsed_pattern.pattern.groups = 1. My rationale behind this was, after doing a bit of code reading, the original version of sre_parse.py that NLTK was designed to work stored groups as an attribute of an instance of the sre_parse.Pattern class. The version that comes with Python 3.5 stores groups as a property which returns (I'm not too familiar with properties, but this is what I presume it does) the length of a subpattern list. The code I'm talking about is here at about line 75. What I don't know is what the long term effects of doing this will be, I came up with this solution just by tracing through the code, I haven't looked at what bugs this may cause in the long run. Someone please tell me if this would cause problems and if there's a better solution."

The above works for me without any issues so far.

like image 59
Akshay Gaur Avatar answered Sep 29 '22 20:09

Akshay Gaur