1 |
#!/usr/bin/perl |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Test::More tests => 7; |
7 |
use blib; |
8 |
use jsFind; |
9 |
use Data::Dumper; |
10 |
|
11 |
BEGIN { use_ok('jsFind'); } |
12 |
|
13 |
my $t = new jsFind B => 200; |
14 |
|
15 |
my $file = shift @ARGV || 't/homer.txt'; |
16 |
|
17 |
ok(-e $file, "reading input file $file"); |
18 |
|
19 |
my $line = 0; |
20 |
my $text = ''; |
21 |
my %words_usage; |
22 |
my $word_count = 0; |
23 |
my $max_words; |
24 |
#$max_words = 100; |
25 |
|
26 |
my $res; |
27 |
|
28 |
my $full_text; |
29 |
|
30 |
ok(open(U, $file), "open $file"); |
31 |
while(<U>) { |
32 |
chomp; |
33 |
$line++; |
34 |
next if (/^\s*$/); |
35 |
|
36 |
$full_text = "$line: "; |
37 |
|
38 |
my %usage; |
39 |
|
40 |
my @words = split(/\s+/,lc($_)); |
41 |
|
42 |
foreach (@words) { |
43 |
$usage{$_}++; |
44 |
} |
45 |
|
46 |
foreach my $word (@words) { |
47 |
|
48 |
next if ($word eq ''); |
49 |
|
50 |
$words_usage{"$word $line"} = $usage{$word}; |
51 |
|
52 |
$res->{$word}->{$line} = $usage{$word}; |
53 |
|
54 |
$t->B_search( |
55 |
Key => $word, |
56 |
Data => { "$line" => { |
57 |
t => "Odyssey line $line", |
58 |
f => $usage{$word}, |
59 |
}, |
60 |
}, |
61 |
Insert => 1, |
62 |
Append => 1, |
63 |
); |
64 |
|
65 |
$word_count++; |
66 |
|
67 |
$full_text .= "$word "; |
68 |
|
69 |
} |
70 |
|
71 |
$full_text = "\n"; |
72 |
|
73 |
last if ($max_words && $word_count >= $max_words); |
74 |
} |
75 |
|
76 |
my $test_data = Dumper($res); |
77 |
$test_data =~ s/=>/:/gs; |
78 |
$test_data =~ s/\$VAR1/var test_data/; |
79 |
ok(open(JS, "> html/test_data.js"), "test_data.js"); |
80 |
print JS $test_data; |
81 |
close(JS); |
82 |
|
83 |
ok($test_data, "test_data saved"); |
84 |
|
85 |
my $sum = 0; |
86 |
ok(open(TD, "> homer_freq.txt"), "homer_freq.txt"); |
87 |
foreach my $w (keys %words_usage) { |
88 |
print TD "$w: $words_usage{$w}\n"; |
89 |
$sum += $words_usage{$w}; |
90 |
} |
91 |
close(TD); |
92 |
diag "homer_freq.txt created"; |
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_usage; |
107 |
|
108 |
cmp_ok($t->to_jsfind(dir=>"./html/homer"), '==', $total_words, " jsfind index with $total_words words"); |
109 |
|
110 |
#print Dumper($t); |