]> git.deb.at Git - deb/packages.git/blob - bin/parse-translations
parse-translations: Fix encoding of Japanese descriptions
[deb/packages.git] / bin / parse-translations
1 #!/usr/bin/perl -w
2 # Convert Translation.gz files into Sleepycat db files for efficient usage of
3 # data
4 #
5 # $Id$
6 #
7 # Copyright (C) 2006  Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22 use strict;
23 use warnings;
24 use lib './lib';
25
26 $| = 1;
27
28 # max. distinct results for a given package postfix
29 my $MAX_PACKAGE_POSTFIXES = 100;
30
31 use DB_File;
32 use Storable;
33 use File::Path;
34 use Digest::MD5;
35 use Text::Iconv;
36 use Deb::Versions;
37 use Lingua::Stem v0.82;
38 use Search::Xapian;
39 use Packages::Config qw( $TOPDIR $DBDIR @DDTP_LANGUAGES );
40 &Packages::Config::init( './' );
41 my %descriptions = ();
42
43 $/ = "";
44
45 -d $DBDIR || mkpath( $DBDIR );
46
47 my $fixja = Text::Iconv->new("EUC-JP", "UTF-8");
48
49 foreach my $lang (@DDTP_LANGUAGES) {
50     print "Reading Translations for $lang...";
51     open PKG, "zcat $TOPDIR/archive/*/*/*/i18n/Translation-$lang.gz|";
52     my $count = 0;
53     while (<PKG>) {
54         next if /^\s*$/;
55         my $data = "";
56         my %data = ();
57         chomp;
58         s/\n /\377/g;
59         while (/^(\S+):\s*(.*)\s*$/mg) {
60             my ($key, $value) = ($1, $2);
61             $value =~ s/\377/\n /g;
62             $key =~ tr [A-Z] [a-z];
63             $data{$key} = $value;
64         }
65         # Skip double descriptions
66         next if exists($descriptions{$data{"description-md5"}}{$lang});
67         # some weirdnesses in the files
68         next unless defined $data{"description-".lc($lang)};
69         if ($lang eq 'ja') {
70             my $fixed = $fixja->convert($data{"description-ja"});
71             $data{"description-ja"} = $fixed if $fixed;
72         }
73         $descriptions{$data{"description-md5"}}{$lang} = $data{"description-".lc($lang)};
74         $count++;
75     }
76     print "($count)\n";
77 }
78 close PKG;
79
80 print "Writing database (".scalar(keys %descriptions)." unique descriptions)...\n";
81 my %descriptions_db;
82 tie %descriptions_db, "DB_File", "$DBDIR/descriptions_translated.db.new",
83         O_RDWR|O_CREAT, 0666, $DB_BTREE
84         or die "Error creating DB: $!";
85 while (my ($md5, $v) = each(%descriptions)) {
86     my $str = "";
87     while (my ($lang, $desc) = each %$v) {
88         unless ($lang && $desc) {
89             warn "MD5: $md5 LANG: $lang DESC: $desc\n";
90             exit;
91         }
92         $str .= "$lang\001$desc\000";
93     }
94
95     $descriptions_db{$md5} = $str;
96 }
97 untie %descriptions_db;
98
99 rename("$DBDIR/descriptions_translated.db.new",
100        "$DBDIR/descriptions_translated.db");