1 |
dpavlin |
20 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings; |
5 |
|
|
|
6 |
|
|
use Test::More tests => 5; |
7 |
|
|
use blib; |
8 |
|
|
use jsFind; |
9 |
|
|
use Data::Dumper; |
10 |
|
|
|
11 |
|
|
BEGIN { use_ok('jsFind'); } |
12 |
|
|
|
13 |
|
|
my $t = new jsFind B => 100; |
14 |
|
|
|
15 |
dpavlin |
21 |
ok(-e "t/homer.txt", "Homer: The Odyssey preface"); |
16 |
dpavlin |
20 |
|
17 |
|
|
my $p = 0; |
18 |
|
|
my $text = ''; |
19 |
|
|
my %words; |
20 |
dpavlin |
21 |
my $max = 2; |
21 |
dpavlin |
20 |
|
22 |
|
|
my $res; |
23 |
|
|
|
24 |
dpavlin |
21 |
ok(open(U, "t/homer.txt"), "open homer.txt"); |
25 |
dpavlin |
20 |
while(<U>) { |
26 |
|
|
chomp; |
27 |
|
|
if (/^\s*$/) { |
28 |
|
|
$p++; |
29 |
|
|
|
30 |
|
|
diag "paragraph $p"; |
31 |
|
|
|
32 |
|
|
my %usage; |
33 |
|
|
foreach (split(/\s+/,lc($text))) { |
34 |
|
|
$usage{$_}++; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
foreach my $word (keys %usage) { |
38 |
|
|
|
39 |
|
|
$words{$word} += $usage{$word}; |
40 |
|
|
|
41 |
|
|
$res->{$word}->{$p} = $usage{$word}; |
42 |
|
|
|
43 |
|
|
$t->B_search( |
44 |
|
|
Key => $word, |
45 |
|
|
Data => { "$p" => { |
46 |
|
|
t => "Ulyss paragraph $p", |
47 |
|
|
f => $usage{$word}, |
48 |
|
|
}, |
49 |
|
|
}, |
50 |
|
|
Insert => 1, |
51 |
|
|
Append => 1, |
52 |
|
|
); |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
$text = ''; |
56 |
|
|
|
57 |
|
|
last if ($max && $max == $p); |
58 |
|
|
} else { |
59 |
|
|
$text .= $_; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
my $test_data = Dumper($res); |
65 |
|
|
$test_data =~ s/=>/:/gs; |
66 |
|
|
$test_data =~ s/\$VAR1/var test_data/; |
67 |
|
|
open(JS, "> html/test_data.js") || die "can't open test_data.js: $!"; |
68 |
|
|
print JS $test_data; |
69 |
|
|
close(JS); |
70 |
|
|
|
71 |
|
|
ok($test_data, "test_data saved"); |
72 |
|
|
|
73 |
|
|
#my $sum = 0; |
74 |
|
|
#foreach my $w (keys %words) { |
75 |
|
|
# print STDERR "$w: $words{$w}\n"; |
76 |
|
|
# $sum += $words{$w}; |
77 |
|
|
#} |
78 |
|
|
|
79 |
|
|
my $total_words = scalar keys %words; |
80 |
|
|
|
81 |
dpavlin |
21 |
cmp_ok($t->to_jsfind("./html/homer"), '==', $total_words, " jsfind index with $total_words words"); |