TypeError:无法使用python3在级别级函数段上调用“list”对象

2024-04-24 00:38:28 发布

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

我似乎无法纠正这个错误。它曾经起作用,但现在不行了。代码生成一个近似的等级级别。我没有给其他任何地方分配“列表”。当然,我一直在上下的笔记本在一个相当混乱的方式旋转。你知道吗

df3是一个包含tweets的数据帧。你知道吗

我在Jupyter笔记本中使用python3.6

有什么想法吗?你知道吗

dofe公司:

# Prepare readability scores based on Flesch-Kincaid Grade Level
from re import match

# Load Carnegie Mellon Pronouncing Dictionary
cmu = n.corpus.cmudict.dict()

def reduce_word(word):
    return ''.join([x for x in word.lower() if match(r'\w', x)])

def get_syllable_count(word):
    word = reduce_word(word)
    if (not len(word)) or (not word in cmu):
        return 0
    return len([x for x in list(''.join(list(cmu[word])[-1])) if match(r'\d', x)])

def get_grade_level(text):
    """Flesch-Kincaid Grade Level formula"""
    sentences = n.tokenize.sent_tokenize(text)
    sentence_count = len(sentences)
    word_count = 0
    syllable_count = 0
    for sentence in sentences:
        words = n.tokenize.word_tokenize(sentence)
        words = [reduce_word(word) for word in words]
        words = [word for word in words if word != '']
        word_count += len(words)
        syllable_count += sum([get_syllable_count(word) for word in words])
    if word_count is 0:
        return 0
    word_count = float(word_count)
    return (0.39 * (word_count / sentence_count)
            + 11.8 * (syllable_count / word_count)
            - 15.59)

df3['grade_level'] = df3.text.apply(get_grade_level)

错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-f2e77b4afe86> in <module>()
     33             - 15.59)
     34 
---> 35 df3['grade_level'] = df3.text.apply(get_grade_level)

~/anaconda3/lib/python3.6/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   2549             else:
   2550                 values = self.asobject
-> 2551                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   2552 
   2553         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()

<ipython-input-50-f2e77b4afe86> in get_grade_level(text)
     25         words = [word for word in words if word != '']
     26         word_count += len(words)
---> 27         syllable_count += sum([get_syllable_count(word) for word in words])
     28     if word_count is 0:
     29         return 0

<ipython-input-50-f2e77b4afe86> in <listcomp>(.0)
     25         words = [word for word in words if word != '']
     26         word_count += len(words)
---> 27         syllable_count += sum([get_syllable_count(word) for word in words])
     28     if word_count is 0:
     29         return 0

<ipython-input-50-f2e77b4afe86> in get_syllable_count(word)
     12     if (not len(word)) or (not word in cmu):
     13         return 0
---> 14     return len([x for x in list(''.join(list(cmu[word])[-1])) if match(r'\d', x)])
     15 
     16 def get_grade_level(text):

TypeError: 'list' object is not callable

Tags: textinforgetlenreturnifcount