OSDN Git Service

Initial commit
[wordring-tm/wordring-tm.git] / third_party / mecab-0.996 / tests / t9 / mkdic.pl
1 #!/usr/bin/perl -w
2
3 my $FACTOR = 200;
4
5 sub toCost()
6 {
7     my $prob = shift @_;
8     return int(-log($prob) * $FACTOR);
9 }
10
11 sub char2num {
12     my $n = ord(shift @_);
13     return 1 if ($n >= 0x30A1 && $n <= 0x30AA);
14     return 2 if ($n >= 0x30AB && $n <= 0x30B4);
15     return 3 if ($n >= 0x30B5 && $n <= 0x30BE);
16     return 4 if ($n >= 0x30BF && $n <= 0x30C9);
17     return 5 if ($n >= 0x30CA && $n <= 0x30CE);
18     return 6 if ($n >= 0x30CF && $n <= 0x30DD);
19     return 7 if ($n >= 0x30DF && $n <= 0x30E2);
20     return 8 if ($n >= 0x30E3 && $n <= 0x30E8);
21     return 9 if ($n >= 0x30E9 && $n <= 0x30ED);
22     return 0 if ($n >= 0x30EE && $n <= 0x30F3);
23     return 0 if ($n == 0x30FC);
24     return -1;
25 }
26
27
28 while (<>) {
29     chomp;
30     next if /^EOS/;
31     s/^\s+//g;
32     my ($freq, $s) = split;
33     utf8::decode($s);
34     my $length = length($s);
35     
36     my @list;
37     push @list, [ (-1, "_") ];
38     for (my $i = 0; $i < $length; ++$i) {
39         my $k = substr($s, $i, 1);
40         my $n = &char2num($k);
41         utf8::encode($k);
42         goto NEXT if ($n == -1);
43         push @list, [ ($n, $k) ];
44     }
45
46     push @list, [ (-1, "_") ];
47
48     for (my $i = 0; $i < $#list; ++$i) {
49         my ($n0, $w0) = @{$list[$i]};
50         my ($n1, $w1) = @{$list[$i+1]};
51         $e1{$w0} += $freq;
52         $e2{$w0}{$w1} += $freq;
53     }
54
55   NEXT:
56 }
57
58 # mkid
59 my %e3;
60 my $id = 0;
61 for (sort keys %e1) {
62     $e3{$_} = $id;
63     ++$id;
64 }
65
66 open (S, "> dic.csv") || die;
67 for $w0 (keys %e1) {
68     my $k = $w0;
69     utf8::decode($k);
70     my $n = &char2num($k);
71     next if ($n == -1);
72     printf S "%s,%d,%d,0,%s\n", $n, $e3{$w0}, $e3{$w0}, $w0;
73 }
74 close (S);
75
76 open(S, "> matrix.def") || die;
77 my $size = scalar keys %e3;
78 print S "$size $size\n";
79 my @l = sort keys %e3;
80 for my $w0 (@l) {
81     my $f1 = $e1{$w0} || 0.0;
82     for my $w1 (@l) {
83         my $f2 = $e2{$w0}{$w1} || 0.0;;
84         my $c = &toCost(1.0 * ($f2 + 0.5) / ($f1 + $size * 0.5));
85         printf S "%d %d %d\n", $e3{$w0}, $e3{$w1}, $c;
86     }
87 }
88 close(S);