aboutsummaryrefslogtreecommitdiff
path: root/challenge-024/lubos-kolouch/perl5/ch-2.pl
blob: 3aa5183b4145596d943db83098e20378e0efcb64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/perl 
#===============================================================================
#
#         FILE: ch-2.pl
#
#        USAGE: ./ch-2.pl  
#
#  DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-024/
#               
#
#    Create a script to implement full text search functionality using Inverted Index. According to wikipedia:
#
#        In computer science, an inverted index (also referred to as a postings file or inverted file) is a database index storing a mapping from content, such as words or numbers, to its locations in a table, or in a document or a set of documents (named in contrast to a forward index, which maps from documents to content). The purpose of an inverted index is to allow fast full-text searches, at a cost of increased processing when a document is added to the database.
#
#
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lubos Kolouch
# ORGANIZATION: 
#      VERSION: 1.0
#      CREATED: 09/08/2019 12:32:53 PM
#     REVISION: ---
#===============================================================================

use strict;
use warnings;
use Carp;
use Data::Dumper;

my @files = glob "*";

my %words;

for my $file (@files) {
    open my $what, '<', $file or croak "Cannot open $file $!";
    while (my $line = <$what>) {
        for (split /\s+/msx, $line) {
            s/\W//gmsx;
            $words{lc($_)}{$file} = 1;
        }
    }
    close $what;
}

warn Dumper \%words;

# ------ TESTS -----------

use Test::More;

is ($words{'using'}{'ch-2.pl'}, 1);
is ($words{'using'}{'ch-1.pl'}, undef);

done_testing;