662be4e5893ce898b0f9f4e919981f79646dfa49
[chise/kage.git] / glyphwiki / detect_ko.pl
1 use utf8;
2 binmode STDOUT, ":utf8";
3
4 $target_lang = "ko";
5 $target_cur = "$target_lang.txt";
6 $target_unused = "$target_lang.unused.txt";
7 $target_done = "$target_lang.done.txt";
8 $target_notyet = "$target_lang.notyet.txt";
9 $target_done_new = "$target_lang.done.new.txt";
10 $target_notyet_new = "$target_lang.notyet.new.txt";
11
12 $done = "";
13 $notyet = "";
14 $result = "";
15
16 if(-e $target_cur || -e $target_unused || -e $target_done_new || -e $target_notyet_new){
17   print "Error. Clean files first.\n";
18   exit;
19 }
20
21 %current = ();
22 open(my $fh, "<:utf8", $target_notyet);
23 while(<$fh>){
24   if($_ =~ m/^[:!]/){
25     next;
26   }
27   if($_ =~ m/^(\r\n|\r|\n)$/){
28     next;
29   }
30   $ja = $_;
31   $ja =~ s/(\r\n|\r|\n)$//;
32   $local = <$fh>;
33   $local =~ s/(\r\n|\r|\n)$//;
34   if($ja ne $local){
35     $current{$ja} = $local;
36   }
37 }
38 close $fh;
39 open(my $fh, "<:utf8", $target_done);
40 while(<$fh>){
41   if($_ =~ m/^[:!]/){
42     next;
43   }
44   if($_ =~ m/^(\r\n|\r|\n)$/){
45     next;
46   }
47   $ja = $_;
48   $ja =~ s/(\r\n|\r|\n)$//;
49   $local = <$fh>;
50   $local =~ s/(\r\n|\r|\n)$//;
51   if($ja ne $local){
52     $current{$ja} = $local;
53   }
54 }
55 close $fh;
56 %current2 = %current;
57
58 @files = ();
59 push(@files, "/var/www/glyphwiki.org/index.cgi");
60 opendir($dh, "/home/kamichi/glyphwiki/") or die $!;
61 foreach(grep(/\.pl$/, readdir($dh))){
62   if($_ !~ m/^en./){
63     push(@files, "/home/kamichi/glyphwiki/$_");
64   }
65 }
66 closedir($dh);
67
68 foreach(@files){
69         $fn = $_;
70         %cand = ();
71         open(my $fh, "<:utf8", $_);
72   $line = 1;
73   while(<$fh>){
74     if($_ =~ m/^[ \t]*([^\#]*[^ -~\r\n\t]+[^\#]*)(\#.*)?[\r\n]+$/){
75       if($_ !~ m/\#.*ja_mama/){
76         $cand{$1} .= $line.",";
77       }
78     }
79                 $line++;
80   }
81   close($fh);
82   $buffer1 = "";
83   $buffer2 = "";
84   $buffer3 = "";
85   foreach(sort({$cand{$a} <=> $cand{$b}} keys(%cand))){
86     if(exists($current{$_})){
87       $buffer1 .= ":".substr($cand{$_}, 0, length($cand{$_}) - 1)."\n$_\n$current{$_}\n";
88       $buffer3 .= ":".substr($cand{$_}, 0, length($cand{$_}) - 1)."\n$_\n$current{$_}\n";
89       delete($current2{$_});
90     } else {
91       $buffer2 .= ":".substr($cand{$_}, 0, length($cand{$_}) - 1)."\n$_\n$_\n";
92       $buffer3 .= ":".substr($cand{$_}, 0, length($cand{$_}) - 1)."\n$_\n$_\n";
93     }
94   }
95   $done .= "!".$fn."\n".$buffer1."\n";
96   $notyet .= "!".$fn."\n".$buffer2."\n";
97   $result .= "!".$fn."\n".$buffer3."\n";
98 }
99
100 $unused = "";
101 foreach(sort({$current2{$a} <=> $current2{$b}} keys(%current2))){
102   $unused .= ":\n$_\n$current2{$_}\n";
103 }
104
105 open(my $fh, ">:utf8", $target_cur);
106 print $fh $result;
107 close $fh;
108 open(my $fh, ">:utf8", $target_done_new);
109 print $fh $done;
110 close $fh;
111 open(my $fh, ">:utf8", $target_notyet_new);
112 print $fh $notyet;
113 close $fh;
114 open(my $fh, ">:utf8", $target_unused);
115 print $fh $unused;
116 close $fh;