1

基于给定的输入:

我可以做得更好 waaaaaaaaaaaay :DDDD!!!! 我对此感到非常兴奋:)))好!

期望:输出

我可以做得更好/液化天然气:D/液化天然气!/液化天然气我很兴奋/液化天然气对此感到兴奋:)/液化天然气好!/液化天然气

--- 挑战:

  1. 更好 vs. soooooooooo >> 我们需要保持第一个不变,但缩短第二个
  2. 其次,我们需要添加一个标签(LNG),因为它可能对加强主观性和情绪有一定的重要性

---- 问题:错误消息“不平衡括号”

有任何想法吗?

我的代码是:

import re 

lengWords = {} # a dictionary of lengthened words 

def removeDuplicates(corpus):

data = (open(corpus, 'r').read()).split()
myString = " ".join(data)

for word in data:
    for chr in word: 
        countChr = word.count(chr)
        if countChr >= 3: 
            lengWords[word] = word+"/LNG"
            lengWords[word] = re.sub(r'([A-Za-z])\1+', r'\1', lengWords[word])
            lengWords[word] = re.sub(r'([\'\!\~\.\?\,\.,\),\(])\1+', r'\1', lengWords[word])                             

    for k, v in lengWords.items():
        if k == word: 
            re.sub(word, v, myString)
return myString
4

1 回答 1

1

这不是完美的解决方案,但我现在没有时间完善它 - 只是想让你开始使用简单的方法:

s = "I can do waaaaaaaaaaaaay better :DDDD!!!! I am sooooooooo exicted about it :))) Good !!"
re.sub(r'(.)(\1{2,})',r'\1/LNG',s)
>> 'I can do wa/LNGy better :D/LNG!/LNG I am so/LNG exicted about it :)/LNG Good !!'
于 2012-05-28T17:30:15.137 回答