5548e965de51c54a2eba1d15b2a8254dea58e7f8
[chise/perl.git] / Chise_utils / scripts / sample.pl
1 #!/usr/bin/perl -w
2
3 use Getopt::Long;  
4 use strict;
5 use vars qw($atr $value $char
6             $opt_map $opt_rev_map $opt_mapfrom $opt_mapto
7             $opt_atr $opt_allatr $opt_showatr
8             $opt_query
9             $opt_help
10             );
11 use Chise_utils ':all';
12
13 my $usage=<<EOF;
14 Usage: perl $0 [-m <map> | --map <map> |
15                 -r <reverse map> |
16                 --mapfrom <map> |
17                 --mapto <map> |
18                 -q <expression> |
19                 --show-attribute |
20                 -h | --help |
21                 -a=<atribuite> <char> |
22                 -aa <char>]
23     -q  <expression>
24       ex. of <expression>
25          total-strokes==8 means
26            the attribute of total-strokes exactly matches to 8.
27          ids-parts=~矢 means
28            the attribute of ids-parts contains the string 矢.
29          total-strokes==8,ids-parts=~矢 means both at the same time.
30     -a  get <attribute> of <char>
31     -aa get all attributes of <char>
32 EOF
33
34 &GetOptions("mapto=s"=>\$opt_mapto,
35             "mapfrom=s"=>\$opt_mapfrom,
36             "m=s"=>\$opt_map,
37             "r=s"=>\$opt_rev_map,
38             "a=s"=>\$opt_atr,
39             "aa"=>\$opt_allatr,
40             "show-attribute"=>\$opt_showatr,
41             "q"=>\$opt_query,
42             "help",\$opt_help,
43             "h",\$opt_help);
44
45 if($opt_help
46    or !((($opt_map or $opt_rev_map or $opt_mapfrom or $opt_mapto) and @ARGV==0)
47         or($opt_showatr and @ARGV==0)
48         or @ARGV==1
49         )
50    ){
51     print $usage;
52     exit 1;
53 }else{
54     $char=shift or $char="";
55 }
56
57 if($opt_mapfrom){
58     $opt_map="<=".$opt_mapfrom;
59 }elsif($opt_mapto){
60     $opt_map="=>".$opt_mapto;
61 }
62
63 if($opt_map){
64     if(&get_db($opt_map)){
65         foreach $char (sort keys %{$chardb{$opt_map}}){
66             if($chardb{$opt_map}->{$char}){
67                 print $char,"\t",$chardb{$opt_map}->{$char},"\n";
68             }
69         }
70     }else{
71         print STDERR "No map for $opt_map.\n";
72     }
73 }elsif($opt_rev_map){
74     if(&get_reverse_db($opt_rev_map)){
75         foreach $char (sort keys %{$reverse_chardb{$opt_rev_map}}){
76             if($reverse_chardb{$opt_rev_map}->{$char}){
77                 print $char,"\t",$reverse_chardb{$opt_rev_map}->{$char},"\n";
78             }
79         }
80     }else{
81         print STDERR "No map for $opt_rev_map.\n";
82     }
83 }elsif($opt_allatr){
84     $char=&de_er($char) if($char=~/\d/);
85     print $char,"\n";
86     foreach $atr (sort keys %db){
87         if($value=&get_char_attribute($char,$atr)){
88             print "  ",$atr,":",$value,"\n";
89         }
90     }
91 }elsif($opt_atr){
92     $char=&de_er($char) if($char=~/\d/);
93     print $char,"\n";
94     print "  ",$opt_atr,":",&get_char_attribute($char,$opt_atr),"\n";
95 }elsif($opt_showatr){
96 #    print "In DB\n";
97     foreach $atr (sort keys %db){
98         print "  ",$atr,"\n";
99     }
100 #    print "In reverse DB\n";
101 #    foreach $atr (sort keys %reverse_db){
102 #       print "  ",$atr,"\n";
103 #    }
104 }elsif($opt_query){
105     my $query=$char;
106     print join "\n",&get_chars_for($query),"\n";
107 }
108
109 exit 0;