1 |
#!/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 |
ok(-e "t/homer.txt", "Homer: The Odyssey preface"); |
16 |
|
17 |
my $p = 0; |
18 |
my $text = ''; |
19 |
my %words; |
20 |
my $word_count = 0; |
21 |
my $max_words = 100; |
22 |
|
23 |
my $res; |
24 |
|
25 |
my $full_text; |
26 |
|
27 |
ok(open(U, "t/homer.txt"), "open homer.txt"); |
28 |
while(<U>) { |
29 |
chomp; |
30 |
if (/^\s*$/) { |
31 |
$p++; |
32 |
|
33 |
$full_text .= "\n" if ($full_text); |
34 |
$full_text = "paragraph $p\n\n"; |
35 |
|
36 |
diag "paragraph $p"; |
37 |
|
38 |
my %usage; |
39 |
|
40 |
my @words = split(/\s+/,lc($text)); |
41 |
|
42 |
foreach (@words) { |
43 |
$usage{$_}++; |
44 |
} |
45 |
|
46 |
foreach my $word (@words) { |
47 |
|
48 |
$words{$word} += $usage{$word}; |
49 |
|
50 |
$res->{$word}->{$p} = $usage{$word}; |
51 |
|
52 |
$t->B_search( |
53 |
Key => $word, |
54 |
Data => { "$p" => { |
55 |
t => "Ulyss paragraph $p", |
56 |
f => $usage{$word}, |
57 |
}, |
58 |
}, |
59 |
Insert => 1, |
60 |
Append => 1, |
61 |
); |
62 |
|
63 |
$word_count++; |
64 |
|
65 |
$full_text .= "$word "; |
66 |
|
67 |
last if ($max_words && $word_count >= $max_words); |
68 |
} |
69 |
|
70 |
$text = ''; |
71 |
|
72 |
last if ($max_words && $word_count >= $max_words); |
73 |
} else { |
74 |
$text .= $_; |
75 |
} |
76 |
|
77 |
} |
78 |
|
79 |
my $test_data = Dumper($res); |
80 |
$test_data =~ s/=>/:/gs; |
81 |
$test_data =~ s/\$VAR1/var test_data/; |
82 |
open(JS, "> html/test_data.js") || die "can't open test_data.js: $!"; |
83 |
print JS $test_data; |
84 |
close(JS); |
85 |
|
86 |
ok($test_data, "test_data saved"); |
87 |
|
88 |
#my $sum = 0; |
89 |
#foreach my $w (keys %words) { |
90 |
# print STDERR "$w: $words{$w}\n"; |
91 |
# $sum += $words{$w}; |
92 |
#} |
93 |
|
94 |
if (open(T,"> homer_text.txt")) { |
95 |
print T $full_text; |
96 |
close(T); |
97 |
} |
98 |
diag "homer_text.txt created"; |
99 |
|
100 |
if (open(T,"> homer_words.txt")) { |
101 |
print T $t->to_string; |
102 |
close(T); |
103 |
} |
104 |
diag "homer_words.txt created"; |
105 |
|
106 |
my $total_words = scalar keys %words; |
107 |
|
108 |
cmp_ok($t->to_jsfind("./html/homer"), '==', $total_words, " jsfind index with $total_words words"); |