]> git.deb.at Git - deb/packages.git/blob - bin/parse-translations
parse-translations: new script to parse the Translation files
[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 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 foreach my $lang (@DDTP_LANGUAGES) {
47     print "Reading Translations for $lang...";
48     open PKG, "zcat $TOPDIR/archive/*/*/*/i18n/Translation-$lang.gz|";
49     my $count = 0;
50     while (<PKG>) {
51         next if /^\s*$/;
52         my $data = "";
53         my %data = ();
54         chomp;
55         s/\n /\377/g;
56         while (/^(\S+):\s*(.*)\s*$/mg) {
57             my ($key, $value) = ($1, $2);
58             $value =~ s/\377/\n /g;
59             $key =~ tr [A-Z] [a-z];
60             $data{$key} = $value;
61         }
62         # Skip double descriptions
63         next if exists($descriptions{$data{"description-md5"}}{$lang});
64         # some weirdnesses in the files
65         next unless defined $data{"description-".lc($lang)};
66         $descriptions{$data{"description-md5"}}{$lang} = $data{"description-".lc($lang)};
67         $count++;
68     }
69     print "($count)\n";
70 }
71 close PKG;
72
73 print "Writing database (".scalar(keys %descriptions)." unique descriptions)...\n";
74 my %descriptions_db;
75 tie %descriptions_db, "DB_File", "$DBDIR/descriptions_translated.db.new",
76         O_RDWR|O_CREAT, 0666, $DB_BTREE
77         or die "Error creating DB: $!";
78 while (my ($md5, $v) = each(%descriptions)) {
79     my $str = "";
80     while (my ($lang, $desc) = each %$v) {
81         unless ($lang && $desc) {
82             warn "MD5: $md5 LANG: $lang DESC: $desc\n";
83             exit;
84         }
85         $str .= "$lang\001$desc\000";
86     }
87
88     $descriptions_db{$md5} = $str;
89 }
90 untie %descriptions_db;
91
92 rename("$DBDIR/descriptions_translated.db.new",
93        "$DBDIR/descriptions_translated.db");