69721


# 実現したいこと

以下のように、ダッシュはカンマに変えて、ハイフンはそのままにする処理がしたいです。

[code]

    #input
    'A former employee of the accused company - - offered a statement off the record.', #two dashes
    'He is afraid of two things — spiders and senior prom - they are.', #dash
    'Fifty-six bottles of pop on the wall, fifty-six bottles of pop.' #hyphen
    
    #output
    'A former employee of the accused company, offered a statement off the record.', #two dashes
    'He is afraid of two things, spiders and senior prom, they are.', #dash
    'Fifty-six bottles of pop on the wall, fifty-six bottles of pop.' #hyphen
    
[/code]

# 問題

今のコードの出力だと、ハイフンはそのままですが、ダッシュを上記の実現したい例のように出力できていません。

[code]

    ['A former employee of the accused company - - offered a statement off the record.', 'He is afraid of two thingsspiders and senior prom - they are.', 'Fifty-six bottles of pop on the wall, fifty-six bottles of pop.']
    
[/code]

# 実行しているコード

[code]

    samples = [
        'A former employee of the accused company - - offered a statement off the record.', #two dashes
        'He is afraid of two things — spiders and senior prom - they are.', #dash
        'Fifty-six bottles of pop on the wall, fifty-six bottles of pop.' #hyphen
    ]
    
    def remove_dash_preserve_hyphen(str_sentence, dash_signatures=[' — ']):
        for dash_sig in dash_signatures:
            str_sentence = str_sentence.replace(dash_sig, '')
        return str_sentence
    
    samples_without_dash = [remove_dash_preserve_hyphen(sentence) for sentence in samples]
    
    print(samples_without_dash)
    
[/code]

# 試したこと

以下のコードでも、求めたい出力が得られず、どのように修正すべきかアドバイスをいただきたいです。  
ただし、二重リストではなく一重リスト（実行中の現在の出力の形）で求めたいです。

[code]

    samples = [
        'A former employee of the accused company - - offered a statement off the record.', #two dashes
        'He is afraid of two things — spiders and senior prom - they are.',　#dash
        'Fifty-six bottles of pop on the wall, fifty-six bottles of pop.' #hyphen
    ]
    output = [[
                   sentence.strip() for sentence in elem.replace(chr(8212), ',').split(',') 
                   if sentence.strip()
                  ] for elem in samples]
    output
    
[/code]

出力

[code]

    [['A former employee of the accused company - - offered a statement off the record.'],
     ['He is afraid of two things', 'spiders and senior prom - they are.'],
     ['Fifty-six bottles of pop on the wall', 'fifty-six bottles of pop.']]
    
[/code]

# 環境

Python 3.7.4


CC BY-SA 4.0
2020-08-20T08:45:48.320
69719
2020-08-20T09:12:28.217
question
python python3 自然言語処理 文字コード
ハイフンとダッシュを区別した上でダッシュのみカンマに変える処理がうまくいかない問題について


求める出力を正規表現で得る方法はいかがでしょうか。

[code]

    import re
    
    samples = [
        'A former employee of the accused company - - offered a statement off the record.', #two dashes
        'He is afraid of two things — spiders and senior prom - they are.', #dash
        'Fifty-six bottles of pop on the wall, fifty-six bottles of pop.' #hyphen
    ]
    
    pattern = re.compile(r'\s+(?:[-—]\s)+')
    samples_without_dash = [pattern.sub(', ', sentence) for sentence in samples]
    
    print("\n".join(samples_without_dash))
    
[/code]

出力結果:

[code]

    A former employee of the accused company, offered a statement off the record.
    He is afraid of two things, spiders and senior prom, they are.
    Fifty-six bottles of pop on the wall, fifty-six bottles of pop.
    
[/code]


CC BY-SA 4.0
2020-08-20T09:12:28.217
69721
2020-08-20T09:12:28.217
9820
69719
answer