| 1 |
#!/bin/sh |
| 2 |
########################################################################### |
| 3 |
## ## |
| 4 |
## Language Technologies Institute ## |
| 5 |
## Carnegie Mellon University ## |
| 6 |
## Copyright (c) 2004 ## |
| 7 |
## All Rights Reserved. ## |
| 8 |
## ## |
| 9 |
## Permission is hereby granted, free of charge, to use and distribute ## |
| 10 |
## this software and its documentation without restriction, including ## |
| 11 |
## without limitation the rights to use, copy, modify, merge, publish, ## |
| 12 |
## distribute, sublicense, and/or sell copies of this work, and to ## |
| 13 |
## permit persons to whom this work is furnished to do so, subject to ## |
| 14 |
## the following conditions: ## |
| 15 |
## 1. The code must retain the above copyright notice, this list of ## |
| 16 |
## conditions and the following disclaimer. ## |
| 17 |
## 2. Any modifications must be clearly marked as such. ## |
| 18 |
## 3. Original authors' names are not deleted. ## |
| 19 |
## 4. The authors' names are not used to endorse or promote products ## |
| 20 |
## derived from this software without specific prior written ## |
| 21 |
## permission. ## |
| 22 |
## ## |
| 23 |
## CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK ## |
| 24 |
## DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ## |
| 25 |
## ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT ## |
| 26 |
## SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE ## |
| 27 |
## FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ## |
| 28 |
## WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN ## |
| 29 |
## AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ## |
| 30 |
## ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF ## |
| 31 |
## THIS SOFTWARE. ## |
| 32 |
## ## |
| 33 |
########################################################################### |
| 34 |
## Author: Alan W Black ([email protected]) ## |
| 35 |
## Date: December 2004 ## |
| 36 |
########################################################################### |
| 37 |
## ## |
| 38 |
## Make a Huffman table data ## |
| 39 |
## done by finding the top singletons, bigrams, trigrams ... and byte ## |
| 40 |
## coding them ## |
| 41 |
## ## |
| 42 |
## But this isn't full huffman coding yet ## |
| 43 |
## ## |
| 44 |
## Hmm, this should probably be written in something other than shell ## |
| 45 |
########################################################################### |
| 46 |
|
| 47 |
## There are N stages |
| 48 |
## Find the alphabet (usually 256 things) |
| 49 |
## This is done by finding the top singletons, bigrams, trigrams ... |
| 50 |
## Find the frequencies (currently ignored, as codes are always 1 byte) |
| 51 |
## Compress the data, and output the lookup tables |
| 52 |
|
| 53 |
## There is some octal conversion in here that probably is unnecessary |
| 54 |
|
| 55 |
# Tidy up the tmp files if we get interrupted |
| 56 |
trap 'rm -f huff.tmp.corpus.[0-9]* ; exit' 0 2 3 5 10 13 15 |
| 57 |
|
| 58 |
# Newer versions of Linux have a not very useful value for LANG by default |
| 59 |
LANG=C |
| 60 |
export LANG |
| 61 |
|
| 62 |
## 0 is reserved, 1 is reserved too |
| 63 |
ALPHABET_SIZE=254 |
| 64 |
|
| 65 |
# if [ $1 = "xentries" ] |
| 66 |
# then |
| 67 |
# infile=$2 |
| 68 |
# outfile=$3 |
| 69 |
# # Normalize the string to be compressed |
| 70 |
# cat $infile | awk '{print $1}' | |
| 71 |
# tr "A-Z" "a-z" | |
| 72 |
# awk '{for (i=1; i<=length($1); i++) |
| 73 |
# printf("%s ",substr($1,i,1)); |
| 74 |
# printf("\n");}' >huff.tmp.corpus |
| 75 |
# fi |
| 76 |
|
| 77 |
if [ $1 = "entries" ] |
| 78 |
then |
| 79 |
# actually works for non-utf8 entries too |
| 80 |
infile=$2 |
| 81 |
outfile=$3 |
| 82 |
# Normalize the string to be compressed |
| 83 |
cat $infile | awk '{printf("\"%s\"\n",$1)}' | |
| 84 |
cat >huff.entries.corpus |
| 85 |
$ESTDIR/../festival/bin/festival -b $FLITEDIR/tools/make_lex.scm '(utf8entries "huff.entries.corpus" "huff.tmp.corpus")' |
| 86 |
fi |
| 87 |
|
| 88 |
if [ $1 = "phones" ] |
| 89 |
then |
| 90 |
infile=$2 |
| 91 |
outfile=$3 |
| 92 |
# Normalize the text to be compressed |
| 93 |
cat $infile | awk '{print $NF}' | |
| 94 |
sed 's/\\/ /g' >huff.tmp.corpus |
| 95 |
fi |
| 96 |
|
| 97 |
if [ $1 = "residual" ] |
| 98 |
then |
| 99 |
# This really doesn't work: just some tests to see what's worthwhile |
| 100 |
infile=$2 |
| 101 |
outfile=$3 |
| 102 |
# Normalized the text to be compressed |
| 103 |
cat $infile | |
| 104 |
awk '{if ($5 ~ /.*_res_/) |
| 105 |
n=1; |
| 106 |
else if (n==1) |
| 107 |
{ |
| 108 |
printf("%s ",$i); |
| 109 |
b=$i; |
| 110 |
for (i=2; i<=NF; i++) |
| 111 |
{ |
| 112 |
q=b-$i; |
| 113 |
if (q < 0) |
| 114 |
q+=256; |
| 115 |
printf("%s ",q); |
| 116 |
b=$i |
| 117 |
} |
| 118 |
printf("\n"); |
| 119 |
n=0 |
| 120 |
}}' | |
| 121 |
sed 's/,//g;s/};//' >huff.tmp.corpus |
| 122 |
fi |
| 123 |
|
| 124 |
if [ $1 = "other" ] |
| 125 |
then |
| 126 |
infile=$2 |
| 127 |
outfile=$3 |
| 128 |
cat $infile >huff.tmp.corpus |
| 129 |
fi |
| 130 |
|
| 131 |
CORPUS_SIZE=`cat huff.tmp.corpus | wc -w` |
| 132 |
echo "Original Corpus size = $CORPUS_SIZE" |
| 133 |
|
| 134 |
# Find the base alphabet |
| 135 |
cat huff.tmp.corpus | |
| 136 |
awk '{for (i=1; i<=NF; i++) |
| 137 |
printf("%s\n",$i);}' | |
| 138 |
sort -u >huff.tmp.base.alphabet |
| 139 |
|
| 140 |
BASE_ALPHABET_SIZE=`cat huff.tmp.base.alphabet | wc -l` |
| 141 |
echo "Base alphabet = $BASE_ALPHABET_SIZE" |
| 142 |
|
| 143 |
# Find the additional best n grams |
| 144 |
PASS=000 |
| 145 |
cp -p huff.tmp.corpus huff.tmp.corpus.$PASS |
| 146 |
|
| 147 |
PASS_ALPHABET_SIZE=$BASE_ALPHABET_SIZE |
| 148 |
while [ $PASS_ALPHABET_SIZE != $ALPHABET_SIZE ] |
| 149 |
do |
| 150 |
# Loop until we get to the desired alphabet size |
| 151 |
|
| 152 |
cat huff.tmp.corpus.$PASS | |
| 153 |
awk '{for (i=1; i<NF; i++) |
| 154 |
{ |
| 155 |
bb = sprintf("%s_%s",$i,$(i+1)); |
| 156 |
freq[bb]++; |
| 157 |
part1[bb] = $i; |
| 158 |
part2[bb] = $(i+1); |
| 159 |
}} |
| 160 |
END {best=""; |
| 161 |
bestscore=0; |
| 162 |
for (i in freq) |
| 163 |
{ |
| 164 |
score = freq[i]; |
| 165 |
if (score > bestscore) |
| 166 |
{ |
| 167 |
bestscore = score; |
| 168 |
best = i; |
| 169 |
} |
| 170 |
} |
| 171 |
printf("%s %s %d %d\n",part1[best],part2[best],bestscore,freq[best]) |
| 172 |
}' >huff.tmp.corpus.$PASS.best |
| 173 |
|
| 174 |
# Substitute the best bigram to give next corpus |
| 175 |
NPASS=`echo $PASS | awk '{printf("%03d",$1+1)}'` |
| 176 |
cat huff.tmp.corpus.$PASS | |
| 177 |
awk 'BEGIN {p1="'`cat huff.tmp.corpus.$PASS.best | awk '{print $1}'`'"; |
| 178 |
p2="'`cat huff.tmp.corpus.$PASS.best | awk '{print $2}'`'"; |
| 179 |
} |
| 180 |
{for (i=1; i<=NF; i++) |
| 181 |
{ |
| 182 |
if (($i == p1) && ($(i+1) == p2)) |
| 183 |
{ |
| 184 |
printf("%s+%s ",$i,$(i+1)); |
| 185 |
i++; |
| 186 |
} |
| 187 |
else |
| 188 |
printf("%s ",$i); |
| 189 |
} |
| 190 |
printf("\n");}' >huff.tmp.corpus.$NPASS |
| 191 |
|
| 192 |
# Summary |
| 193 |
echo $PASS joining `cat huff.tmp.corpus.$PASS.best` |
| 194 |
echo " PAS $PASS_ALPHABET_SIZE AS $ALPHABET_SIZE" |
| 195 |
echo " Corpus was "`cat huff.tmp.corpus.$PASS | wc -w` |
| 196 |
echo " Corpus is "`cat huff.tmp.corpus.$NPASS | wc -w` |
| 197 |
CORPUS_IS=`cat huff.tmp.corpus.$NPASS | wc -w` |
| 198 |
echo $CORPUS_SIZE $CORPUS_IS | |
| 199 |
awk '{printf(" Compression %0.2f\n",100*($2*1.0)/$1)}' |
| 200 |
|
| 201 |
PASS=$NPASS |
| 202 |
PASS_ALPHABET_SIZE=`cat huff.tmp.corpus.$NPASS | |
| 203 |
awk '{for (i=1; i<=NF; i++) |
| 204 |
printf("%s\n",$i);}' | sort -u | wc -l` |
| 205 |
done |
| 206 |
|
| 207 |
# Now build the map |
| 208 |
mv huff.tmp.corpus.$NPASS huff.tmp.corpus.best |
| 209 |
rm -f huff.tmp.corpus.[0-9]* |
| 210 |
|
| 211 |
cat huff.tmp.corpus.best | |
| 212 |
awk 'BEGIN {tot=0;} |
| 213 |
{ for (i=1; i<=NF; i++) |
| 214 |
{ |
| 215 |
freq[$i]++ |
| 216 |
tot++; |
| 217 |
} |
| 218 |
} |
| 219 |
END { for (i in freq) |
| 220 |
printf("%f %s\n",freq[i]/tot,i);}' | |
| 221 |
sort -g | |
| 222 |
awk '{printf("map[\"%s\"] = %d; unmap[%d] = \"%s\";\n",$2,NR,NR,$2)}' >huff.tmp.corpus.maptable |
| 223 |
|
| 224 |
maptable=`cat huff.tmp.corpus.maptable` |
| 225 |
|
| 226 |
# compress the corpus |
| 227 |
cat huff.tmp.corpus.best | |
| 228 |
awk 'BEGIN {'"$maptable"'} |
| 229 |
function enoctal(x) |
| 230 |
{ |
| 231 |
units = x % 8; |
| 232 |
eights = ((x - units) / 8) % 8; |
| 233 |
sixtyfours = ((((x - units) / 8) - eights) / 8) % 8; |
| 234 |
return sprintf("%d%d%d", sixtyfours, eights, units); |
| 235 |
} |
| 236 |
{ |
| 237 |
for (i=1; i<=NF; i++) |
| 238 |
printf("\\%s",enoctal(map[$i])); |
| 239 |
printf("\n"); |
| 240 |
}' > huff.tmp.corpus.compressed |
| 241 |
|
| 242 |
# uncompress the corpus |
| 243 |
cat huff.tmp.corpus.compressed | |
| 244 |
sed 's/\\/ /g' | |
| 245 |
awk 'BEGIN {'"$maptable"'} |
| 246 |
function unenoctal(x) |
| 247 |
{ |
| 248 |
y = ((substr(x,1,1)+0)*64) + ((substr(x,2,1)+0)*8) + (substr(x,3,1)+0); |
| 249 |
return y; |
| 250 |
} |
| 251 |
{ |
| 252 |
for (i=1; i<=NF; i++) |
| 253 |
printf("%s ",unmap[unenoctal($i)]); |
| 254 |
printf("\n"); |
| 255 |
}' > huff.tmp.corpus.uncompressed |
| 256 |
|
| 257 |
if [ $1 = "phones" ] |
| 258 |
then |
| 259 |
cat huff.tmp.corpus.best | |
| 260 |
sed 's/+/\\/g' | |
| 261 |
awk '{ for (i=1; i<=NF; i++) |
| 262 |
{ |
| 263 |
freq[$i]++ |
| 264 |
tot++; |
| 265 |
} |
| 266 |
} |
| 267 |
END { for (i in freq) |
| 268 |
printf("%f %s\n",freq[i]/tot,i);}' | |
| 269 |
sort -g | |
| 270 |
awk '{printf(" \"\\%s\" , /* %f */ \n",$2,$1);}' >$outfile |
| 271 |
mv huff.tmp.corpus.compressed huff.phones.compressed |
| 272 |
fi |
| 273 |
|
| 274 |
if [ $1 = "entries" ] |
| 275 |
then |
| 276 |
cat huff.tmp.corpus.best | |
| 277 |
sed 's/+//g' | |
| 278 |
awk '{ for (i=1; i<=NF; i++) |
| 279 |
{ |
| 280 |
freq[$i]++ |
| 281 |
tot++; |
| 282 |
} |
| 283 |
} |
| 284 |
END { for (i in freq) |
| 285 |
printf("%f %s\n",freq[i]/tot,i);}' | |
| 286 |
sort -g | |
| 287 |
awk '{printf(" \"%s\" , /* %f */ \n",$2,$1);}' >$outfile |
| 288 |
mv huff.tmp.corpus.compressed huff.entries.compressed |
| 289 |
fi |
| 290 |
|
| 291 |
exit |
| 292 |
|
| 293 |
|
| 294 |
|