python - 提取动词短语中的平均单词数

标签 python spacy

所以我有一个有点愚蠢的问题,但作为 Python 的新手,我自己似乎找不到答案。我使用 spaCy 的匹配器提取了动词短语。现在,我希望获得每个人文本中提取的动词短语中的平均单词数,并将它们存储在新的数据框列中。为此,我正在尝试创建一个函数,然后将其应用于所述数据框列。

我创建了这个函数:

def get_length_phrases(column):
    for phrase in column:
        phrase_length = len(phrase)
        mean_length = np.mean(phrase_length)
    return mean_length

问题是,当我将它应用于存储动词短语的列时,我得到如下所示的输出:

0      1.0
1      1.0
2      1.0
3      1.0
4      1.0
      ... 
235    1.0
236    1.0
237    1.0
238    1.0
239    1.0
Name: verb_phrases_length, Length: 240, dtype: float64

问题是,每个短语有不止一个词,很明显,我做错了什么,但似乎无法弄清楚是什么...... statistics.mean 也不起作用......

最佳答案

np.mean() 将数组(或类似数组)作为参数。据我所知(如果我错了请纠正我)你得到的是每个阶段的长度的平均值,这只是一个数字,一个数字的平均值就是那个数字.

来自 numpy docs:

Parameters: a:array_like - Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.

您将要将每个长度保存到一个列表中,然后将其提供给 np.mean()

def get_length_phrases(column):
    phrase_lengths = []
    for phrase in column:
        phrase_lengths.append(len(phrase))
    mean_length = np.mean(phrase_lengths)
    return mean_length

如果此时您仍然得到 1.0,则可能是获取短语的问题,而不是此函数的问题。

关于python - 提取动词短语中的平均单词数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72929507/

相关文章:

python - 如何在 Tkinter 菜单中获取 Mac "command"符号

python - 如何迭代 python 中的列以查找键值的匹配项?

python-3.x - 属性错误 : 'PathDistribution' object has no attribute 'name'

python - 获取扩展的 spaCy 形态信息

Python:将字符串时间字典转换为日期时间

python - 从 str 转换为 float 时保持尾随 0

python - 警告 : [W108] The rule-based lemmatizer did not find POS annotation for the token 'This'

nlp - Spacy中的依存解析树

python - 在 Python 中扫描一组 URL 的最快方法是什么?

machine-learning - 使用 spaCy 进行额外的命名实体识别需要多少训练数据量?