| 1 | # Copyright 2018 Francesco Nidito. All rights reserved. | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # This library is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 4 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Text::Shingle; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Unicode::Normalize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Text::NGrammer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 18 | 4 | my %config = ( w => 2, # shingles length | |||||||||||||||||||||||||||
| 19 | norm => 1, # by default, enable the normalization | ||||||||||||||||||||||||||||
| 20 | lang => 'en', # used by the sentencer | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | my %param = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | for my $opt (keys %param) { | |||||||||||||||||||||||||||
| 26 | 2 | ▶ | croak "option $opt unsupported by version $VERSION of Text::Shingle" unless exists $config{$opt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | croak "window size cannot be negative or zero" if $opt eq 'w' && $param{w} < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opt eq "w" and $param{"w"} < 1
MC/DC0%0/2
$opt eq "w" and $param{"w"} < 1
$opt eq "w"
$param{"w"} < 1
Truth table33%1/3
A and B
A= $opt eq "w"B= $param{"w"} < 1
| |||||||||||||||||||||||||||||
| 28 | 2 | $config{$opt} = $param{$opt}; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 4 | $config{version} = $VERSION; | |||||||||||||||||||||||||||
| 31 | 4 | return bless \%config, $class; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 3 | ▶ | sub _shingles_from_ngrams { | ||||||||||||||||||||||||||
|
Subroutine
_shingles_from_ngrams: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 35 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 36 | 3 | my @ngrams = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | my %shingles = (); | |||||||||||||||||||||||||||
| 39 | 3 | for my $ngram (@ngrams) { | |||||||||||||||||||||||||||
| 40 | 16 | my $shingle = join(' ', sort {$a cmp $b} | |||||||||||||||||||||||||||
| 41 | 25 | ▶ | map { ($self->{norm}) ? NFKC($_) : $_ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 11 | @{$ngram} | |||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | 11 | ▶ | $shingles{$shingle} = 1 if (!exists $shingles{$shingle}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 3 | return keys %shingles; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub shingle_array { | ||||||||||||||||||||||||||
|
Subroutine
shingle_array: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 0 | my $n = scalar(@_); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return () if $n < $self->{w}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | my $ngrammer = Text::NGrammer->new(lang => $self->{lang}); | |||||||||||||||||||||||||||
| 56 | 0 | return $self->_shingles_from_ngrams($ngrammer->ngrams_array($self->{w}, @_)); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub shingle_sentence { | ||||||||||||||||||||||||||
|
Subroutine
shingle_sentence: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 61 | 0 | my $sentence = shift; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | my $ngrammer = Text::NGrammer->new(lang => $self->{lang}); | |||||||||||||||||||||||||||
| 64 | 0 | return $self->_shingles_from_ngrams($ngrammer->ngrams_sentence($self->{w}, $sentence)); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 3 | ▶ | sub shingle_text { | ||||||||||||||||||||||||||
|
Subroutine
shingle_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | 3 | my $text = shift; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | my $ngrammer = Text::NGrammer->new(lang => $self->{lang}); | |||||||||||||||||||||||||||
| 72 | 3 | return $self->_shingles_from_ngrams($ngrammer->ngrams_text($self->{w}, $text)); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | 1; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | __END__ | ||||||||||||||||||||||||||||