Shifu
Shifu

Reputation: 2185

Issue with conditional frequency distribution

I had the following code:

corpus= CategorizedPlaintextCorpusReader(loc,'(?!\.svn).*\.txt', cat_pattern=r'(Shakespeare|Milton)/.*')
    cfd=nltk.ConditionalFreqDist ((genre,word)
                          for genre in corpus.categories()
                          for word in corpus.words(categories = genre))
    genres=['Shakespeare','Milton']
    pronouns=['I','you','he','she', 'it','we','they']

    cfd.tabulate (conditions=genres,samples=pronouns)

Now, for some mighty perculiar reason I get the following error: "category = re.match(self._pattern, file_id).group(1) AttributeError: 'NoneType' object has no attribute 'group'"

Anyone have an idea what that is about?

Upvotes: 1

Views: 525

Answers (1)

Ram Narasimhan
Ram Narasimhan

Reputation: 22506

category = re.match(self._pattern, file_id).group(1) 
AttributeError: 'NoneType' object has no attribute 'group'

This error message is telling you that the re.match returned None. In other words, there was no match. When you look for group(1) it throws up the error.

You have a few options in terms of moving forward:

  1. Simplify your match command.

  2. Be defensive. First use an if to check if there was a match then look for its group.

  3. In general, when you get stuck with errors like this, keep simplifying your code to see what is causing the problem. Python can be written in very terse ways, but I find it better to be more descriptive when learning.

This SO question should give you a few more options.

Hope that helps you move forward.

Upvotes: 1

Related Questions