Sunday, August 3, 2008

Mobile T9 Dictionary Implementation in BASH

Well, a computer is good if it does the job.
So, without taking into account trie structures or complicated search algorithms, here's my T9 implementation in BASH

#!/bin/bash
# This bash script does something outrageous.
# It attempts to reproduce the T9 capability
# of the Mobile Phones !

umask 077
DICTN=/tmp/dict.$$
NUMN=/tmp/numn.$$
trap "exit 1" HUP INT PIPE QUIT TERM
trap "rm -f $DICTN $NUMN" EXIT

tr A-Z a-z /usr/share/dict/words |
tr a-z 22233344455566677778889999 |
tr -cd a-z | awk '{print NR ":" $0}'> $NUMN

awk '{print NR ":" $0}' /usr/share/dict/words > $DICTN

if [ $# -eq 0 ] ; then
echo "Usage : t9.sh "
exit 1;

else
PAT="^$1"
join -t: $NUMN $DICTN | cut -d":" -f 2,3 |
grep "$PAT"| cut -d":" -f2 | sort
fi


It surely does the job !
Any comments ?