/usr/lib/irstlm/bin/sort-lm.pl is in irstlm 6.00.05-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 | #! /usr/bin/perl
#*****************************************************************************
# IrstLM: IRST Language Model Toolkit
# Copyright (C) 2010 Marcello Federico, FBK-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
#******************************************************************************
#Sorts n-grams of an ARPA file according to lexicographic order.
#Inverted sorting option is propedeutic to building a binary
#lmtable with compile-lm with n-grams stored in reverted order.
use strict;
use Getopt::Long "GetOptions";
use File::Basename;
my ($help,$ilm,$olm,$inv,$tmpdir)=();
$ilm="/dev/stdin";
$olm="/dev/stdout";
my $tmpdir="$ENV{TMP}";
$help=1 unless
&GetOptions('ilm=s' => \$ilm,
'olm=s' => \$olm,
'tmpdir=s' => \$tmpdir,
'inv' => \$inv,
'h|help' => \$help,);
if ($help || !$ilm || !$olm){
my $cmnd = basename($0);
print "\n$cmnd - sorts n-grams of ARPA file according to lexicographic order\n",
"\nUSAGE:\n",
" $cmnd [options]\n",
"\nDESCRIPTION:\n",
" Sorts n-grams of an ARPA file according to lexicographic order.\n",
" Inverted sorting option is propedeutic to building a binary\n",
" mtable with compile-lm with n-grams stored in reverted order.\n",
"\nOPTIONS:\n",
" -h, --help (optional) print these instructions\n",
" -ilm <fname> input ARPA LM filename (default /dev/stdin)\n",
" -olm <fname> output ARPA LM filename (default /dev/stdout)\n",
" -tmpdir <path> temporary directory for sorting (default is the\n",
" enivronment variable TMP\n",
" -inv inverted n-gram sort for compile-lm\n",
"\n";
exit(1);
}
warn "temporary directory for sorting is $tmpdir\n";
my $order=0;
my $sortcmd="";
$ENV{'LC_ALL'}='C';
open (INP, "< $ilm") || die "cannot open input LM file: $ilm\n";
open (OUT, "> $olm") || die "cannot open output LM file: $olm\n";
warn "reading from standard input\n" if $ilm eq "/dev/stdin";
warn "writing to standard output\n" if $olm eq "/dev/stdout";
$_=<INP>;
#sanity check
die "Error: input cannot be an intermediate iARPA file. First convert it to ARPA format with compile-lm.\n" if
$_=~/^iARPA/;
my $isQuantized=0;
$isQuantized=1 if $_=~/^qARPA/;
while(!/^\\end\\/){
if (($order)=$_=~/^\\(\d+)-grams:/){
print(OUT $_);$_=<INP>;
if ($isQuantized){
print(OUT $_); chop $_;#print centers
my $centers=$_; $_=<INP>;
warn "skip $centers centers\n";
for (my $c=1;$c<=$centers;$c++){
print(OUT $_);$_=<INP>;
}
}
#sort command
#$sortcmd="sort -b"; #does not seem to work properly
$sortcmd="sort --temporary-directory=$tmpdir";
if ($inv){
warn "inverted sorting of $order-grams\n";
for (my $n=$order;$n>0;$n--){
$sortcmd.=" -k ".($n+1).",".($n+1);
}
}else{
warn "direct sorting of $order-grams\n";
for (my $n=1;$n<=$order;$n++){
$sortcmd.=" -k ".($n+1).",".($n+1);
}
}
close(OUT);open (OUT,"|$sortcmd >> $olm");
do{
print(OUT $_);$_=<INP>;
}until (/^\\/ || /^\n/);
close(OUT); open(OUT, ">> $olm");
}
else{
print(OUT $_);$_=<INP>;
}
}
print(OUT $_);
close(INP);
close(OUT);
|