OSDN Git Service

d9cef0d441a60a824952ce5f92b6cf79ebc46064
[o-dic/o-dic.git] / script / ODIC.pm
1 #
2 # 沖縄辞書フォーマット共通ライブラリー
3 #
4
5 package ODIC;
6
7 use strict;
8 use Encode;
9
10 our $MAX_PHONATE = 40;
11 our $MAX_WORD    = 64;
12
13 sub check_phonate {
14     my $phonate = shift;
15
16     if (length(Encode::decode('utf-8', $phonate)) > $MAX_PHONATE) {
17         print STDERR "Warning: $.: too long phonate `$phonate'\n";
18     }
19     if ($phonate =~ /[^あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもらりるれろがぎぐげござじずぜぞだぢづでどばびぶべぼぁぃぅぇぉっょゃゅゎぱぴぷぺぽやゆよわをんヴー]/) {
20         print STDERR "Warning: $.: ilegal character in `$phonate'\n";
21     }
22 }
23
24 sub check_word {
25     my $word = shift;
26
27     if (length(Encode::decode('utf-8', $word)) > $MAX_WORD) {
28         print STDERR "Warning: $.: too long word `$word'\n";
29     }
30     if ($word =~ /[ \t",#]/) {
31         print STDERR "Warning: $.: ilegal character in `$word'\n";
32     }
33 }
34
35 sub to_eucjp {
36     my $utf8_string = Encode::decode('utf-8', shift);
37     return Encode::encode('euc-jp', $utf8_string);
38 }
39
40 sub to_shiftjis {
41     my $utf8_string = Encode::decode('utf-8', shift);
42     # 'cp932' は、「~」の変換でおかしくなるので使用しない。
43     return Encode::encode('shift_jis', $utf8_string);
44 }
45
46 1;
47 __END__
48
49 =head1 NAME
50
51 ODIC - 沖縄辞書 <http://www.zukeran.org/o-dic/> フォーマット向けの共通関数
52
53 =head1 EXAMPLE
54
55   require 'ODIC.pm';
56
57   while (<>) {
58     next if (/^\s*$|^\s*\#.*$/);        # 空行・コメントのみの行を読み飛ばす
59
60     if (/^(\S+)\s+(\S+)\s+(\S+)\s+#\s*([[:^cntrl:]]*).*$/) {
61       my $phonate = $1; # 読み
62       my $word    = $2; # 単語
63       my $class   = $3; # 品詞
64       my $comment = $4; # コメント
65       ODIC::check_phonate($phonate);
66       ODIC::check_word($word);
67       &convert_class;
68
69     } elsif (/^(\S+)\s+(\S+)\s+(\S+)/) {
70       my $phonate = $1; # 読み
71       my $word    = $2; # 単語
72       my $class   = $3; # 品詞
73       my $comment = ''; # コメント
74       ODIC::check_phonate($phonate);
75       ODIC::check_word($word);
76       &convert_class;
77
78     } else {
79       print STDERR "Error: $.: too few field number `$_'\n";
80       print  "$_";
81     }
82   }
83   exit 0;
84
85   sub convert_class {
86     print "$phonate\t$word\t$class\t$comment\n";
87   }
88
89 =head1 LICENSE
90
91 Public domain.
92
93 =cut