【问题标题】:Join negative particle加入负粒子
【发布时间】:2021-11-17 11:40:40
【问题描述】:

如何在python中的下一个单词上附加否定粒子(对于所有可以是“no”的文本)?

例如,使这个字符串['This is not apple']

进入这个:['This is not_apple']

【问题讨论】:

  • 您的输入是一个列表,而不是一个字符串(您可能需要更新您的问题以澄清)

标签: python join nlp


【解决方案1】:

你可以使用正则表达式:

\bnot\s+(?=\w) 匹配单词not(但不是其他以not 结尾的单词)后跟一个或多个空格和另一个单词。

import re

s = 'This is not apple'

s2 = re.sub(r'\bnot\s+(?=\w)', 'not_', s)

输出:'This is not_apple'

有一个列表
import re

l = ['This is not apple']
[re.sub(r'\bnot\s+(?=\w)', 'not_', s) for s in l]

输出:['This is not_apple']

【讨论】:

  • 谢谢,它有效!这正是我所拥有的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 2011-11-15
  • 2012-06-28
  • 1970-01-01
相关资源
最近更新 更多