获取AttributeError:在Python代码2中

2024-10-03 17:22:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在读《编程集体智能》一书,第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)

一切都很好。我不明白为什么会收到这个错误消息。你知道吗


Tags: oftheinselfforreturndefcount
1条回答
网友
1楼 · 发布于 2024-10-03 17:22:49

第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)

相关问题 更多 >