gpt4 book ai didi

Python 用字典值替换字符串

转载 作者:太空宇宙 更新时间:2023-11-03 19:17:47 27 4
gpt4 key购买 nike

基于给定的输入:

I can do waaaaaaaaaaaaay better :DDDD!!!! I am sooooooooo exicted about it :))) Good !!

期望:输出

I can do way/LNG better :D/LNG !/LNG I am so/LNG exicted about it :)/LNG Good !/LNG

--- 挑战:

  1. 更好 vs. sooooooooooo >> 我们需要保持第一个不变,但缩短第二个
  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

最佳答案

这不是完美的解决方案,但我现在没有时间改进它 - 只是想让您以简单的方法开始:

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 !!'

关于Python 用字典值替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10787487/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com