#!/usr/bin/perl
#
#    Copyright 2004 by Jason Stover.
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License as
#    published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
#    02111-1307  USA
#
#
# Read the corpus and record interarrival times between words of interest.
# Also record the time between arrivals of words of interest.

use strict;
use HTML::TreeBuilder;
sub PrintIt
{
    my $arrayref=shift @_;
    foreach my $x (@$arrayref)
    {
	print $x,"\t";
    }
    print "\n";
}

my $necessary='(necessary)|(necessity)|(unnecessary)|(necessarily)|(unnecessarily)';
my $fruit='(fruit)|(fruits)|(fruity)';
my $door='(door)|(doors)|(doorway)';
my @Words=($fruit,$door,$necessary);
my %Positions=($necessary=>[],
	       $fruit=>[],
	       $door=>[]);
while(@ARGV)
{
    my $filename=shift(@ARGV);
    my $file=HTML::TreeBuilder->new_from_file($filename);
    my @paragraphs=$file->find_by_tag_name('p');
    my $position=0;
    my $sentence=shift(@paragraphs);
    while(@paragraphs)
    {
	my $text=$sentence->as_text();
	my @Text=split ' ',$text;
	my $word=shift(@Text);
	while(@Text)
	{
	    if($word =~ m/$necessary/)
	    {
		my $arrayref=$Positions{$necessary};
		push(@$arrayref,$position);
	    }
	    elsif($word =~ m/$door/)
	    {
		my $arrayref=$Positions{$door};
		push(@$arrayref,$position);
	    }
	    elsif($word =~ m/$fruit/)
	    {
		my $arrayref=$Positions{$fruit};
		push(@$arrayref,$position);
	    }
	    $position++;
	    $word=shift(@Text);
	}
	$sentence=shift(@paragraphs);
    }
    print "necessary\t";
    PrintIt($Positions{$necessary});
    print "fruit\t";
    PrintIt($Positions{$fruit});
    print "door\t";
    PrintIt($Positions{$fruit});
}


