アルファベットの文字の前に句読点がある場合、関数は前にスペースを入れ、アルファベットの文字の後に句読点がある場合はその後ろにスペースを入れてください。しかし、整数の場合にはそうはならない。Python Text Parsing&Splitting
def normalize(utterance):
# Converting to lowercase & removing multiple white spaces
utterance = ' '.join(utterance.lower().split())
# List of punctuations
punctuations_list = [',','.','?',':',';','!',')','(','\'']
for punctuation in punctuations_list:
if punctuation in utterance:
try:
char_before = str(utterance[utterance.index(punctuation) -1])
char_after = str(utterance[utterance.index(punctuation) +1])
except IndexError:
char_after = "0"
if char_before.isdigit()==False and char_before not in punctuations_list:
utterance = utterance.replace(punctuation, " " + punctuation)
if char_after.isdigit()==False and char_after not in punctuations_list:
utterance = utterance.replace(punctuation, punctuation + " ")
return utterance
normalize("thank you:? the time is 2:30pm")
>>>'thank you :? the time is 2 :30pm'
私が欲しいの出力は次のとおりです:
'thank you :? the time is 2:30pm'
が1つの句読点であるが、同じ句読点が繰り返されていないとき、それ自体が以下の私のコードを見ると例えば
("thanks." >>> "thanks ." and "hello?123!lom" >>> "hello ?123! lom")
私の次のコードは正常に動作します
つまり、時間の間にスペースがない場合、問題はコロン ":"が繰り返されているためです。誰かがこれを修正できますか?
utterance = utterance.replace(punctuation, " " + punctuation)
それが一致してどこに、しかし、私はこの点で修正する方法がわからない全体句読点を置き換える:
エラーが下の行にあるように思えます!
これが何をすべき
これは間違った出力を与えています: 'ハンク・ユー:? 1時30分に会う –
ありがとう。それを更新しました – taras