File Coverage

blib/lib/Lingua/TermWeight/WordSegmenter/LetterNgram.pm
Criterion Covered Total %
statement 29 29 100.0
branch 6 6 100.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 42 43 97.6


line stmt bran cond sub pod time code
1             # SPDX-FileCopyrightText: 2014 Koichi SATOH
2             # SPDX-FileCopyrightText: 2026 Wesley Schwengle
3             #
4             # SPDX-License-Identifier: MIT
5              
6             package Lingua::TermWeight::WordSegmenter::LetterNgram;
7             our $VERSION = '0.01';
8             # ABSTRACT: Word segmenter
9              
10 3     3   222590 use v5.20;
  3         9  
11 3     3   414 use utf8;
  3         266  
  3         31  
12 3     3   62 use warnings;
  3         3  
  3         136  
13 3     3   1654 use Object::Pad;
  3         28139  
  3         18  
14 3     3   396 use Carp qw(croak);
  3         5  
  3         583  
15              
16             class Lingua::TermWeight::WordSegmenter::LetterNgram {
17              
18             field $n : param;
19              
20             ADJUST {
21             croak "Word length must be 1+" if !defined($n) || $n <= 0;
22             }
23              
24             method n { $n }
25              
26 9     9 0 499 method segment ($document) {
  9         21  
  9         12  
  9         14  
27 9 100       22 $document = \"$document" unless ref $document;
28              
29 9         27 my $length = length $$document;
30 9         12 my $index = -1;
31 9         23 my $n = $self->n;
32              
33             return sub {
34             GET_NEXT_NGRAM: {
35 480     480   602 ++$index;
  616         774  
36 616 100       817 return if $index + $n > $length;
37              
38 607         826 my $ngram = substr $$document, $index, $n;
39 607 100       1023 redo GET_NEXT_NGRAM if $ngram =~ /\s/;
40 471         810 return $ngram;
41             }
42 9         44 };
43             }
44             }
45              
46             1;
47              
48             __END__