如何在字符串周围添加标点符号?

How to add punctuation around string?

我想在标识为 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 .