如何在字符串周围添加标点符号? [重复]

问题描述 投票:0回答:3

这个问题在这里已有答案:

我想在标识为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.
python regex spacy
3个回答
3
投票

一个常见的习惯用法是使用列表来收集单词然后加入它们:

sentence = []
doc = nlp(s)
for token in doc:
    if (token.tag_ == 'NNS'):
        sentence.append('[' + token + ']')
    else:
        sentence.append(token)

sentence = ' '.join(sentence)

2
投票

@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])

0
投票
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 .
© www.soinside.com 2019 - 2024. All rights reserved.