| 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::NGrammer; | ||||||||||
| 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 Lingua::Sentence; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | $VERSION = '0.06'; | |||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||
| 16 | 1 | my $class = shift; | |||||||||
| 17 | 1 | my %config = (lang => 'en', # used by the sentencer | |||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | 1 | my %param = @_; | |||||||||
| 21 | |||||||||||
| 22 | 1 | for my $opt (keys %param) { | |||||||||
| 23 | 0 | ▶ | croak "option $opt unsupported by version $VERSION of Text::NGrammer" unless exists $config{$opt}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | $config{$opt} = $param{$opt}; | |||||||||
| 25 | } | ||||||||||
| 26 | 1 | $config{version} = $VERSION; | |||||||||
| 27 | 1 | return bless \%config, $class; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | ## | ||||||||||
| 31 | # Skip-grams | ||||||||||
| 32 | |||||||||||
| 33 | 11 | ▶ | sub skipgrams_array { | ||||||||
|
Subroutine
skipgrams_array: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 34 | 11 | my $self = shift; | |||||||||
| 35 | 11 | my $n = shift; | |||||||||
| 36 | 11 | my $k = shift; | |||||||||
| 37 | 11 | my $length = scalar(@_); | |||||||||
| 38 | |||||||||||
| 39 | 11 | ▶ | croak "the n-gram length cannot be lesser than 1" if $n < 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 11 | ▶ | croak "the tokens to be skipped cannot be lesser than 0" if $k < 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | 11 | my @ngrams = (); | |||||||||
| 43 | 11 | my $step = $k + 1; | |||||||||
| 44 | 11 | ▶ | for (my $i = 0; $i <= ($length - ($n+($k*($n-1)))); $i += 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 25 | my @tokens = (); | |||||||||
| 46 | 25 | my $at = $i; | |||||||||
| 47 | 25 | ▶ | while (@tokens < $n) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 64 | push @tokens, $_[$at]; | |||||||||
| 49 | 64 | $at += $step; | |||||||||
| 50 | } | ||||||||||
| 51 | 25 | push @ngrams, \@tokens; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 11 | return @ngrams; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 11 | ▶ | sub skipgrams_sentence { | ||||||||
|
Subroutine
skipgrams_sentence: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||
| 58 | 11 | my $self = shift; | |||||||||
| 59 | 11 | my $n = shift; | |||||||||
| 60 | 11 | my $k = shift; | |||||||||
| 61 | 11 | my $sentence = shift; | |||||||||
| 62 | |||||||||||
| 63 | 11 | ▶ | croak "the n-gram length cannot be lesser than 1" if $n < 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 11 | ▶ | croak "the tokens to be skipped cannot be lesser than 0" if $k < 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | # splits a string -- assumed to be a sentence -- according to spaces, control chars, etc. | ||||||||||
| 67 | 11 | my @tokens = grep /\S+/, split(/(?:\p{C}|\p{M}|\p{P}|\p{S}|\p{Z})+/, $sentence); | |||||||||
| 68 | 11 | ▶ | return () if @tokens < $n; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 11 | return $self->skipgrams_array($n, $k, @tokens); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 12 | ▶ | sub skipgrams_text { | ||||||||
|
Subroutine
skipgrams_text: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 73 | 12 | my $self = shift; | |||||||||
| 74 | 12 | my $n = shift; | |||||||||
| 75 | 12 | my $k = shift; | |||||||||
| 76 | 12 | my $text = shift; | |||||||||
| 77 | |||||||||||
| 78 | 12 | ▶ | croak "the n-gram length cannot be lesser than 1" if $n < 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 12 | ▶ | croak "the tokens to be skipped cannot be lesser than 0" if $k < 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | |||||||||||
| 81 | 11 | my @ngrams = (); | |||||||||
| 82 | |||||||||||
| 83 | 11 | my $splitter = Lingua::Sentence->new($self->{lang}); | |||||||||
| 84 | 11 | for my $sentence ($splitter->split_array($text)) { | |||||||||
| 85 | 11 | push @ngrams, $self->skipgrams_sentence($n, $k, $sentence); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 11 | return @ngrams; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | ## | ||||||||||
| 92 | # N-Grams | ||||||||||
| 93 | |||||||||||
| 94 | 0 | ▶ | sub ngrams_array { | ||||||||
|
Subroutine
ngrams_array: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 95 | 0 | my $self = shift; | |||||||||
| 96 | 0 | my $n = shift; | |||||||||
| 97 | |||||||||||
| 98 | 0 | return $self->skipgrams_array($n, 0, @_); | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 0 | ▶ | sub ngrams_sentence { | ||||||||
|
Subroutine
ngrams_sentence: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 102 | 0 | my $self = shift; | |||||||||
| 103 | 0 | my $n = shift; | |||||||||
| 104 | 0 | my $sentence = shift; | |||||||||
| 105 | |||||||||||
| 106 | 0 | return $self->skipgrams_sentencey($n, 0, $sentence); | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 3 | ▶ | sub ngrams_text { | ||||||||
|
Subroutine
ngrams_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 110 | 3 | my $self = shift; | |||||||||
| 111 | 3 | my $n = shift; | |||||||||
| 112 | 3 | my $text = shift; | |||||||||
| 113 | |||||||||||
| 114 | 3 | return $self->skipgrams_text($n, 0, $text); | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | |||||||||||
| 118 | 1 | 1; | |||||||||
| 119 | |||||||||||
| 120 | __END__ | ||||||||||