File: split-dict.pl

package info (click to toggle)
irstlm 6.00.05-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,600 kB
  • sloc: cpp: 15,933; ansic: 1,590; sh: 1,293; perl: 831; makefile: 118
file content (157 lines) | stat: -rwxr-xr-x 4,296 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#! /usr/bin/perl

#*****************************************************************************
# IrstLM: IRST Language Model Toolkit
# Copyright (C) 2007 Marcello Federico, ITC-irst Trento, Italy

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

#******************************************************************************

#usage:
#split-dict.pl <input> <output> <parts>
#It splits the <input> dictionary into <parts> dictionaries
#(named <output000>, ... <output999>)
#splitting is balanced wrt to frequency of the <input> dictionary
#if not available a frequency of 1 is considered

use strict;
use Getopt::Long "GetOptions";
use File::Basename;

my ($help,$input,$output,$parts)=();

$help=1 unless
&GetOptions('input=s' => \$input,
            'output=s' => \$output, 
             'parts=i' => \$parts,           
             'h|help' => \$help,);

if ($help || !$input || !$output || !$parts) {
	my $cmnd = basename($0);
  print "\n$cmnd - splits a dictionary into frequency-balanced partitions\n",
	"\nUSAGE:\n",
	"       $cmnd [options]\n",
	"\nDESCRIPTION:\n",
	"       $cmnd splits a dictionary into frequency-balanced partitions.\n",
	"       The dictionary must be generated with IRSTLM command dict.\n",
	"       If dictionary does not contain frequencies, then a frequency 1 is\n",
	"       assumed for all words.\n",
	"\nOPTIONS:\n",
    "       --input <string>      input dictionary with frequencies\n",
    "       --output <string>     prefix of output dictionaries\n",
    "       --parts <int>         number of partitions to create\n",
    "       -h, --help            (optional) print these instructions\n",
    "\n";

  exit(1);
}



my $freqflag=0;
my ($w,$f,$globf,$thr);
my (@D,@F,%S,@C);
open(IN,"$input");

chomp($_=<IN>);
#if input is a dictionary.
if (/^dictionary[ \t]+\d+[ \t]+\d+$/i){
  my ($dummy,$size);
  ($dummy,$dummy,$size)=split(/[ \t]+/,$_);
  $freqflag=1 if /DICTIONARY/;
}

$globf=0;
while(chomp($_=<IN>)){
	if ($freqflag){
		($w,$f)=split(/[ \t]+/,$_);
	}
	else{
		$w=$_;
		$f=1;
	}
	push @D, $w;
	push @F, $f;
  $globf+=$f;
}
close (IN);

$thr=$globf/$parts;
my $totf=0;
print STDERR "Dictionary 0: (thr: $thr , $globf, $totf , $parts)\n";

my $sfx=0;
my $w;
for (my $i=0;$i<=$#D;$i++){
	
# if the remaining words are less than or equal to 
# the number of remaining sub-dictionaries to create
# put only one word per each sub-dictionary.
	if (($totf>0) && ($#D+1-$i) <= ($parts-1-$sfx)){
# recompute threshold on the remaining global frequency
# according to the number of remaining parts
		$sfx++;
		$globf-=$totf;
		$thr=($globf)/($parts-$sfx);
		print STDERR "Dictionary $sfx: (thr: $thr , $globf , $totf , ",($parts-$sfx),")\n";
		$totf=0;
	}

	$totf+=$F[$i];
	$w=$D[$i];
	$S{$w}=$sfx;
	$C[$sfx]++;
	if ($totf>$thr){
# recompute threshold on the remaining global frequency
# according to the number of remaining parts
		$sfx++;
		$globf-=$totf;
		$thr=($globf)/($parts-$sfx);
		print STDERR "Dictionary $sfx: (thr: $thr , $globf , $totf , ",($parts-$sfx),")\n";
		$totf=0;
	}
}


my $oldsfx=-1;
for (my $i=0;$i<=$#D;$i++){
	$w=$D[$i];
	$sfx="0000$S{$w}";
	$sfx=~s/.+(\d{3})/$1/;
	if ($sfx != $oldsfx){
#print STDERR "opening $output$sfx\n";
		close (OUT) if $oldsfx!= -1;
		open(OUT,">$output$sfx");
		if ($freqflag){
			print OUT "DICTIONARY 0 $C[$sfx]\n";
		}
		else{
			print OUT "dictionary 0 $C[$sfx]\n";
		}
		$oldsfx=$sfx;
	}
	if ($freqflag){
		print OUT "$w $F[$i]\n";
	}
	else{
		print OUT "$w\n";
	}
}
close (OUT) if $oldsfx!= -1;

my $numdict=$S{$D[$#D]}+1;
die "Only $numdict dictionaries were crested instead of $parts!" if ($numdict != $parts);