我有几千个解析不好的文本文件,它们在长度的10%到30%之间显示了一些有趣的行为。不幸的是,我没有原始数据,因此无法尝试重新解析,但几乎每个文件都需要(部分清理)
输入示例
text = 'The European l a n g u a g es ar e members of the same fa m i l y
. Their sep a rate e xi ste nce is a myth . F or s c i e n c e , music,
sport , etc, Europe uses the s a m e v oca bula ry. The languages o n l y d
i f f e r i n t heir grammar, their pro nu n c iation and their most common
words. Everyone realizes why a new common language would be desirable: one could
refuse to pay expensive translators.'
预期产量
'The European languages are members of the same family. Their separate existence
i s a myth. For science, music, sport, etc, Europe uses the same vocabulary. The
languages only differ in their grammar, their pronunciation and their most
common words. Everyone realizes why a new common language would be desirable:
one could refuse to pay expensive translators.'
从一种奇怪的格式到另一种格式似乎没有太多规律性,也没有明确的“原因”或触发词或符号。我注意到一件事:格式奇怪的单词被两个空格隔开(除了有时在标点符号之前,但这是一个简单的text.replace(' ,',',')
)。你知道吗
问题
如何从一个双空格对之间的字符串中删除所有空格?我想有一个正则表达式,我只是没有想过。。。你知道吗
更多信息
我不知道每个文档中有多少奇怪的部分/字母,我也不知道文档的内容。我唯一有把握的是:
我尝试过创建一个正则表达式来与re.sub()
一起使用,但是我没有得到任何结果-没有匹配(最新的尝试是(?<= )[a-z]* (.* [a-z]*)(?= )
,但这不起作用)也没有替换组。你知道吗
谢谢你!你知道吗
如果没有模式,请给出一些建议:
在建议2中,检查单词是否为单词。如果没有,则添加下一个字符并再次检查。一直这样直到你找到一个词。它不会适用于每个单词,但“l a n g u a g es”会变成“语言”,除了“la”和“lan”。所以,即使你找到一个单词,也要不断添加字符,直到它再次变成一个单词,或者限制在16个字符左右。你知道吗
在伪代码中:
将所有空间替换为多个空间
基于单个空格将字符串拆分为数组
循环浏览每个单词
检查英语中是否有单词
添加字符直到找到匹配项
移到下一个单词
对于标点符号,如果标点符号位于字符的开头或两个空格之间,请删除前面的空格字符。你知道吗
How to check if a word is an English word with Python?
我会分三步来做(如果你按照选项做的话,可以分五步):
text.replace(' *','(@)')
(星号前三个空格)。将所有这些空格对(或多于两个)转换成某些标记,您可以确定这些标记不会出现在文本中(我以(@)
为例),如demo1所示。这是为了避免将两个(或更多)空间序列视为单个空间的序列(如下我们将删除这些序列)text.replace(' ','')
。将所有单个空格转换为空字符串,如demo2所示。这将连接示例文本中由单个空格分隔的许多单词,请小心。text.replace('\(@\)',' ')
。将第一步中的所有标记转换为单个空格,如demo3。你知道吗text.replace(' *([.!?]) *([A-Z])','. $1')
。如果你也将所有的点后跟一个大写字符转换成一个点,后跟两个空格和匹配的大写字符,那么你会得到一个更漂亮的外观。如demo4。你知道吗text.match(' *([,;:]) *','$1 ')')
。对其他标点符号执行相同的操作,但只使用一个空格。你知道吗您可以使用
sed(1)
执行此操作,如下所示:最后一个例子还将
[,;:]
转换为它们加上一个空格,并对?
和!
标记进行句子分隔。你知道吗不要考虑两个之间的空间。。。这与两个或多个相同,只是
text.replace(' *',' ')
(在*
之前有三个空格),或者用两个的字符串替换两个或多个空格的字符串。同样可以通过text.replace(' +',' ')'
(在+
之前的两个空格)实现。你知道吗相关问题 更多 >
编程相关推荐