Archive

SKK用cdb作成スクリプト

私はSKKで日本語変換していて、dbskkd-cdbをサーバとして導入しています。Gentoo Linuxdbskkd-cdbを導入すると 親切にSKK-JISYO.L.cdbもダウンロードしてくれるのですが、私は自前の辞書をcdb化して使用しています。

以前は辞書ファイルをcdb化するためにmultiskkservに含まれる skkdic-p2cdbコマンドを用いていましたが、今回は自前でスクリプトを作成し てみました。perl製です。freecdbなどに付属するcdbmakeが必要となります。

使い方

下のソースコードをコピペしてcdbmake-for-skkという名前などで保存します。 例えばSKK-JISYO.Lを変換する時は、

$cdbmake-for-skk SKK-JISYO.L

とすると実行したディレクトリ内にSKK-JISYO.L.cdbができあがります。

スクリプトに自信がありませんのでユーザ権限で

$cdbmake-for-skk /usr/share/skk/SKK-JISYO.L 

などと実行して、/tmp内部にできたものをroot権限で正規の場所にコピーすることなどをおすすめします。

ライセンス

修正BSDライセンスです。

コード

#!/usr/bin/perl
# -*- cperl -*-
# Copyright(C) 2004-2009, 2310. All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the
#    distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# ===================
#   header
# ===================
use strict;
use File::Basename;

# ===================
#   global
# ===================
my $command_cdbmake = "/usr/bin/cdbmake";
# ===================
#   main
# ===================
my @contents;
my @contents_converted;

&mainsub;

sub mainsub () {
  #----- variable
  my $input_file = $ARGV[0];
  my $output_file;
  my $output_tmp_file;
  my $line;

  #----- file check
  if (! $input_file) {
    print_help_message("input skk dictionary filename");
    exit 1;
  } else {
    $output_file = basename($input_file).".cdb";
    $output_tmp_file = "$output_file.tmp";
  }

  # ----- read file
  if (!open(FILE, "<$input_file")) {
    print_help_message("can't read $input_file");
    exit 1;
  }
  @contents = <FILE>;
  close(FILE);


  # ----- convert
  convert_contents();

  # ---- cdbmake
  open(OUTPUT, "|$command_cdbmake $output_file $output_tmp_file") || die "command error";
  print OUTPUT join("\n", @contents_converted);
  print OUTPUT "\n\n";
  close(OUTPUT);
  exit 0;
}

# ===================
#   functions
# ===================
sub print_help_message () {
  my $script_name = basename($0);
  my $message = @_[0];
  print "\t$script_name is small utility to make cdb file from skk-dictionary files\n";
  print "\tUsage: $script_name <input_file_name>\n";
  if ($message) {
    print "***** $message *****\n";
  }
}

sub convert_contents () {
  my $key;
  my $key_length;
  my $value;
  my $value_length;
  foreach (@contents) {
    if ($_ =~ /^\s*;/) {
      next;
    }
    if ($_ =~ /^(\S+) +(\S.*)$/) {
      $key = $1;
      $value = $2;
      $key_length = length($key);
      $value_length = length($value);
    }
    push(@contents_converted, "+${key_length},${value_length}:${key}->${value}");
  }

}