【问题标题】:TypeError: 'LazyCorpusLoader' object is not callableTypeError:“LazyCorpusLoader”对象不可调用
【发布时间】:2015-10-19 21:49:29
【问题描述】:

我正在测试 NLTK 并尝试训练 punkttokenizer,我正在尝试获得 GWBush-2005 和 6 州工会演讲,并且我得到 Lazycorpusloader 不可调用。

代码:

import nltk
from nltk.corpus import state_union
from nltk.tokenize import PunktSentenceTokenizer

train_text = state_union("2005-GWBush.txt")
sample_text = state_union("2006-GWBush.txt")

custom_sent_tokenizer = PunktSentenceTokenizer(train_text)

tokenized = custom_sent_tokenizer.tokenize(sample_text)

def process_content():
    try:
        for i in tokenized:
            words = nltk.word_tokenize(i)
            tagged = nltk.pos_tag(words)
            print tagged
    except Exception as e:
            print(str(e))

process_content()

错误:

Traceback (most recent call last):
  File "C:\Users\smash_000\My Documents\Li-Clipse-Python\CodeTesting\blahblahblah\smallcodetesting.py", line 7, in <module>
    train_text = state_union("2005-GWBush.txt")
TypeError: 'LazyCorpusLoader' object is not callable

【问题讨论】:

    标签: python python-2.7 nltk typeerror


    【解决方案1】:

    想通了。因为我使用的是 python 2.7,所以它有点不同。我手动调用单词,然后将它们编码为枯萎的 ascii 或 UTF-8,因为它是从网站上抓取的,通常是 unicode 格式,因此需要我对其进行编码。

    这是所需的代码片段。

    train_text = nltk.corpus.state_union.words("2005-GWBush.txt")
    sample_text = nltk.corpus.state_union.words("2006-GWBush.txt")
    
    for words in train_text:
        train_text = words.encode("ascii")
    for wordes in sample_text:
        sample_text = wordes.encode("ascii")
    

    【讨论】:

      【解决方案2】:
         from nltk.corpus import state_union
         nltk.download("state_union")
         nltk.download("averaged_perceptron_tagger")
        from nltk.tokenize import PunktSentenceTokenizer
        train_text = state_union.raw("2005-GWBush.txt")
        sample_text = state_union.raw("2006-GWBush.txt")
      
       custom_sent_tokenizer = PunktSentenceTokenizer(train_text)
      
       tokenized = custom_sent_tokenizer.tokenize(sample_text)
      
      def process_content():
      try:
          for i in tokenized:
              words = nltk.word_tokenize(i)
              tagged = nltk.pos_tag(words)
              print(tagged)
          except Exception as e:
              print(str(e))
      
      process_content()
      

      试试这个...使用 .raw 来使用来自 state_union 的文本数据

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-25
        • 2021-04-15
        • 2011-10-01
        • 2020-11-10
        • 2017-09-09
        • 2016-07-23
        相关资源
        最近更新 更多