created.
[chise/perl.git] / CHISE_REG.pm
1 package CHISE_REG;
2
3 use strict;
4 use warnings;
5 use utf8;
6 use overload;
7
8 sub import {
9   overload::constant ( 'qr' => \&ChiseLikeRegex )
10 }
11
12 sub unimport {
13   overload::remove_constant
14 }
15
16 # データベースの場を指。いずれはlibchiseに...
17 # データベースの全ファイルをchownしないとえないかも
18 my $DB_HOME='';
19 if (-e '/usr/local/lib/chise/char-db') {
20   $DB_HOME = '/usr/local/lib/chise/char-db';
21 } elsif (-e '/sw/lib/xemacs-21.4.11/powerpc-apple-darwin6.6/char-db') {
22   $DB_HOME = '/sw/lib/xemacs-21.4.11/powerpc-apple-darwin6.6/char-db';
23 } elsif (-e '/usr/local/lib/xemacs-21.4.11/i686-pc-linux/char-db') {
24   $DB_HOME = '/usr/local/lib/xemacs-21.4.11/i686-pc-linux/char-db';
25 } elsif (-e '/usr/local/lib/xemacs-21.4.11/powerpc-apple-darwin6.4/char-db') {
26   $DB_HOME = '/usr/local/lib/xemacs-21.4.11/powerpc-apple-darwin6.4/char-db';
27 } elsif (-e '/usr/local/xemacs-utf2000/lib/xemacs-21.4.11/powerpc-apple-darwin6.4/char-db'){
28   $DB_HOME = '/usr/local/xemacs-utf2000/lib/xemacs-21.4.11/powerpc-apple-darwin6.4/char-db';
29 } elsif (-e '/usr/local/lib/xemacs-21.4.10/i686-pc-linux/char-db') {
30   $DB_HOME = '/usr/local/lib/xemacs-21.4.10/i686-pc-linux/char-db';
31 } elsif (-e '/usr/local/lib/xemacs-21.4.10/powerpc-apple-darwin6.4/char-db') {
32   $DB_HOME = '/usr/local/lib/xemacs-21.4.10/powerpc-apple-darwin6.4/char-db';
33 } elsif (-e '/usr/local/xemacs-utf2000/lib/xemacs-21.4.10/powerpc-apple-darwin6.4/char-db'){
34   $DB_HOME = '/usr/local/xemacs-utf2000/lib/xemacs-21.4.10/powerpc-apple-darwin6.4/char-db';
35 } elsif (-e 'd:/work/chise/char-db'){
36   $DB_HOME = 'd:/work/chise/char-db';
37 } else {
38   print STDERR "CHISE.pm: No database found.\n";
39   print STDERR "CHISE.pm: Please set \$DB_HOME to CHISE.pm.\n";
40   exit 1;
41 }
42
43 #--- 正規表現のCHISE的拡張 ------------------------------------#
44
45 sub ChiseLikeRegex ($) {
46 # 正規表現のオーバーロード
47   my ($RegexLiteral) = @_;
48   #print STDERR "BEFORE: $RegexLiteral\n"; # for debug
49   $RegexLiteral
50     =~ s/\\same_([^_]+)_(\d)/(??{CHISE_REG->chise_backref(\$$2,'$1')})/g;
51   #print STDERR "AFTER:  $RegexLiteral\n"; # for debug
52   return $RegexLiteral;
53 }
54
55 sub chise_backref {
56   my $self = shift;
57   my ($backreference, $feature_name) = @_;
58   #print STDERR "backreference: $backreference\n"; # for debug
59   #print STDERR "feature name:  $feature_name\n";  # for debug
60   my $result = `./chisereg.pl $backreference $DB_HOME $feature_name`;
61   print STDERR $result, "\n"; # for debug
62   return "[$result]";
63 }
64
65 1;