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