LoginSignup
1
3

More than 3 years have passed since last update.

Pythonでモールス符号の日本語辞書作成

Last updated at Posted at 2021-02-21

はじめに

Pythonのモールス符号の日本語辞書を作成しました。
辞書の元にする情報はGoogleのGoogle 日本語入力 - モールスバージョンを参照させていただきました。

基本方針

Google 日本語入力 - モールスバージョンの和文モールス符号表の

部分のHTMLソースコードをパースし、Pythonの辞書形式にする。

パース関数

には、'-'なら、class='dah', '.'なら、class='dit'となっている。
この点に着目し、パースする。
def google_morse_parser(msg):
    parsed_dict = {}
    soup = bs(msg, 'html.parser')
    soup_td = soup.find('table').find_all('td')
    for i in soup_td:
        if i.text is not None:
            if '(' in i.text:
                key = i.text.split('(')[1].split(')')[0]
            else:
                key = i.text
            soup_span = i.find_all('span')
            value = ''
            for span in soup_span:
                dahdit = span.get('class')
                if dahdit == ['dah']:
                    value += '-'
                elif dahdit == ['dit']:
                    value += '.'
            parsed_dict[key] = value
    return parsed_dict

出力結果

{'ア': '--.--', 'イ': '.-', 'ウ': '..-', 'エ': '-.---', 'オ': '.-...', 'カ': '.-..', 'キ': '-.-..', 'ク': '...-', 'ケ': '-.--', 'コ': '----', 'サ': '-.-.-', 'シ': '--.-.', 'ス': '---.-', 'セ': '.---.', 'ソ': '---.', 'タ': '-.', 'チ': '..-.', 'ツ': '.--.', 'テ': '.-.--', 'ト': '..-..', 'ナ': '.-.', 'ニ': '-.-.', 'ヌ': '....', 'ネ': '--.-', 'ノ': '..--', 'ハ': '-...', 'ヒ': '--..-', 'フ': '--..', 'ヘ': '.', 'ホ': '-..', 'マ': '-..-', 'ミ': '..-.-', 'ム': '-', 'メ': '-...-', 'モ': '-..-.', 'ヤ': '.--', '': '', 'ユ': '-..--', 'ヨ': '--', 'ラ': '...', 'リ': '--.', 'ル': '-.--.', 'レ': '---', 'ロ': '.-.-', 'ワ': '-.-', 'ヰ': '.-..-', 'ヱ': '.--..', 'ヲ': '.---', 'ン': '.-.-.', 'ー': '.--.-', '゛': '..', '゜': '..--.', '、': '.-.-.-'}

ソースコード

GitHub: make_morse_dict.py

参考文献

Google 日本語入力 - モールスバージョン

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3