2017-06-22 7 views
1

アルファベットの文字の前に句読点がある場合、関数は前にスペースを入れ、アルファベットの文字の後に句読点がある場合はその後ろにスペースを入れてください。しかし、整数の場合にはそうはならない。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) 

それが一致してどこに、しかし、私はこの点で修正する方法がわからない全体句読点を置き換える:

エラーが下の行にあるように思えます!

これが何をすべき

答えて

0

あなたの問題は、replace関数が各句読点文字の置換を行います。

あなたがutterance代わり の各文字を反復処理し、適切な交換に新しいtarget文字列を構築することができます。

def normalize(utterance): 

    # Converting to lowercase & removing multiple white spaces 
    utterance = ' '.join(utterance.lower().split()) 
    #  List of punctuations 
    punctuations_list = [',','.','?',':',';','!',')','(','\''] 


    target = utterance[0] 
    for i in range(1, len(utterance) -1): 
     ch = utterance[i] 
     char_before = utterance[i-1] 
     char_after = utterance[i+1] 
     if ch in punctuations_list and not char_before.isdigit() and char_before not in punctuations_list: 
      target += " " 
     target += ch 
     if ch in punctuations_list and not char_after.isdigit() and char_after not in punctuations_list: 
      target += " " 
    target += utterance[-1] 
    return target 
+0

これは間違った出力を与えています: 'ハンク・ユー:? 1時30分に会う –

+0

ありがとう。それを更新しました – taras

0

utterance = utterance.replace(punctuation, "" + punctuation)

編集

私が述べたように、あなたの代わりにすべての句読点のあなたの文章内のすべての文字を経る必要があります。私はいくつかの修正を加えましたが、あなたはまだ私が行ったことから倍増するスペースに対処しなければなりません。

あなたはこのようなものだろう:

def normalize(utterance): 

    # Converting to lowercase & removing multiple white spaces 
    utterance = ' '.join(utterance.lower().split()) 
    print utterance 

#  List of punctuations 
    punctuations_list = [',','.','?',':',';','!',')','(','\''] 

    for punctuation in utterance: 
     if punctuation in punctuations_list: 
      print punctuation 

      try: 
       char_before = str(utterance[utterance.index(punctuation) -1]) 
       char_after = str(utterance[utterance.index(punctuation) +1]) 
      except IndexError: 
       char_after = "0" 

      print char_before 

      if char_before.isdigit()==False and char_before not in punctuations_list: 
       utterance = utterance.replace(char_before+punctuation, char_before+" " + punctuation) 

      if char_before.isdigit()==True:     
       utterance = utterance.replace(punctuation, "" + punctuation) 

      if char_after.isdigit()==False and char_after not in punctuations_list: 
       utterance = utterance.replace(punctuation+char_after, punctuation + " "+char_after) 

    return utterance 

print normalize("thank you:? the time is 2:30pm") 
+0

問題は、あなたがpuctuationリストを以下の各句読点の前に文字を選択しているということですこの句読点の最初の出現後に。この同じ文字は、あなたの文全体で同じ種類のすべての句読点について考慮されています。 forループは、句読点リストの代わりに元の文章のすべての文字を通過するように変更する必要があります。 – Diego

+0

これを明確にしていただきありがとうございます。 –

+0

あなたは大歓迎です!もっと調整が必要ですが、今はもっと簡単になると思います。 – Diego

0

あなたが使用することができregex

import re 

def normalize(text): 
    return re.sub(r"(?<=[a-zA-Z])(?=[,.?:;!()'])|(?<=[,.?:;!()'])(?=[a-zA-Z])", ' ', text) 

この関数は、文字a-zA-Zで前または後のキャラクター,.?:;!()'の一つ見つけを、そして挿入間にスペース。

0

index()、次にfind()のドキュメントをご覧ください。

検索():

戻りサブサブサブが完全S [スタート:終了を]に含まれるように見出されるs内の最小のインデックス。失敗したら-1を返します。開始と終了のデフォルトと負の値の解釈は、スライスの場合と同じです。私は疑う

あなたはchar_beforechar_afterを設定するには、インデックスを()を使用しているので、あなただけのutteranceに存在する任意の他のインスタンスを残して、句読点の最初のインスタンスのためにそれをやっています。ループバックしてこの最初のインスタンス以上を探すことはありません。