0
我在Python的工作文字,並有缺字的幾種情況,如像:添加缺少的人物句子
test_list = ['people can t believe','we couldn t be happier','let s not forget']
在test_list所有撇號丟失,我寫了一個函數再次添加:
def add_apostrophe(sentense):
words = sentense.split()
fixed_s = []
flag = False
buffer_ = ''
for w in reversed(words):
if flag:
fixed_s.append(''.join([w,buffer_]))
flag = False
buffer_ = ''
elif w in ['t','s']:
flag = True
buffer_ = "'{}".format(w)
else:
fixed_s.append(w)
fixed_s = ' '.join(reversed(fixed_s))
return fixed_s
這類工程:
[add_apostrophe(s) for s in test_list]
["people can't believe", "we couldn't be happier", "let's not forget"]
但我認爲這可能會破壞句子一些CA對於我來說,我還沒有對它做過詳盡的測試。 此外,這似乎是一個常見問題,是一些圖書館來恢復丟失撇號和一些其他字符?
選中此鏈接。也許你可以從這裏得到一些線索:https://www.hackerrank.com/contests/nov13/challenges/punctuation-corrector-its/leaderboard – MYGz