简体   繁体   English

难以转换列表:'str' object 没有属性 'items'

[英]Difficulty converting a list: 'str' object has no attribute 'items'

I am trying to create a classifier using NLTK, however, I believe that I have a problem in the format of my data that I cannot get over.我正在尝试使用 NLTK 创建一个分类器,但是,我相信我的数据格式存在无法克服的问题。

My data looks like this:我的数据如下所示:

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

Then, I run the following code:然后,我运行以下代码:

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

But, unfortunately I have the following error.但是,不幸的是我有以下错误。 What does this error consist of and how do I overcome it?这个错误是由什么组成的,我该如何克服它?

---------------------------------------------------------------------------
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'

From the documentation:从文档中:

train(train_toks, algorithm=None, trace=3, encoding=None, labels=None, gaussian_prior_sigma=0, **cutoffs) train(train_toks, algorithm=None, trace=3, encoding=None, 标签=None, gaussian_prior_sigma=0, **cutoffs)

Train Docs 培训文件

Parameters train_toks (list) – Training data, represented as a list of pairs, the first member of which is a featureset , and the second of which is a classification label.参数 train_toks (list) – 训练数据,表示为对列表,其中第一个成员是特征集,第二个成员是分类 label。

Your tuples need to have the first element be a dict that "map[s] strings to either numbers, booleans or strings" then you need to have your second element be the classification label.您的元组需要第一个元素是“将 [s] 字符串映射到数字、布尔值或字符串”的dict ,然后您的第二个元素需要是分类 label。

from nltk.classify import maxent

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'str' object 没有属性 'items' - 'str' object has no attribute 'items' 'List' object 没有属性 'startswith' - 解决困难 - 'List' object has no attribute 'startswith' - difficulty solving Python - AttributeError: &#39;str&#39; 对象没有属性 &#39;items&#39; - Python - AttributeError: 'str' object has no attribute 'items' Robotframework - AttributeError: 'str' object 没有属性 'items' - Robotframework - AttributeError: 'str' object has no attribute 'items' AttributeError: 'str' object 没有属性 'items' (python) - AttributeError: 'str' object has no attribute 'items' (python) AttributeError: &#39;str&#39; 对象没有属性 &#39;items&#39; - AttributeError: 'str' object has no attribute 'items' python 'str' object 没有属性 'items' - python 'str' object has no attribute 'items' 为什么会出现属性错误:'str' object 没有属性 'copy'? 从 dict.items() 解包的变量并被视为 str 而不是 list。? - Why is there an Attribute Error: 'str' object has no attribute 'copy'? Variable unpacked from dict.items() and is treated as str instead of list.? AttributeError: 'str' object 没有属性 'list' - AttributeError: 'str' object has no attribute 'list' Python 3-Dictionary.items str对象没有属性项错误 - Python 3 - Dictionary.items str object has no attribute items error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM