#!/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 "<html><head><title>Spellchecker Dictionary Editor</title></head><body>\n";

    print "<h1>Words currently in wordlist</h1>";
    
    #Make sure the list displays correctly
    print "<ul>\n";
    
    open (WORDLIST, $wordlist)|| die "couldn't open the file!";
    
    while ($word = <WORDLIST>)
    {
	print("<li>");
	print($word);
	print("</li>");
    }
    
    close(WORDLIST);
    
    
    print "</ul>\n";
    
    
    print "<form method='GET' action='editdict.cgi' name='addWord'>";
    print "<h3>Add a Word to the wordlist:</h3>";
    print "<input type='text' name='word' value=''>";
    print "<input type='Submit' name='func' value='add'>";
    print "</form>";
    
    
    print "<h1>Replacement Pairs</h1>";
    
    #Make sure the list displays correctly
    print "<ul>\n";
    
    open (REPLACELIST, $replace_file)|| die "couldn't open the file!";
    
    while ($word = <REPLACELIST>)
    {
	print("<li>");
	print($word);
	print("</li>");
    }
    
    close(REPLACELIST);
    
    
    print "</ul>\n";
    
    
    print "<form method='GET' action='editdict.cgi' name='replaceWord'>";
    print "<h3>Add a replacement pair</h3>";
    print "Incorrectly Spelt Word:<br> <input type='text' name='word' value=''><br>";
    print "Correctly Spelt Word:<br> <input type='text' name='replacement' value=''><br>";
    print "<input type='Submit' name='func' value='replace'>";
    print "</form>";
	


    print "<form method='GET' action='editdict.cgi' name='rebuild'>";
    print "<h3>Rebuild the Additional Words dictionary and Replacment Pairs file</h3>";
    print "<input type='Submit' name='func' value='rebuild'>";
   
    print "<h3>Sort the word lists</h3>";
    print "<input type='Submit' name='func' value='sort'>";
    
    print "</form>";

    print "</body></html>";
	    

    }

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<br>\n Result:";
    if(0 == system($cmdstring))
    {
	print system($cmdcp);
	print " - Successfully sorted the wordlist<br>\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<br>\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 = <REPLACELIST>)
   {
	($rword,$replace) = split(",", $line);
	
	#strip whitepace
	$rword = trim($rword);
	$replace = trim($replace);
	
	unless($rword eq "" && $replace eq "")
	{
	    print "Word: |$rword| Replacement: |$replace| <br>";
	    $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 = <WORDLIST>)
   {
	
	#strip whitepace
	$line = trim($line);
	
	unless($line eq "")
	{
#DEBUG	    print "Word: |$line| <br>";
	    $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;
}
