AttributeError:' list'对象没有属性' isdigit'

时间:2016-09-04 21:05:25

标签: python nltk pos-tagger

我想在熊猫中提取POS。我这样做

import pandas as pd
from nltk.tag import pos_tag
df = pd.DataFrame({'pos': ['noun', 'Alice', 'good', 'well', 'city']})
s = df['pos']
tagged_sent = pos_tag(s.str.split())

但得到追溯:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "../lib/python2.7/site-packages/nltk/tag/__init__.py", line 111, in pos_tag
    return _pos_tag(tokens, tagset, tagger)
  File "../lib/python2.7/site-packages/nltk/tag/__init__.py", line 82, in _pos_tag
    tagged_tokens = tagger.tag(tokens)
  File "/Users/mjpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/nltk/tag/perceptron.py", line 152, in tag
    context = self.START + [self.normalize(w) for w in tokens] + self.END
  File "../lib/python2.7/site-packages/nltk/tag/perceptron.py", line 224, in normalize
    elif word.isdigit() and len(word) == 4:
AttributeError: 'list' object has no attribute 'isdigit'

出了什么问题?

2 个答案:

答案 0 :(得分:2)

您实际上可以直接将Series对象传递给pos_tag()方法:

s = df['pos']
tagged_sent = pos_tag(s)  # or pos_tag(s.tolist())
print(tagged_sent)

打印:

[('noun', 'JJ'), ('Alice', 'NNP'), ('good', 'JJ'), ('well', 'RB'), ('city', 'NN')]

答案 1 :(得分:0)

表达式s.str.split()list字符串,而非字符串(由pos_tag预期。因为isdigitstr的方法。