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