--- trunk/t/10homer.pl 2004/10/06 15:33:33 21 +++ trunk/t/10homer.t 2004/10/30 20:50:39 36 @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 7; use blib; use jsFind; use Data::Dumper; @@ -14,68 +14,95 @@ ok(-e "t/homer.txt", "Homer: The Odyssey preface"); -my $p = 0; +my $line = 0; my $text = ''; -my %words; -my $max = 2; +my %words_usage; +my $word_count = 0; +my $max_words; +#$max_words = 100; my $res; +my $full_text; + ok(open(U, "t/homer.txt"), "open homer.txt"); while() { chomp; - if (/^\s*$/) { - $p++; + $line++; + next if (/^\s*$/); + + $full_text = "$line: "; + + my %usage; + + my @words = split(/\s+/,lc($_)); - diag "paragraph $p"; + foreach (@words) { + $usage{$_}++; + } - my %usage; - foreach (split(/\s+/,lc($text))) { - $usage{$_}++; - } + foreach my $word (@words) { - foreach my $word (keys %usage) { + next if ($word eq ''); - $words{$word} += $usage{$word}; + $words_usage{"$word $line"} = $usage{$word}; - $res->{$word}->{$p} = $usage{$word}; + $res->{$word}->{$line} = $usage{$word}; - $t->B_search( - Key => $word, - Data => { "$p" => { - t => "Ulyss paragraph $p", - f => $usage{$word}, - }, + $t->B_search( + Key => $word, + Data => { "$line" => { + t => "Odyssey line $line", + f => $usage{$word}, }, - Insert => 1, - Append => 1, - ); - } - - $text = ''; - - last if ($max && $max == $p); - } else { - $text .= $_; + }, + Insert => 1, + Append => 1, + ); + + $word_count++; + + $full_text .= "$word "; + } + $full_text = "\n"; + + last if ($max_words && $word_count >= $max_words); } my $test_data = Dumper($res); $test_data =~ s/=>/:/gs; $test_data =~ s/\$VAR1/var test_data/; -open(JS, "> html/test_data.js") || die "can't open test_data.js: $!"; +ok(open(JS, "> html/test_data.js"), "test_data.js"); print JS $test_data; close(JS); ok($test_data, "test_data saved"); -#my $sum = 0; -#foreach my $w (keys %words) { -# print STDERR "$w: $words{$w}\n"; -# $sum += $words{$w}; -#} +my $sum = 0; +ok(open(TD, "> homer_freq.txt"), "homer_freq.txt"); +foreach my $w (keys %words_usage) { + print TD "$w: $words_usage{$w}\n"; + $sum += $words_usage{$w}; +} +close(TD); +diag "homer_freq.txt created"; + +if (open(T,"> homer_text.txt")) { + print T $full_text; + close(T); +} +diag "homer_text.txt created"; + +if (open(T,"> homer_words.txt")) { + print T $t->to_string; + close(T); +} +diag "homer_words.txt created"; + +my $total_words = scalar keys %words_usage; -my $total_words = scalar keys %words; +cmp_ok($t->to_jsfind(dir=>"./html/homer"), '==', $total_words, " jsfind index with $total_words words"); -cmp_ok($t->to_jsfind("./html/homer"), '==', $total_words, " jsfind index with $total_words words"); +#print Dumper($t);