在动词标签中使用默认函数计数
您能解释一下为什么 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
set_value()
函数被弃用。作为替换,您可以使用“ .AT”,例如:df.at [“ yourIndex”,“ yourColumn”] =“ yourvalue”。
另外,您在此行上有问题
verbs = defaultDict(calculate_the_word_types)
用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 -
Output -