[英]Python remove non-latin textlines in csv
我有一个csv文件,其中包含字符串形式的文本。 一些文本行例如是中文或俄语。
我想做的是使用Python计算文本行中unicode和ASCII字符的数量。 如果ASCII与Unicode字符的比率超过90%,我想保留该行,如果不从CSV中删除它。
这背后的想法是删除所有非拉丁语言,但保留例如德国的Umlauts,为此,我要使用具有比率的解决方案。
有没有人想解决这个任务?
非常感谢你!
这是我的csv数据的一些示例:
She wants to ride my BMW the go for a ride in my BMW lol http://t.co/FeoNg48AQZ
RT @YuaElena: Бабушка лаÑково говорит 5-летнему Тёмочке: - Смотри, Темик, вон едет "би-би". - Бог Ñ Ñ‚Ð¾Ð±Ð¾Ð¹, бабка, Ñто-ж BMW 335xi 4x4.
因此,您应该了解我的数据的样子。
拉丁语的范围以\ÿ
,因此您要做的就是使用正则表达式删除\Ā-\
范围内的字符,然后将新行的长度与原始行进行比较。
也就是说,使用re.sub(r'[\Ā-\]', "?", line)
保留该行并将所有不需要的字符替换为?
可能更有用?
。
最好的选择是使用unicodedata模块。 该解决方案会占用大量资源,因为它将检查字符串中每个字符的Unicode名称。
import unicodedata
def compute_ratio(input_str):
'''
This function will return the ratio between the number of latin letter and other letters.
'''
num_latin = 0
input_str = "".join(input_str.split()) # Remove whitespaces.
for char in input_str:
try:
if unicodedata.name(unicode(char))[:5] == "LATIN":
num_latin += 1
#end if
except UnicodeDecodeError:
pass
#end try
#end for
return (num_latin*1.0)/len(input_str)
这是输入数据的用法示例。 saved_Output是一个包含所有有效行的数组。
>>> lines = '''She wants to ride my BMW the go for a ride in my BMW lol http://t.co/FeoNg48AQZ
RT @YuaElena: Бабушка лаÑково говорит 5-летнему Тёмочке: - Смотри, Темик, вон едет "би-би". - Бог Ñ Ñ‚Ð¾Ð±Ð¾Ð¹, бабка, Ñто-ж BMW 335xi 4x4.'''
>>> saved_Output = []
>>> for line in lines.split('\n'):
if compute_ratio(line) > 0.95:
saved_Output.append(line)
#end if
#end for
>>> "\n".join(saved_Output)
''
>>> compute_ratio('She wants to ride my BMW the go for a ride in my BMW lol http://t.co/FeoNg48AQZ')
0.890625
>>> # A ratio of 0.95 seems too high even for your first line.
>>> compute_ratio('this is a long string')
0.8095238095238095
>>> compute_ratio(u"c'est une longue cha\xeene")
0.8260869565217391
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.