moytrage

group_dump_by_symbols.pl

Mar 4th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.31 KB | None | 0 0
  1. # A helper tool for hacking code pages of MT
  2. # Outputs pretty-printed HTML grouping phrases examples by unknown symbols and highlighting those symbols
  3. # Red symbols are unknown symbols of the current group, blue - of some other group
  4.  
  5. # All non-ru/en examples of use:
  6. # perl group_dump_by_symbols.pl 24 dump/dutch.dump    >html/dutch.html
  7. # perl group_dump_by_symbols.pl 26 dump/estonian.dump >html/estonian.html
  8. # perl group_dump_by_symbols.pl  4 dump/french.dump   >html/french.html
  9. # perl group_dump_by_symbols.pl  3 dump/german.dump   >html/german.html
  10. # perl group_dump_by_symbols.pl 23 dump/italian.dump  >html/italian.html
  11. # perl group_dump_by_symbols.pl 27 dump/latvian.dump  >html/latvian.html
  12. # perl group_dump_by_symbols.pl  5 dump/spanish.dump  >html/spanish.html
  13.  
  14.  
  15. use strict;
  16. use warnings;
  17. use URI::Escape;
  18.  
  19. our %desired_range = map { $_ => 1 } (0x80..0xFF); # Which range of bytes considered to be unknown set of symbols
  20. our $max_num_examples = 20; # How many examples per group
  21. our %examples;
  22. our $lang_id = ($ARGV[0] + 0) or die "First argument should be language id.";
  23.  
  24. binmode $_ for *STDIN, *STDOUT;
  25.  
  26. $examples{$_} = [] for keys %desired_range;
  27.  
  28. open my $f, "<$ARGV[1]" or die "Can't open for reading [$ARGV[1]].";
  29. binmode $f;
  30.  
  31. while (<$f>) {
  32.     chomp;
  33.     my $s = $_;
  34.     my @bytes = unpack("C*", $s);
  35.  
  36.     for my $byte (@bytes) {
  37.         push @{$examples{$byte}}, $s if $desired_range{$byte} and @{$examples{$byte}} < $max_num_examples;
  38.     }
  39. }
  40.  
  41. close $f;
  42.  
  43. # ISO-8859-1
  44. print "<html><meta charset=\"windows-1251\"><style type=\"text/css\">a:link{text-decoration:none;}</style><body><font family=\"Courier\"><pre>\n";
  45.  
  46. for my $byte (sort keys %examples) {
  47.     my $zbyte = sprintf("%02X", $byte);
  48.     my $bytesym = chr($byte);
  49.     print("<a name=\"sym_$zbyte\">$zbyte</a> [$bytesym]\n");
  50.  
  51.     for my $s (sort @{$examples{$byte}}) {
  52.         my $orig_word = $s;
  53.  
  54.         for my $b (keys %desired_range) {
  55.             my $bsym = chr($b);
  56.             my $zb = sprintf("%02X", $b);
  57.             my $color = ($b == $byte) ? 'red' : 'blue';
  58.             $s =~ s/$bsym/<a href="#sym_$zb" title="$zb"><font color="$color">$bsym<\/font><\/a>/g;
  59.         }
  60.  
  61.         print "\t<a href=\"http://multitran.ru/c/m.exe?CL=1&s=".uri_escape($orig_word)."&l1=$lang_id\" target=\"_blank\"><font color=forestgreen>MT</font></a> $s\n";
  62.     }
  63.  
  64.     print "\n";
  65. }
  66.  
  67. print "</pre></font></body></html>\n";
Add Comment
Please, Sign In to add comment