[英]How can I iterate on a column with spacy to get named entities?
I got a dataframe with a column named "categories".我得到了一个 dataframe ,其中有一列名为“类别”。 Some data of this column looks like this {[], [], [amazon], [clothes], [telecommunication],[],...}
.该列的一些数据看起来像这样{[], [], [amazon], [clothes], [telecommunication],[],...}
。 Every row has only one of this values.每行只有一个值。 My task is now to give this values their entities.我现在的任务是为这些值赋予它们的实体。 I tried a lot but it didn't go well.我尝试了很多,但它没有 go 很好。 This was my first attempt这是我的第一次尝试
import spacy
nlp = spacy.load("de_core_news_sm")
doc=list(nlp.pipe(df.categories))
print([(X.text, X.label_) for X in doc.ents])
AttributeError 'list' object has no attribute 'ents'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in ----> 1 print([(X.text, X.label_) for X in doc.ents])
AttributeError: 'list' object has no attribute 'ents'
My second attempt:我的第二次尝试:
for token in doc:
print(token.doc, token.pos_, token.dep_)
AttributeError 'spacy.tokens.doc.Doc' object has no attribute 'pos_'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in 1 for token in doc: ----> 2 print(token.doc, token.pos_, token.dep_)
AttributeError 'spacy.tokens.doc.Doc' object has no attribute 'pos_'
Third attempt:第三次尝试:
docs = df["categories"].apply(nlp)
for token in docs:
print(token.text, token.pos_, token.dep_)
AttributeError 'spacy.tokens.doc.Doc' object has no attribute 'docs'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in 1 docs = df["categories"].apply(nlp) 2 for token in docs: ----> 3 print(token.docs, token.pos_, token.dep_)
AttributeError: 'spacy.tokens.doc.Doc' object has no attribute 'docs'
I just want to iterate spacy on this column to give me for the values an entity.我只想在此列上迭代 spacy,以便为我提供一个实体的值。 For the empty values it should give me no entity.对于空值,它不应该给我任何实体。 The column is a string.该列是一个字符串。 Thanks for help.感谢帮助。
You have list with many doc
and you have to use extra for
-loop to work with every doc separatelly.您有许多doc
的列表,您必须使用额外for
-loop 来分别处理每个文档。
docs = list(nlp.pipe(df.categories)) # variable `docs` instead of `doc`
for doc in docs:
print([(X.text, X.label_) for X in doc.ents])
and和
docs = list(nlp.pipe(df.categories)) # variable `docs` instead of `doc`
for doc in docs:
for token in doc:
print(token.text, token.pos_, token.dep_)
Documentations Language Processing Pipelines shows it like文档语言处理管道显示它像
for doc in nlp.pipe(df.categories):
print([(X.text, X.label_) for X in doc.ents])
for token in doc:
print(token.text, token.pos_, token.dep_)
And the same problem is with apply(nlp)
同样的问题是apply(nlp)
docs = df["categories"].apply(nlp)
for doc in docs:
for token in doc:
print(token.text, token.pos_, token.dep_)
Full working example:完整的工作示例:
import spacy
import pandas as pd
df = pd.DataFrame({
'categories': ['amazon', 'clothes', 'telecommunication']
})
nlp = spacy.load("de_core_news_sm")
print('\n--- version 1 ---\n')
docs = list(nlp.pipe(df.categories))
for doc in docs:
print([(X.text, X.label_) for X in doc.ents])
for token in doc:
print(token.text, token.pos_, token.dep_)
print('\n--- version 2 ---\n')
docs = df["categories"].apply(nlp)
for doc in docs:
for token in doc:
print(token.text, token.pos_, token.dep_)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.