#!/usr/bin/perl

$numbers = "/home/pi/bubu/bubunumbers.csv";
$corrections = "/home/pi/bubu/bubucorrections.csv";
$debug = 0;
$debnr = 0;

@words = @ARGV;
println(++$debnr .") ". join(" ", @words)) if($debug);
@numbers = readFileIntoArray($numbers);
@corrections = readFileIntoArray($corrections);

# word corrections of four words each
for($w=0;$w<$#words+1;$w++){
	$word1 = $words[$w+0];
	$word2 = $words[$w+1];
	$word3 = $words[$w+2];
	$word4 = $words[$w+3];
	foreach $line(@corrections){
		if($line=~/^$word1 $word2 $word3 $word4;(.+)$/i){
			$word1 = $1;
			$w += 3;
		}
	}
	push(@newwords, $word1);
}	
@words = @newwords; @newwords = ();
$wordstr = join(" ", @words);
@words = split(/\s+/, $wordstr);
println(++$debnr .") ". join(" ", @words)) if($debug);

# word corrections of three words each
for($w=0;$w<$#words+1;$w++){
	$word1 = $words[$w+0];
	$word2 = $words[$w+1];
	$word3 = $words[$w+2];
	foreach $line(@corrections){
		if($line=~/^$word1 $word2 $word3;(.+)$/i){
			$word1 = $1;
			$w += 2;
		}
	}
	push(@newwords, $word1);
}
@words = @newwords; @newwords = ();
$wordstr = join(" ", @words);
@words = split(/\s+/, $wordstr);
println(++$debnr .") ". join(" ", @words)) if($debug);

# word corrections of two words each
for($w=0;$w<$#words+1;$w++){
	$word1 = $words[$w+0];
	$word2 = $words[$w+1];
	foreach $line(@corrections){
		if($line=~/^$word1 $word2;(.+)$/i){
			$word1 = $1;
			$w += 1;
		}
	}
	push(@newwords, $word1);
}	
@words = @newwords; @newwords = ();
$wordstr = join(" ", @words);
@words = split(/\s+/, $wordstr);
println(++$debnr .") ". join(" ", @words)) if($debug);

# simple word corrections
@newwords = ();
for($w=0;$w<$#words+1;$w++){
	$word1 = $words[$w+0];
	foreach $line(@corrections){
		$word1 = $1 if($line=~/^$word1;(.+)$/i);
	}
	push(@newwords, $word1);
}
@words = @newwords; @newwords = ();
$wordstr = join(" ", @words);
@words = split(/\s+/, $wordstr);
println(++$debnr .") ". join(" ", @words)) if($debug);


# simple number corrections
for($w=0;$w<$#words+1;$w++){
	$word1 = $words[$w+0];
	foreach $line(@numbers){
		$word1 = $1 if($line=~/^$word1;(.+)$/i);
	}
	push(@newwords, $word1);
}
@words = @newwords; @newwords = ();
$wordstr = join(" ", @words);
@words = split(/\s+/, $wordstr);
println(++$debnr .") ". join(" ", @words)) if($debug);

# correct numbers with a space in between
for($w=0;$w<$#words+1;$w++){
	$word1 = $words[$w+0];
	$word2 = $words[$w+1];
	foreach $line(@numbers){
		if($line=~/^$word1$word2;(.+)$/i){
			$word1 = $1;
			$w += 1;
		}
	}
	push(@newwords, $word1);
}
@words = @newwords; @newwords = ();
$wordstr = join(" ", @words);
@words = split(/\s+/, $wordstr);
println(++$debnr .") ". join(" ", @words)) if($debug);

# simple number corrections
for($w=0;$w<$#words+1;$w++){
	$word1 = $words[$w+0];
	foreach $line(@numbers){
		$word1 = $1 if($line=~/^$word1;(.+)$/i);

		$w += 0;
	}
	push(@newwords, $word1);
}
@words = @newwords; @newwords = ();
$wordstr = join(" ", @words);
@words = split(/\s+/, $wordstr);
println(++$debnr .") ". join(" ", @words)) if($debug);


# output
print join(" ", @words);




sub readFileIntoArray{
	my $file = shift;
	my @lines = ();
	open(F, "<$file") or die("ERROR: cannot read file $file:$!");
	while(<F>){
		my $line = $_;
		chomp($line);
		push(@lines, $line);
	}
	close(F);
	return @lines;
}
sub println{
	my $msg = shift;
	print $msg ."\n";
}
