From: moro Date: Sun, 10 Aug 2003 09:11:56 +0000 (+0000) Subject: modified samples. X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=6090f9cbd96aba6891b105e7a6a70da64b95cd52;p=chise%2Fperl.git modified samples. --- diff --git a/CHISE.pm b/CHISE.pm index 8eebbda..a0dd988 100644 --- a/CHISE.pm +++ b/CHISE.pm @@ -1,26 +1,53 @@ # # CHISE.pm by Shigeki Moro -# $Id: CHISE.pm,v 1.3 2003-03-12 16:53:59 moro Exp $ +# $Id: CHISE.pm,v 1.4 2003-08-10 09:11:56 moro Exp $ # package CHISE; use strict; +use warnings; use BerkeleyDB; our ($EXCLUSIVE, $HAVE_INTERSECTION, $PROPER_SUBSET, $PROPER_SUPERSET, $EQSET); ($EXCLUSIVE, $HAVE_INTERSECTION, $PROPER_SUBSET, $PROPER_SUPERSET, $EQSET) = (1, 2, 3, 4, 5); -my $DB_HOME = '/usr/local/lib/xemacs-21.4.10/i686-pc-linux/char-db'; +# データベースの場ø½Š ã‚’ø½Š©³ø½Š¤™æŒ‡ø½ŠªŸã€‚いずれはlibchiseに... +# データベースの全ファイルをchownしないとø»‚¢³ãˆãªã„かも +my $DB_HOME=''; +if (-e '/usr/local/lib/chise/char-db') { + $DB_HOME='/usr/local/lib/chise/char-db'; +} elsif (-e '/sw/lib/xemacs-21.4.11/powerpc-apple-darwin6.6/char-db') { + $DB_HOME = '/sw/lib/xemacs-21.4.11/powerpc-apple-darwin6.6/char-db'; +} elsif (-e '/usr/local/lib/xemacs-21.4.11/i686-pc-linux/char-db') { + $DB_HOME='/usr/local/lib/xemacs-21.4.11/i686-pc-linux/char-db'; +} elsif (-e '/usr/local/lib/xemacs-21.4.11/powerpc-apple-darwin6.4/char-db') { + $DB_HOME='/usr/local/lib/xemacs-21.4.11/powerpc-apple-darwin6.4/char-db'; +} elsif (-e '/usr/local/xemacs-utf2000/lib/xemacs-21.4.11/powerpc-apple-darwin6.4/char-db'){ + $DB_HOME='/usr/local/xemacs-utf2000/lib/xemacs-21.4.11/powerpc-apple-darwin6.4/char-db'; +} elsif (-e '/usr/local/lib/xemacs-21.4.10/i686-pc-linux/char-db') { + $DB_HOME='/usr/local/lib/xemacs-21.4.10/i686-pc-linux/char-db'; +} elsif (-e '/usr/local/lib/xemacs-21.4.10/powerpc-apple-darwin6.4/char-db') { + $DB_HOME='/usr/local/lib/xemacs-21.4.10/powerpc-apple-darwin6.4/char-db'; +} elsif (-e '/usr/local/xemacs-utf2000/lib/xemacs-21.4.10/powerpc-apple-darwin6.4/char-db'){ + $DB_HOME='/usr/local/xemacs-utf2000/lib/xemacs-21.4.10/powerpc-apple-darwin6.4/char-db'; +} elsif (-e 'd:/work/chise/char-db'){ + $DB_HOME='d:/work/chise/char-db'; +} else { + print STDERR "No database found.\n"; + print STDERR "Please set \$DB_HOME to CHISE.pm.\n"; + exit 1; +} my %alias = (); my %alias_reverse = (); for (glob "$DB_HOME/system-char-id/*") { s/^.*\/([^\/]+)$/$1/; my $i = $_; - s/\->/to_/; - s/<\-/from_/; - s/=>/map_/; + s/^\->/to_/; # Perlではリファレンスに + s/^<\-/from_/; # "-" などがø»‚¢³ãˆãªã„ため、ø½Š²ºø½Š’Žã—ておく。 + s/^=>/mapto/; + s/^=//; s/\-/_/g; $alias{$_} = $i; $alias_reverse{$i} = $_; @@ -33,7 +60,7 @@ for (glob "$DB_HOME/system-char-id/*") { #--- exportする予ø½ŠªŸã®é–¢æ•° -------------------------------------# -sub new { +sub new (@) { # 既存のø½Š²¡å­—オブジェクトの生成 my $invocant = shift; my $class = ref($invocant) || $invocant; @@ -120,11 +147,13 @@ sub compare { my($a, $b) = @_; my($aonly, $bonly, $common) = (0, 0, 0); for my $i (&cup(keys %$a, keys %$b)) { - if ($a->{$i} eq $b->{$i}) { - $common++; - } else { - $aonly++ if (exists $a->{$i}); - $bonly++ if (exists $b->{$i}); + if (exists $a->{$i} and exists $b->{$i}) { + if ($a->{$i} eq $b->{$i}) { + $common++; + } else { + $aonly++ if (exists $a->{$i}); + $bonly++ if (exists $b->{$i}); + } } } if ($common == 0) { @@ -226,10 +255,10 @@ sub chars { my @tmp = (); my $dbname = exists $alias{$attrname} ? $alias{$attrname} : $attrname; if (-f "$DB_HOME/$dbname/system-char-id") { - @tmp = (&getvalue("$DB_HOME/$dbname/system-char-id", $$self{$attrname})); + @tmp = (&getvalue("$DB_HOME/$dbname/system-char-id", $self->{$attrname})); @result = &cap(\@result, \@tmp); } elsif (-f "$DB_HOME/system-char-id/$dbname") { - @tmp = &getkeys("$DB_HOME/system-char-id/$dbname", $$self{$attrname}); + @tmp = &getkeys("$DB_HOME/system-char-id/$dbname", $self->{$attrname}); @result = &cap(\@result, \@tmp); } else { die "cannot find $attrname: $! $BerkeleyDB::Error\n"; @@ -267,7 +296,7 @@ sub getkeys ($$) { -Filename => $dbname; my @keys = (); for my $key (keys %h) { - push @keys, $key if ($h{$key} eq $value); + push @keys, $key if ($h{$key} =~ /\b$value\b/); } untie %h; return @keys; diff --git a/sample1.pl b/sample1.pl index c651543..70e11fc 100644 --- a/sample1.pl +++ b/sample1.pl @@ -1,5 +1,5 @@ use CHISE; -my $s1 = CHISE->new(ideograph_daikanwa => 6942); # 「字」 +my $s1 = CHISE->new(daikanwa => 6942); # 「字」 print $s1->dumpAttr; print $s1->strokes, "\n"; # ø½Šæ•°ã‚’表示 diff --git a/sample2.pl b/sample2.pl index 7c9833c..db18f3a 100644 --- a/sample2.pl +++ b/sample2.pl @@ -2,7 +2,7 @@ use CHISE; my $s1 = CHISE->define_char(strokes => 12, radical => 9); -my $s2 = CHISE->define_char(strokes => 12, radical => 9, ideograph_daikanwa => 694); +my $s2 = CHISE->define_char(strokes => 12, radical => 9, daikanwa => 694); my $c = $s1->compare($s2); if ($c == $CHISE::EXCLUSIVE) {