在动词标签中使用默认函数计数

发布于 2025-02-04 11:56:25 字数 1254 浏览 4 评论 0原文

您能解释一下为什么 defaultdict 函数在下面的代码中未定义动词?谷歌谷歌告诉我语法很好。请帮忙!预先感谢:)

我的代码

import spacy
from collections import defaultdict
nlp = spacy.load("en_core_web_sm")
!pip install pandas==0.24.2 --user
import pandas as pd

def calculate_the_word_types(df):
  verbs = defaultdict(calculate_the_word_types)

for i, row in df.iterrows():
  doc = nlp(row["text"])


for v in map(lambda x: x.lemma_, filter(lambda x: (x.pos_ == 'AUX') | (x.pos_ == 'VERB'), doc)):
  verbs[v] += 1
  df.at(i, "nr_verb", len(list(map(lambda x: x.text,
                                                 filter(lambda x: (x.pos_ == 'AUX') | (x.pos_ == 'VERB'), doc)))))

return df
verbs

错误代码

NameError                                 Traceback (most recent call last)
<ipython-input-32-7e7c626bb331> in <module>()
     13 
     14 for v in map(lambda x: x.lemma_, filter(lambda x: (x.pos_ == 'AUX') | (x.pos_ == 'VERB'), doc)):
---> 15   verbs[v] += 1
     16   df.at(i, "nr_verb", len(list(map(lambda x: x.text,
     17                                                  filter(lambda x: (x.pos_ == 'AUX') | (x.pos_ == 'VERB'), doc)))))

NameError: name 'verbs' is not defined

Could you please explain why the defaultdict function does not define verbs in the code following? Googling tells me that the syntax is fine. Please help! Thanks in advance :)

My code

import spacy
from collections import defaultdict
nlp = spacy.load("en_core_web_sm")
!pip install pandas==0.24.2 --user
import pandas as pd

def calculate_the_word_types(df):
  verbs = defaultdict(calculate_the_word_types)

for i, row in df.iterrows():
  doc = nlp(row["text"])


for v in map(lambda x: x.lemma_, filter(lambda x: (x.pos_ == 'AUX') | (x.pos_ == 'VERB'), doc)):
  verbs[v] += 1
  df.at(i, "nr_verb", len(list(map(lambda x: x.text,
                                                 filter(lambda x: (x.pos_ == 'AUX') | (x.pos_ == 'VERB'), doc)))))

return df
verbs

Error code

NameError                                 Traceback (most recent call last)
<ipython-input-32-7e7c626bb331> in <module>()
     13 
     14 for v in map(lambda x: x.lemma_, filter(lambda x: (x.pos_ == 'AUX') | (x.pos_ == 'VERB'), doc)):
---> 15   verbs[v] += 1
     16   df.at(i, "nr_verb", len(list(map(lambda x: x.text,
     17                                                  filter(lambda x: (x.pos_ == 'AUX') | (x.pos_ == 'VERB'), doc)))))

NameError: name 'verbs' is not defined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末的微笑 2025-02-11 11:56:25

set_value()函数被弃用。

作为替换,您可以使用“ .AT”,例如:df.at [“ yourIndex”,“ yourColumn”] =“ yourvalue”。
另外,您在此行上有问题verbs = defaultDict(calculate_the_word_types)
用0初始化,因为它将充当计数器。

并修复您的凹痕

工作代码 -

import spacy
from collections import defaultdict
nlp = spacy.load("en_core_web_sm")
!pip install pandas==0.24.2 --user
import pandas as pd

def calculate_the_word_types(df):
    verbs = defaultdict(lambda: 0)

    # count all tokens, but not the punctuations
    for i, row in df.iterrows():
        doc = nlp(row["text"])

        # count only the verbs
        for v in map(lambda x: x.lemma_, filter(lambda x: (x.pos_ == 'AUX') | (x.pos_ == 'VERB'), doc)):
            verbs[v] += 1
        df.at[i, "nr_verb"] = len(list(map(lambda x: x.text, 
                                        filter(lambda x: (x.pos_ == 'AUX') | (x.pos_ == 'VERB'), doc)))) 

    return df


# dataframe
df = pd.DataFrame({'text':['hello there', 'I love Tatooine', 'I hate sands']})

# print the dataframe with verb count
print(calculate_the_word_types(df))

输出 -

        text      nr_verb
0   hello there     0.0
1   I love Tatooine 1.0
2   I hate sands    1.0

set_value() function is deprecated.

As replacement, you can use ".at" like this: df.at["YOURINDEX", "YOURCOLUMN"] = "YOURVALUE".
Also, you have issue on this line verbs = defaultdict(calculate_the_word_types)
initialize with 0 since it will act as counter.

And fix your indentation

working code -

import spacy
from collections import defaultdict
nlp = spacy.load("en_core_web_sm")
!pip install pandas==0.24.2 --user
import pandas as pd

def calculate_the_word_types(df):
    verbs = defaultdict(lambda: 0)

    # count all tokens, but not the punctuations
    for i, row in df.iterrows():
        doc = nlp(row["text"])

        # count only the verbs
        for v in map(lambda x: x.lemma_, filter(lambda x: (x.pos_ == 'AUX') | (x.pos_ == 'VERB'), doc)):
            verbs[v] += 1
        df.at[i, "nr_verb"] = len(list(map(lambda x: x.text, 
                                        filter(lambda x: (x.pos_ == 'AUX') | (x.pos_ == 'VERB'), doc)))) 

    return df


# dataframe
df = pd.DataFrame({'text':['hello there', 'I love Tatooine', 'I hate sands']})

# print the dataframe with verb count
print(calculate_the_word_types(df))

Output -

        text      nr_verb
0   hello there     0.0
1   I love Tatooine 1.0
2   I hate sands    1.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文