我在这样的数据帧中有一列.
Text
"Lorum Ipsum Rotterdam dolor sit."
"ed ut perspiciatis Boekarest, New York, consectetur adipiscing elit, sed "
"Excepteur sint occaecat Glasgow cupidatat non proident, sunt in culpa"
我希望每个地理位置都被"GPE"所取代.
我正在使用spacy检测实体.这很好,如下所示.
nlp = spacy.load('en_core_web_lg')
for value in df['text']:
doc = nlp(value)
for ent in doc.ents:
print(ent.text, ent.label_)
Output:
Rotterdam GPE
Boekarest GPE
New York GPE
Glasgow GPE
为了替换列中的城市名称,我try 了下面的代码,但它不起作用.
for value in df['text']:
doc = nlp(value)
for ent in doc.ents:
for word in value.split():
if ent.label_ == "GPE":
word.replace(ent.label, "_GPE_")
有人知道我做错了什么吗?