从单词中删除重复字符 [英] Remove repeating characters from words

查看:27
本文介绍了从单词中删除重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道将haaaaapppppyyy"之类的内容转换为haappyy"的最佳方法是什么.

基本上,在解析俚语时,人们有时会重复字符以增加重点.

我想知道这样做的最佳方法是什么?使用 set() 不起作用,因为字母的顺序显然很重要.

有什么想法吗?我正在使用 Python + nltk.

解决方案

可以使用正则表达式来完成:

<预><代码>>>>进口重新>>>re.sub(r'(.)1+', r'11', "haaaaapppppyyy")'快乐'

(.)1+ 替换任何字符 (.) 后跟一个或多个相同字符(因为反向引用 1它必须是相同的)两倍的字符.

I was wondering what is the best way to convert something like "haaaaapppppyyy" to "haappyy".

Basically, when parsing slang, people sometimes repeat characters for added emphasis.

I was wondering what the best way to do this is? Using set() doesn't work because the order of the letters is obviously important.

Any ideas? I'm using Python + nltk.

解决方案

It can be done using regular expressions:

>>> import re
>>> re.sub(r'(.)1+', r'11', "haaaaapppppyyy")     
'haappyy'

(.)1+ repleaces any character (.) followed by one or more of the same character (because of the backref 1 it must be the same) by twice the character.

这篇关于从单词中删除重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆