转换列表的困难:'str' 对象没有属性 'items'

尤里布兹

我正在尝试使用 NLTK 创建一个分类器,但是,我相信我的数据格式存在无法解决的问题。

我的数据如下所示:

data = [("TEXT 1", 'no'), ("TEXT 2", 'yes'), ("TEXT 3", 'no'), ("TEXT 4", 'no'), ("TEXT 5", 'yes')]

然后,我运行以下代码:

 import nltk
    from nltk.classify import maxent
    
    classifier = maxent.MaxentClassifier.train(data, bernoulli=False, max_iter=10)

但是,不幸的是我有以下错误。这个错误包括什么,我如何克服它?

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-93-e1b29adbeebb> in <module>
----> 1 classifier = maxent.MaxentClassifier.train(data, bernoulli=False, max_iter=10)

/usr/local/lib/python3.8/dist-packages/nltk/classify/maxent.py in train(cls, train_toks, algorithm, trace, encoding, labels, gaussian_prior_sigma, **cutoffs)
    324         algorithm = algorithm.lower()
    325         if algorithm == "iis":
--> 326             return train_maxent_classifier_with_iis(
    327                 train_toks, trace, encoding, labels, **cutoffs
    328             )

/usr/local/lib/python3.8/dist-packages/nltk/classify/maxent.py in train_maxent_classifier_with_iis(train_toks, trace, encoding, labels, **cutoffs)
   1175     # Construct an encoding from the training data.
   1176     if encoding is None:
-> 1177         encoding = BinaryMaxentFeatureEncoding.train(train_toks, labels=labels)
   1178 
   1179     # Count how many times each feature occurs in the training data.

/usr/local/lib/python3.8/dist-packages/nltk/classify/maxent.py in train(cls, train_toks, count_cutoff, labels, **options)
    665 
    666             # Record each of the features.
--> 667             for (fname, fval) in tok.items():
    668 
    669                 # If a count cutoff is given, then only add a joint

AttributeError: 'str' object has no attribute 'items'
亨利·埃克

从文档:

火车(train_toks,算法=无,trace=3,编码=无,标签=无,gaussian_prior_sigma=0,**cutoffs)

培训文档

参数 train_toks (list) – 训练数据,表示为对列表,其中第一个成员是featureset,第二个成员是分类标签。

您的元组需要将第一个元素设为dict“将 [s] 字符串映射到数字、布尔值或字符串”,然后您需要将第二个元素设为分类标签。

from nltk.classify import maxent

data = [({"TEXT 1": 'no'}, "Label")]
classifier = maxent.MaxentClassifier.train(data, bernoulli=False, max_iter=10)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

python'str'对象没有属性'items'

Google数据流作业在writeToBiqquery步骤上失败:“列表”对象和“ str”对象没有属性“ items”

Python-AttributeError:“ str”对象没有属性“ items”

如何修复“ AttributeError:'str'对象没有属性'items'”

Python AttributeError:“ str”对象没有属性“ items”

str对象没有属性“ *”

“str”对象没有属性“str”

AttributeError:'str'对象没有属性'str'

使用pysolr将数据保存到solr中会被拒绝'AttributeError:'str'对象没有属性'items'

如何修复从PubSub读取并写入BigQuery的数据流管道中的“ AttributeError:'str'对象没有属性'items'”

python AttributeError中的类型转换:'str'对象没有属性'astype'

转换时发生Coremltools错误:“'str'对象没有属性'decode'”

即使在列表上操作,Python“str”对象也没有属性“append”?

属性错误:“str”对象没有属性“str”

Argparse:str对象没有属性

AttributeError:“ str”对象没有属性“ name”

AttributeError:'str'对象没有属性'errno'

AttributeError:'str'对象没有属性'union'

'str'对象没有属性'is_authenticated'

例外:'str' 对象没有属性 'text'

Django'str'对象没有属性'get'

AttributeError: 'str' 对象没有属性 'loc'

AttributeError:“ str”对象没有属性“ ndim”

熊猫-'dataframe'对象没有属性'str'

'str'对象没有属性'attname'

AttributeError:“ str”对象没有属性“ values”

AttributeError:'str'对象没有属性'xpath'

AttributeError:“ str”对象没有属性“ decode”

Scrapy:“ str”对象没有属性“ iter”