From: moro Date: Tue, 28 Jan 2003 06:28:45 +0000 (+0000) Subject: New file X-Git-Tag: utf2000pm_0_1 X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d3ceafc4fb4a244eb62c6e9a1fc21a635c47f251;p=chise%2Fperl.git New file --- d3ceafc4fb4a244eb62c6e9a1fc21a635c47f251 diff --git a/UTF2000.pm b/UTF2000.pm new file mode 100644 index 0000000..bc7f123 --- /dev/null +++ b/UTF2000.pm @@ -0,0 +1,150 @@ +package UTF2000; + +use strict; +use BerkeleyDB; + +my $DB_HOME = '/usr/local/lib/xemacs-21.4.10/i686-pc-linux/char-db'; + +my %alias = (); +for (glob "$DB_HOME/system-char-id/*") { + s/^.*\/([^\/]+)$/$1/; + my $i = $_; + s/\->/to_/; + s/<\-/from_/; + s/=>/map_/; + s/\-/_/g; + $alias{$_} = $i; +} +%alias = ( + %alias, + 'radical','ideographic-radical', + 'strokes','ideographic-strokes', +); + +#--------------------------------------------------------# + +sub new { + my $invocant = shift; + my $class = ref($invocant) || $invocant; + my $self = { @_ }; + return bless $self, $class; +} + +sub dumpAttr { + my $self = shift; + for my $i (keys %$self) { + print "$i => $$self{$i}\n"; + } +} + +sub addAttr { + my $model = shift; + my $self = $model->new(%$model, @_); + return $self; +} + +sub delAttr (@) { + my $model = shift; + delete $$model{$_} foreach (@_); + my $self = $model->new(%$model); + return $self; +} + +# +for my $attrname (keys %alias) { + my $slot = __PACKAGE__ . "::$attrname"; + no strict "refs"; + *$slot = sub { + my $self = shift; + my @result; + for my $i ($self->chars) { + my $j = &getvalue("$DB_HOME/system-char-id/$alias{$attrname}", $i); + push @result, $j if $j; + } + return @result; + } +} + +sub utf8 { + # UTF-8をø½Š²¿ã™ + my $self = shift; + my @result; + for my $i ($self->chars) { + $i =~ s/^\?//; + # To Do: 私用ø½Š¸¯åŸŸã®ã¯ø½Š²¿ã•ãªã„ようにしないと。 + push @result, $i; + } + return @result; +} + +#--------------------------------------------------------# + +sub chars { + # ?... をø½Š²¿ã™ + my $self = shift; + my @result = ('*'); + for my $attrname (keys %$self) { + 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})); + @result = &cap(\@result, \@tmp); + } elsif (-f "$DB_HOME/system-char-id/$dbname") { + @tmp = &getkeys("$DB_HOME/system-char-id/$dbname", $$self{$attrname}); + @result = &cap(\@result, \@tmp); + } else { + die "cannot find $attrname: $! $BerkeleyDB::Error\n"; + } + } + return @result; +} + +sub getvalue ($$) { + # キーから値をø½Šž­ã‚Šå‡ºã™ + my($dbname, $key) = @_; + tie my %h, "BerkeleyDB::Hash", + -Filename => $dbname; + my $value = $h{$key}; + untie %h; + return $value; +} + +sub getkeys ($$) { + # 値からキーの配列をø½Šž­ã‚Šå‡ºã™ + my($dbname, $value) = @_; + tie my %h, "BerkeleyDB::Hash", + -Filename => $dbname; + my @keys = (); + for my $key (keys %h) { + push @keys, $key if ($h{$key} eq $value); + } + untie %h; + return @keys; +} + +sub cap { + # 2つの配列の積ø½ŠŸ›åˆã‚’求める + my($a, $b) = @_; + if (!@$a or !@$b) { + return (); + } elsif ($$a[0] eq '*') { # '*'は全体ø½ŠŸ›åˆ + return @$b; + } elsif ($$b[0] eq '*') { + return @$a; + } else { + my %result = (); + my @result = (); + for my $i (@$a, @$b) { + $result{$i}++; + push @result, $i if ($result{$i} == 2); + } + return @result; + #return grep { + # my $c = $_; + # grep /^$c$/, @$b; + #} @$a; + } +} + +#--------------------------------------------------------# +1;