这个问题在这里已有答案:
我想在标识为NNS的单词周围添加方括号。能够将其识别为单个单词如何用句子重新加入。
import spacy, re
nlp = spacy.load('en_core_web_sm')
s = u"The cats woke up but the dogs slept."
doc = nlp(s)
for token in doc:
if (token.tag_ == 'NNS'):
print ([token])
目前的结果:
[cats]
[dogs]
预期结果:
The [cats] woke up but the [dogs] slept.
一个常见的习惯用法是使用列表来收集单词然后加入它们:
sentence = []
doc = nlp(s)
for token in doc:
if (token.tag_ == 'NNS'):
sentence.append('[' + token + ']')
else:
sentence.append(token)
sentence = ' '.join(sentence)
@John Blart,使用列表理解答案是正确的替代方法:
import spacy
nlp = spacy.load('en_core_web_sm')
s = u"The cats woke up but the dogs slept."
doc = nlp(s)
print(' '.join(['[{}]'.format(token) if token.tag_ == 'NNS' else '{}'.format(token) for token in doc])
import spacy
nlp = spacy.load('en_core_web_sm')
s = u"The cats woke up but the dogs slept."
doc = nlp(s)
sentence = []
doc = nlp(s)
for token in doc:
if (token.tag_ == 'NNS'):
sentence.append('[' + (token.text) + ']')
else:
sentence.append(token.text)
sentence = ' '.join(sentence)
print sentence
结果:
The [cats] woke up but the [dogs] slept .