替换所有连续重复的字母,忽略特定单词

Ais*_*sha 5 python regex text preprocessor

我看到很多建议使用 re (正则表达式)或 python 中的 .join 删除句子中连续重复的字母,但我想对特殊单词有例外。

例如:

我想要这句话>sentence = 'hello, join this meeting heere using thiis lllink'

像这样>'hello, join this meeting here using this link'

知道我有这个单词列表要保留并忽略重复的字母检查:keepWord = ['Hello','meeting']

我发现有用的两个脚本是:

我有一个解决方案,但我认为还有一个更紧凑、更高效的解决方案。我现在的解决方案是:

import itertools

sentence = 'hello, join this meeting heere using thiis lllink'
keepWord = ['hello','meeting']

new_sentence = ''

for word in sentence.split():
    if word not in keepWord:
        new_word = ''.join(c[0] for c in itertools.groupby(word))
        new_sentence = sentence +" " + new_word
    else:
        new_sentence = sentence +" " + word
Run Code Online (Sandbox Code Playgroud)

有什么建议么?

Wik*_*żew 1

您可以匹配列表中的整个单词keepWord,并且仅替换其他上下文中两个或多个相同字母的序列:

import re
sentence = 'hello, join this meeting heere using thiis lllink'
keepWord = ['hello','meeting']
new_sentence = re.sub(fr"\b(?:{'|'.join(keepWord)})\b|([^\W\d_])\1+", lambda x: x.group(1) or x.group(), sentence)
print(new_sentence)
# => hello, join this meeting here using this link
Run Code Online (Sandbox Code Playgroud)

查看Python 演示

正则表达式看起来像

\b(?:hello|meeting)\b|([^\W\d_])\1+
Run Code Online (Sandbox Code Playgroud)

请参阅正则表达式演示。如果组 1 匹配,则返回其值,否则,返回完整匹配项(要保留的单词)。

图案细节

  • \b(?:hello|meeting)\b-hellomeeting用单词边界包围
  • |- 或者
  • ([^\W\d_])- 第 1 组:任何 Unicode 字母
  • \1+- 对第 1 组值的一个或多个反向引用