我正在使用Python的NLP库-Spacy。我正在尝试替换文章中的名词块。名词块看起来像这样:“最安全的面部识别”。我想用这样的东西代替它们:“ the_most_secure_facial_recognition”(下划线而不是空格)
所以我写了这段代码:
import spacy
nlp = spacy.load('en_core_web_md')
data = "In the end, the notch was a relatively useless design trend for Android phones, and consumers were left " \
"wanting. The hole-punch camera seems to be a better answer. Here's a new idea that looks genuinely futuristic " \
"and hasn't been pulled off by Apple yet. It's an admission that Face ID is difficult to clone, a hole-punch " \
"won't include all the fancy sensors required for the most secure facial recognition, but consumers probably " \
"don't care that much, anyway. There's always a fingerprint sensor, after all."
doc = nlp(data)
# doc2 = doc.text
doc2 = str(doc)
for nc in doc.noun_chunks:
old_nc = str(nc)
new_nc = old_nc.replace(' ', '_')
print(old_nc)
print(new_nc)
doc2.replace(old_nc, new_nc)
print(doc2)
当我运行它时,doc2中什么也不会被替换。我在做错什么吗?
答案 0 :(得分:1)
replace
不会修改现有字符串,而是返回一个新字符串。
所以这行不通:
doc2.replace(old_nc, new_nc)
但这会:
doc2 = doc2.replace(old_nc, new_nc)
这是相关文档:
str.replace(旧,新[,计数])
返回该字符串的副本,其中所有出现的子字符串old都被new替换。如果指定了可选的参数count,则仅替换第一个出现的计数。