Skip to content

Commit

Permalink
Fuzzy search
Browse files Browse the repository at this point in the history
  • Loading branch information
FCO committed Jun 10, 2018
1 parent 4b525e8 commit fd22f0b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/Trie.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,21 @@ multi method get-node(Str $string) {
self.get-node: $string.comb
}

method get-single(\key) { self.get-node(key).single }
method get-all(\key) { self.get-node(key).all }
method get-single(\key) { self.get-node(key).single }
method get-all(\key) { self.get-node(key).all }

method find-char(\char) { gather { self!find-char(char) } }
method !find-char(\char) { .take with %!children{char}; %!children.pairs.sort(*.key)>>.value>>!find-char(char) }

multi method find-substring($string) { self.find-substring: $string.comb }
multi method find-substring([$first, *@rest]) {
self.find-char($first)>>.get-all(@rest).flat
}

multi method find-fuzzy($string) { self.find-fuzzy: $string.comb }
multi method find-fuzzy([$first]) {
self.find-char($first)>>.all.flat
}
multi method find-fuzzy([$first, *@rest]) {
self.find-char($first)>>.find-fuzzy(@rest).flat
}
12 changes: 12 additions & 0 deletions t/02-trie.t
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,16 @@ is $t1<bla>:v, $t1.get-all: "bla";
is $t1<bla>:kv, ("bla", $t1.get-all: "bla");
is $t1<bla>:p, "bla" => $t1.get-all: "bla";

my $babaca = $t1.insert: "babaca";
my $cacau = $t1.insert: "cacau";
my $abacaxi = $t1.insert: "abacaxi";
my $abacate = $t1.insert: "ababacate";
my $abocanhar = $t1.insert: "abocanhar";
my $babao = $t1.insert: "babao";

is $t1.find-char("a").elems, 13;
is $t1.find-substring("bac"), <abacaxi ababacate babaca>;

is $t1.find-fuzzy("bct"), <ababacate ababacate>;

done-testing

0 comments on commit fd22f0b

Please sign in to comment.