获取AttributeError:在Python代码2中

时间:2015-07-04 11:05:50

标签: python python-2.7

我正在读这本书"编程集体智慧"第6章,文档过滤。我只想尝试运行本书中给出的代码。但我收到此错误消息。

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
cl.train('the quick brown fox jumps over the lazy dog','good')
AttributeError: classifier instance has no attribute 'train'

我使用的代码

import re
import math

def getwords(doc):
  splitter=re.compile('\\W*')
  print doc
  # Split the words by non-alpha characters
  words=[s.lower() for s in splitter.split(doc) 
          if len(s)>2 and len(s)<20]

  # Return the unique set of words only
  return dict([(w,1) for w in words])

class classifier:
  def __init__(self,getfeatures,filename=None):
    # Counts of feature/category combinations
    self.fc={}
    # Counts of documents in each category
    self.cc={}
    self.getfeatures=getfeatures

    # Increase the count of a feature/category pair
    def incf(self,f,cat):
      self.fc.setdefault(f,{})
      self.fc[f].setdefault(cat,0)
      self.fc[f][cat]+=1

    # Increase the count of a category
    def incc(self,cat):
      self.cc.setdefault(cat,0)
      self.cc[cat]+=1

    # The number of times a feature has appeared in a category
    def fcount(self,f,cat):
      if f in self.fc and cat in self.fc[f]:
        return float(self.fc[f][cat])
      return 0.0

    # The number of items in a category
    def catcount(self,cat):
      if cat in self.cc:
        return float(self.cc[cat])
     return 0

    # The total number of items
    def totalcount(self):
      return sum(self.cc.values())

    # The list of all categories
    def categories(self):
      return self.cc.keys()

    def train(self,item,cat):
      features=self.getfeatures(item)
      # Increment the count for every feature with this category
      for f in features:
        self.incf(f,cat)

      # Increment the count for this category
      self.incc(cat)

一切都很好。我不明白为什么我收到此错误消息。

1 个答案:

答案 0 :(得分:1)

第6章文档过滤中的编程集体智慧一书将train()定义为类classifier ,因此应该在与您定义的__init__()相同的缩进级别定义它。您在__init__()内编写的所有其他方法也是如此。

所以,你的最终代码变成了:

import re
import math

def getwords(doc):
    splitter=re.compile('\\W*')
    print doc
    # Split the words by non-alpha characters
    words=[s.lower() for s in splitter.split(doc) 
          if len(s)>2 and len(s)<20]

    # Return the unique set of words only
    return dict([(w,1) for w in words])

class classifier():
    def __init__(self,getfeatures,filename=None):
        # Counts of feature/category combinations
        self.fc={}
        # Counts of documents in each category
        self.cc={}
        self.getfeatures=getfeatures

    # Increase the count of a feature/category pair
    def incf(self,f,cat):
        self.fc.setdefault(f,{})
        self.fc[f].setdefault(cat,0)
        self.fc[f][cat]+=1

    # Increase the count of a category
    def incc(self,cat):
        self.cc.setdefault(cat,0)
        self.cc[cat]+=1

    # The number of times a feature has appeared in a category
    def fcount(self,f,cat):
        if f in self.fc and cat in self.fc[f]:
            return float(self.fc[f][cat])
        return 0.0

    # The number of items in a category
    def catcount(self,cat):
        if cat in self.cc:
            return float(self.cc[cat])
        return 0

    # The total number of items
    def totalcount(self):
        return sum(self.cc.values())

    # The list of all categories
    def categories(self):
        return self.cc.keys()

    def train(self,item,cat):
        features=self.getfeatures(item)
        # Increment the count for every feature with this category
        for f in features:
            self.incf(f,cat)

        # Increment the count for this category
        self.incc(cat)