ubuntu日本語変換Mozcに郵便辞書を追加
mozcで住所変換しようとしたら、Mozcプロパティで「郵便番号変換」にチェックが入っていても変換されない。辞書データがないのが原因。
これを追加した手順をメモしておきます。

郵便番号データをダウンロード
「日本郵便 郵便番号データダウンロード」で検索して、下記ページからデータをダウンロードします。
https://www.post.japanpost.jp/zipcode/download.html
一番上、カタカナのふりがな付きデータを「全国一括」でダウンロードしました。


ken_all.zipファイルがダウンロードされます。展開すると、KEN_ALL.CSVファイルが展開されます。

辞書データへの変換スクリプトを作成
変換スクリプトを作成します。郵便番号データのzipファイルを解凍して、KEN_ALL.CSVがあるディレクトリに作ります。pythonコードになります。
taka@cfnx2:~/ダウンロード/ken_all$ vim convert_zip.py
内容は以下の通り、catコマンドで表示させています。
taka@cfnx2:~/work/mozc_dic$ cat convert_zip.py
import csv
def convert_zipcode_to_mozc_dict():
input_file = 'KEN_ALL.CSV'
output_file_7 = 'zipcode_j_7.txt'
output_file_3_4 = 'zipcode_j_3_4.txt'
with open(input_file, 'r', encoding='shift_jis') as infile, \
open(output_file_7, 'w', encoding='utf-8') as outfile_7, \
open(output_file_3_4, 'w', encoding='utf-8') as outfile_3_4:
reader = csv.reader(infile)
for row in reader:
postal_code = row[2] # 7桁の郵便番号
address = f"{row[6]}{row[7]}{row[8]}" # 住所
reading = row[3] + row[4] + row[5] # カタカナ読み
# 7桁形式の出力
outfile_7.write(f"{postal_code}\t{address}\t名詞\n")
# 3-4桁形式の出力
postal_code_hyphen = f"{postal_code[:3]}-{postal_code[3:]}"
outfile_3_4.write(f"{postal_code_hyphen}\t{address}\t名詞\n")
print("変換が完了しました")
convert_zipcode_to_mozc_dict()
辞書データ作成
変換スクリプトを実行します。
taka@cfnx2:~/ダウンロード/ken_all$ python3 convert_zip.py
変換が完了しました
以下のように、zipcode_j_3_4.txtと zipcode_j_7.txtが作成されています。
taka@cfnx2:~/work/mozc_dic$ ls
KEN_ALL.CSV convert_zip.py zipcode_j_3_4.txt zipcode_j_7.txt
辞書ファイルは以下のような形式のファイルになります。

辞書のインポート
Mozcプロパティから「ユーザ辞書の編集」をクリック。

開いた辞書ツールで、「管理→新規辞書にインポート」を選択します。

郵便番号7桁
郵便番号3-4桁
の辞書名で、それぞれ、zipcode_j_7.txt、zipcode_j_3_4.txtのファイルをインポートします。



これで、7桁を入力しても、3-4桁を入力しても住所に変換してくれます。