#!/usr/bin/perl

# simple way to find passages in bible from arbitrary sentences

# usage: ./rssense01.pl with us against us either

# inspired by http://www.timesonline.co.uk/article/0,,13509-1811332,00.html
# in which Catholics decided that some parts of the Bible are true and 
# not others

$bibFile = "bbe.txt";

%counts=();

for($a=0;$a<=$#ARGV;$a++)
{
    $targ = $ARGV[$a];
    print "counting $targ\n";

    $cmd1="grep -i $targ $bibFile";
    #print "$cmd1\n";

    open(RES,"$cmd1|");
    while(<RES>)
    {
	@sp = split(/\s+/,$_);
	$book = $sp[0];
	@sp2 = split(/\:/,$sp[1]);
	$chap = $sp2[0];
	$verse = $sp2[1];
	#print "book=$book chapter=$chap verse=$verse\n";
	addToDB();
    }

}


$max=0;
while( ($Key, $Value) = each(%counts) )
{
    $book=$Key;
    #print "book = $book, value = $Value\n";
    #print "______\nbook = $book\n";

    $numChaps=$#$Value;
    #print "numChaps is $numChaps\n";

    for($ch=0;$ch<=$numChaps;$ch++)
    {
	$numVerses=@{$counts{$book}[$ch]};
	#print "numVerses = $numVerses\n";
	for($ve=0;$ve<=$numVerses;$ve++)
	{
	    $cou=$counts{$Key}[$ch][$ve];
	    if($cou>=$max)
	    {
		$max=$cou;
		$tehChap=$ch;
		$tehVerse=$ve;
		print "book:$Key Chap:$ch Verse:$ve Count:$cou\n";
		$satz = "$cou $Key $ch $ve";
		push @winners,$satz;
	    }
	}
    }
}

# examine best
@swinners = sort { $b <=> $a } @winners;
for($i=0;$i<10;$i++)
{
    print "_____________\n";
    #print $swinners[$i] ."\n";

    @sp = split(/\s+/,$swinners[$i]);
    $ma = "$sp[1] $sp[2]\:$sp[3]\ ";
    $cmd2 = "grep \"$ma\" bbe.txt";
    #print $cmd2 ."\n";
    $val = `$cmd2`;
    print $val;
}

sub addToDB
{
    $counts{$book}[$chap][$verse]++;
}
