我有一个充满副词的文本,它的替换如下:
adverbe1 |replacement1
adverbe2 |replacement2
adverbe3 |replacement3
我想在我的文本中替换副词:
示例:
'Hello adverbe1 this is a test' to be this : 'Hello replacement1 this is a test'
但我已经没有解决方案了,我的代码到目前为止:
adverbes = open("list_adverbes_replacement.txt", encoding="utf-8")
list_adverbes = []
list_replacement = []
for ad in adverbes.readlines():
if ad != '' and ad.split('|')[0].strip(' ')[-3:] == 'ent':
list_adverbes.append(ad.split('|')[0].strip(' '))
list_replacement.append(ad.split('|')[1])
pattern = r"(\s+\b(?:{}))\b".format("|".join(list_adverbes))
data = re.sub(pattern, r"\1", data)
我找不到用适当的替换词替换每个副词的方法
list_adverbes_replacement.txt
是我在开始时给出的文本,我正在寻找一个正则表达式解决方案,我只是不知道我缺少了什么
简洁的方法。为替换项构建键/值对字典
然后使用regex'
re.sub
替换它们,方法是对每个单词进行匹配,在字典中查找单词,如果单词不在字典中,则默认为单词本身给定这样的副词:
用它创建一个字典,其中key是副词,value是替换文本
现在迭代每个键,只需使用相应的值对给定键的文本调用replace:
输出:
可以使用副词和替换词初始化字典
dct
看起来像{'adverbe1': 'replacement1', 'adverbe2': 'replacement2', 'adverbe3': 'replacement3'}
然后,
pip install triegex
(或者使用来自Speed up millions of regex replacements in Python 3的这个解决方案)来简化动态正则表达式的构建和使用这个演示字典的模式是
\b(?:adverbe(?:1\b|2\b|3\b)|~^(?#match nothing))
,它将adverbe1
、adverbe2
、adverbe3
作为整个单词进行匹配lambda x: dct[x.group()]
是re.sub
的替换参数,它获取相应的替换值相关问题 更多 >
编程相关推荐