#!/usr/local/bin/perl ############################################################################## # Program : candidate.pl # # # # Programmer : Bridget Thomson McInnes # # # # Description : This program checks to see if a given word is found in # # a specified dictionary file. If the word is not found, a # # list of candidate corrections is printed to standard out. # # # # Usage : perl canidate.pl # # # ############################################################################## use Text::English; # Get the possible misspelled word my $word = shift @ARGV; my @chars = split(//, $word); # Initialize my hash tables my %lexicon = (); my %canidates = (); # Set the alphabet my @alphabet = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); # Get the dictionary while(<>) { chomp; $lexicon{$_}++; } # Check to see if the word exists in the dictionary if(exists $lexicon{$word}) { print "Word, $word, exists in the dictionary\n"; exit; } # Get the stem of the word if it exists my @stem = Text::English::stem( $word ); # Check if the stem exists in the dictionary foreach (@stem) { # Check to see if the word exists in the dictionary if(exists $lexicon{$word}) { print "Word, $word, exists in the dictionary\n"; exit; } if(exists $lexicon{$_}) { print "Word, $word, exists in the dictionary\n"; exit; } } # Get the candiates based on a simple insertion, # transposition, substitution or deletion for $x(0..$#chars) { for $y(0..$#alphabet) { # Deletion - del[x,y] @del = @chars; $del[$x] .= $alphabet[$y]; if(exists $lexicon{(join "", @del)}) { $canidates{(join "", @del)}++; } # Substitution - sub[x,y] @subs = @chars; $subs[$x] = $alphabet[$y]; if(exists $lexicon{(join "", @subs)}) { $canidates{(join "", @subs)}++; } } # Insertion - ins[x,y] @ins = @chars; $ins[$x] = ""; if(exists $lexicon{(join "", @ins)}) { $canidates{(join "", @ins)}++; } # Transposition - trans[x,y] @trans = @chars; $first = $trans[$x]; $second = $trans[$x+1]; $trans[$x] = $second; $trans[$x+1] = $first; if(exists $lexicon{(join "", @trans)}) { $canidates{(join "", @trans)}++; } } print "$word\n"; # Print the canidates foreach $canidate (sort keys %canidates) { print "$canidate\n"; }