#!/usr/local/bin/perl -T
# ---------------------------------------------------------------------------------
# Author: James Brunskill (Library/University of Waikato) - brunskil@waikato.ac.nz
# Original Release Date: 05/03/2007
# Description/Purpose:
# Uses perl and the Unix commandline to edit a list of words, and compile an aspell dictionary
#
#
# ---------------------------------------------------------------------------------
use strict;
use CGI;
use Text::Aspell;
#edit these variables to point to the files you want to edit
my $wordlist = "/m1/voyager/shared/aspell/additionalwords.txt";
my $replace_file = "/m1/voyager/shared/aspell/replacements.txt";
my $scripturl = "http://conzapp01-back.lconz.ac.nz:7201/cgi-bin/editdict.cgi";
my $q=new CGI;
# grab all the params we're expecting
my $mode = $q->param('func');
unless ($mode =~ /add|rebuild|replace|sort/) { $mode = 'view'; }
my $newword = $q->param('word');
if ($mode eq 'view') {
#We now need to open the wordlist file and display the contents
print $q->header();
my $word = "";
print "
Spellchecker Dictionary Editor\n";
print "Words currently in wordlist
";
#Make sure the list displays correctly
print "\n";
open (WORDLIST, $wordlist)|| die "couldn't open the file!";
while ($word = )
{
print("- ");
print($word);
print("
");
}
close(WORDLIST);
print "
\n";
print "";
print "Replacement Pairs
";
#Make sure the list displays correctly
print "\n";
open (REPLACELIST, $replace_file)|| die "couldn't open the file!";
while ($word = )
{
print("- ");
print($word);
print("
");
}
close(REPLACELIST);
print "
\n";
print "";
print "";
print "";
}
if ($mode eq 'add') {
#For debugging output only
#print $q->header();
#append to the file
open (WORDLIST, ">>$wordlist") || die "couldn't open the file!";
unless($newword eq "")
{
print WORDLIST "\n$newword";
print "Added word $newword";
}
close(WORDLIST);
print $q->redirect($scripturl);
}
if ($mode eq 'sort')
{
print $q->header();
#sort the wordlist file
my $cmdstring = "/usr/bin/sort -ubd $wordlist > $wordlist.bck";
my $cmdcp = "/usr/bin/cp $wordlist.bck $wordlist";
$ENV{"PATH"} = "";
print "Running Command: " . $cmdstring . "\n
\n Result:";
if(0 == system($cmdstring))
{
print system($cmdcp);
print " - Successfully sorted the wordlist
\n";
}
#sort the replacements file
$cmdstring = "/usr/bin/sort -ubd $replace_file > $replace_file.bck";
$cmdcp = "/usr/bin/cp $replace_file.bck $replace_file";
print "Running Command: " . $cmdstring . "\n
\n Result:";
print system($cmdstring);
if(0 == system($cmdstring))
{
print system($cmdcp);
print " - Successfully sorted the replacements list\n";
}
}
if ($mode eq 'rebuild')
{
print $q->header();
rebuildDictionaries();
# print $q->redirect($scripturl);
}
if ($mode eq 'replace')
{
# print $q->header();
my $replacement = $q->param('replacement');
#append to the file
open (REPLACELIST, ">>$replace_file") || die "couldn't open the file!";
unless($newword eq "" || $replacement eq "")
{
print REPLACELIST "$newword, $replacement\n";
print "Added word $newword -> $replacement";
}
close(REPLACELIST);
print $q->redirect($scripturl);
}
sub rebuildDictionaries()
{
#1. Create and setup the Aspell object (used to store replacements and personal dictionary words)
my $speller = Text::Aspell->new;
die "" unless $speller;
my $lang = 'en_ALL';
#set options for aspell.
$speller->set_option('home-dir', '/m1/voyager/shared/aspell/');
$speller->set_option('lang',$lang); # default in our local install at UOW
$speller->set_option('sug-mode','fast');
$speller->set_option('extra-dicts','maoriwords.local');
$speller->set_option('ignore-case','true');
#2. Load in replacements.txt
my $line = "";
my $rword = "";
my $replace = "";
open (REPLACELIST, $replace_file) || die "couldn't open replacements file!";
while ($line = )
{
($rword,$replace) = split(",", $line);
#strip whitepace
$rword = trim($rword);
$replace = trim($replace);
unless($rword eq "" && $replace eq "")
{
print "Word: |$rword| Replacement: |$replace|
";
$speller->store_replacement("$rword", "$replace");
}
}
close(REPLACELIST);
#3. Add additional words to 'personal dictionary'
open (WORDLIST, $wordlist) || die "couldn't open replacements file!";
while ($line = )
{
#strip whitepace
$line = trim($line);
unless($line eq "")
{
#DEBUG print "Word: |$line|
";
$speller->add_to_personal("$line");
}
}
close(REPLACELIST);
#4. Save changes to the word lists....
$speller->save_all_word_lists;
#DEBUG print "Created additional words dictionary and replacements file";
}
sub trim {
my $string = shift;
for ($string) {
s/^\s+//;
s/\s+$//;
}
return $string;
}