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']
我发现有用的两个脚本是:
使用.join:
import itertools
sentence = ''.join(c[0] for c in itertools.groupby(sentence))
Run Code Online (Sandbox Code Playgroud)
使用正则表达式:
import re
sentence = re.compile(r'(.)\1{1,}').sub(r'\1', sentence)
Run Code Online (Sandbox Code Playgroud)
我有一个解决方案,但我认为还有一个更紧凑、更高效的解决方案。我现在的解决方案是:
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)
有什么建议么?
您可以匹配列表中的整个单词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)
正则表达式看起来像
\b(?:hello|meeting)\b|([^\W\d_])\1+
Run Code Online (Sandbox Code Playgroud)
请参阅正则表达式演示。如果组 1 匹配,则返回其值,否则,返回完整匹配项(要保留的单词)。
图案细节
\b(?:hello|meeting)\b-hello或meeting用单词边界包围|- 或者([^\W\d_])- 第 1 组:任何 Unicode 字母\1+- 对第 1 组值的一个或多个反向引用