正则表达式将两个由句号链接的单词分隔开

2024-06-13 12:11:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理一个文件,我发现word被一个句号链接,我认为这是一个错误,我想纠正它,所以我正在寻找正则表达式来做它

['<repdns text="boys.aussi" />']
['<repdns text="interpretation.une" />']
['<repdns text="catastrophe.michelle" />']
['<repdns text="paquerettes.ewan" />']
['<repdns text="amour.hugh" />']

我实际上读取了一个文件,并使用treetagger来获取引理,但是出现了上面这样的错误,所以我需要在使用treetagger函数之前更正它们。我一直坚持使用哪个正则表达式,因为我不想让word与“.com”或“.org”分开

a = [' boys.aussi', 'interpretation.une', 'amour.hugh', 'amy.com', 'frenchemabassy.net']

alphabet = "([a-z][...])"
alphabets = "([A-Za-z])"
prefixes = "(Mr|St|Mrs|Ms|Dr)[.]"
suffixes = "(Inc|Ltd|Jr|Sr|Co)[.]"
starters = "(M|Mr|Mme|Sr|Dr)"
acronyms = "([A-Z][.][A-Z][.](?:[A-Z][.])?)"
websites = "[.](com|net|org|io|gov)"
digits = "([0-9])"

# sépare les phrases

def normalize(text):  # do_lower=False):
    text = re.sub(alphabets + "[.]" + alphabets,)
    
    
    return text

normalize(a)

期望值

a = [' boys. aussi', 'interpretation. une', 'amour. hugh', 'amy.com', 'frenchemabassy.net']


Tags: 文件textcomnet错误wordtreetaggerinterpretation
1条回答
网友
1楼 · 发布于 2024-06-13 12:11:07

在正则表达式中使用负前瞻断言,以便将“.”替换为“.”仅当其后面没有任何特殊的Internet顶级域名时:

import re

def normalize(text):
    return re.sub(r'\.(?!(com|net|org|io|gov))', '. ', text)

a = [' boys.aussi', 'interpretation.une', 'amour.hugh', 'amy.com', 'frenchemabassy.net']
a = [normalize(s) for s in a]
print(a)

印刷品:

[' boys. aussi', 'interpretation. une', 'amour. hugh', 'amy.com', 'frenchemabassy.net']

注意,我只是在使用您websites变量中的TLD列表;还有很多你想添加的

相关问题 更多 >