私の仕事の目的は、句読点の前後にスペースを追加することです。現在、反復str.replace()
を使用して各句読点p
をに置き換えてい" "+p+" "
ます。2つのリストまたは辞書を渡すだけで同じ出力を得るにはどうすればよいですかstr.translate()
:
inlist = string.punctuation
outlist = [" "+p+" " for p in string.punctuation]
inoutdict = {p:" "+p+" " for p in string.punctuation}
私が持っているすべての句読点がstring.punctuation
. 現在、私はそのようにやっています:
from string import punctuation as punct
def punct_tokenize(text):
for ch in text:
if ch in deupunct:
text = text.replace(ch, " "+ch+" ")
return " ".join(text.split())
sent = "This's a foo-bar sentences with many, many punctuation."
print punct_tokenize(sent)
また、この反復str.replace()
には時間がかかりすぎstr.translate()
ます。もっと速くなりますか?