| 1 | package Lingua::Stem::Uk; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use vars qw (@ISA @EXPORT_OK @EXPORT %EXPORT_TAGS $VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | @ISA = qw (Exporter); | |||||||||||||||||||||||||||
| 12 | 2 | @EXPORT = (); | |||||||||||||||||||||||||||
| 13 | 2 | @EXPORT_OK = qw (stem stem_word clear_stem_cache stem_caching); | |||||||||||||||||||||||||||
| 14 | 2 | %EXPORT_TAGS = (); | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | 2 | $VERSION = "0.01"; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | my $Stem_Caching = 0; | |||||||||||||||||||||||||||
| 19 | 2 | my $Stem_Cache = {}; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | my $VOWEL = qr/аеіоуиєюяї/; | |||||||||||||||||||||||||||
| 22 | 2 | my $PERFECTIVEGROUND = qr/((ів|івши|івшись|ив|ивши|ившись)|((?<=[ая])(в|вши|вшись)))$/; | |||||||||||||||||||||||||||
| 23 | 2 | my $REFLEXIVE = qr/(с[яь])$/; | |||||||||||||||||||||||||||
| 24 | 2 | my $ADJECTIVE = qr/(ее|іе|ие|ое|ими|іми|ої|а|е|і|у|ю|ій|ий|ой|ем|ім|им|ом|его|ого|ему|ому|іх|их|ую|юю|ая|яя|ою|ею)$/; | |||||||||||||||||||||||||||
| 25 | 2 | my $PARTICIPLE = qr/((івш|ивш|уюч|ьн|л)|((?<=[ая])(ем|нн|вш|ющ|щ)))$/; | |||||||||||||||||||||||||||
| 26 | 2 | my $VERB = qr/((іла|ила|ена|ейте|уйте|іть|іли|или|ей|уй|іл|ил|ім|им|ен|іло|ило|ено|ять|ує|ують|іт|ит|ени|іть|ить|ую|ю)|((?<=[ая])(ла|на|ете|йте|ли|й|л|ем|н|ло|но|ет|ют|ны|ть|ешь|нно)))$/; | |||||||||||||||||||||||||||
| 27 | 2 | my $NOUN = qr/(а|ев|ов|іе|ье|е|іями|ями|ами|еї|ії|и|ією|ею|єю|ой|ий|й|иям|ям|ием|ем|ам|ом|о|у|ах|іях|ях|и|і|ь|ію|ью|ю|ия|ья|я)$/; | |||||||||||||||||||||||||||
| 28 | 2 | my $RVRE = qr/^(.*?[$VOWEL])(.*)$/; | |||||||||||||||||||||||||||
| 29 | 2 | my $DERIVATIONAL = qr/[^$VOWEL][$VOWEL]+[^$VOWEL]+[$VOWEL].*(?<=і)сть?$/; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 4 | ▶ | sub stem { | ||||||||||||||||||||||||||
|
Subroutine
stem: called CC=13 SCAR=32.1
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | return [] if ($#_ == -1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 4 | my $parm_ref; | |||||||||||||||||||||||||||
| 34 | 4 | ▶ | if (ref $_[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 4 | $parm_ref = shift; | |||||||||||||||||||||||||||
| 36 | } else { | ||||||||||||||||||||||||||||
| 37 | 0 | $parm_ref = { @_ }; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 4 | my $words = []; | |||||||||||||||||||||||||||
| 41 | 4 | my $locale = 'uk'; | |||||||||||||||||||||||||||
| 42 | 4 | my $exceptions = {}; | |||||||||||||||||||||||||||
| 43 | 4 | foreach (keys %$parm_ref) { | |||||||||||||||||||||||||||
| 44 | 4 | my $key = lc ($_); | |||||||||||||||||||||||||||
| 45 | 4 | ▶ | if ($key eq '-words') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 46 | 4 | @$words = @{$parm_ref->{$key}}; | |||||||||||||||||||||||||||
| 47 | } elsif ($key eq '-exceptions') { | ||||||||||||||||||||||||||||
| 48 | 0 | $exceptions = $parm_ref->{$key}; | |||||||||||||||||||||||||||
| 49 | } elsif ($key eq '-locale') { | ||||||||||||||||||||||||||||
| 50 | 0 | $locale = $parm_ref->{$key}; | |||||||||||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||||||||||
| 52 | 0 | croak (__PACKAGE__ . "::stem() - Unknown parameter '$key' with value '$parm_ref->{$key}'\n"); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 4 | local( $_ ); | |||||||||||||||||||||||||||
| 57 | 4 | foreach (@$words) { | |||||||||||||||||||||||||||
| 58 | # Flatten case | ||||||||||||||||||||||||||||
| 59 | 34 | $_ = lc $_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Check against exceptions list | ||||||||||||||||||||||||||||
| 62 | 34 | ▶ | if (exists $exceptions->{$_}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | $_ = $exceptions->{$_}; | |||||||||||||||||||||||||||
| 64 | 0 | next; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Check against cache of stemmed words | ||||||||||||||||||||||||||||
| 68 | 34 | my $original_word = $_; | |||||||||||||||||||||||||||
| 69 | 34 | ▶ | if ($Stem_Caching && exists $Stem_Cache->{$original_word}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$Stem_Caching and exists $Stem_Cache->{$original_word}
MC/DC0%0/2
$Stem_Caching and exists $Stem_Cache->{$original_word}
$Stem_Caching
exists $Stem_Cache->{$original_word}
Truth table33%1/3
A and B
A= $Stem_CachingB= exists $Stem_Cache->{$original_word}
| |||||||||||||||||||||||||||||
| 70 | 0 | $_ = $Stem_Cache->{$original_word}; | |||||||||||||||||||||||||||
| 71 | 0 | next; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 34 | $_ = stem_word($_); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 34 | ▶ | $Stem_Cache->{$original_word} = $_ if $Stem_Caching; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 4 | ▶ | $Stem_Cache = {} if ($Stem_Caching < 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 4 | return $words; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 34 | ▶ | sub stem_word { | ||||||||||||||||||||||||||
|
Subroutine
stem_word: called CC=9 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 34 | my $word = lc shift; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Check against cache of stemmed words | ||||||||||||||||||||||||||||
| 87 | 34 | ▶ | if ($Stem_Caching && exists $Stem_Cache->{$word}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$Stem_Caching and exists $Stem_Cache->{$word}
MC/DC0%0/2
$Stem_Caching and exists $Stem_Cache->{$word}
$Stem_Caching
exists $Stem_Cache->{$word}
Truth table33%1/3
A and B
A= $Stem_CachingB= exists $Stem_Cache->{$word}
| |||||||||||||||||||||||||||||
| 88 | 0 | return $Stem_Cache->{$word}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 34 | my ($start, $RV) = $word =~ /$RVRE/; | |||||||||||||||||||||||||||
| 92 | 34 | ▶ | return $word unless $RV; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Step 1 | ||||||||||||||||||||||||||||
| 95 | 34 | ▶ | unless ($RV =~ s/$PERFECTIVEGROUND//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 32 | $RV =~ s/$REFLEXIVE//; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 32 | ▶ | if ($RV =~ s/$ADJECTIVE//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 29 | $RV =~ s/$PARTICIPLE//; | |||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||
| 101 | 3 | ▶ | $RV =~ s/$NOUN// unless $RV =~ s/$VERB//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Step 2 | ||||||||||||||||||||||||||||
| 106 | 34 | $RV =~ s/и$//; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Step 3 | ||||||||||||||||||||||||||||
| 109 | 34 | ▶ | $RV =~ s/ість?$// if $RV =~ /$DERIVATIONAL/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Step 4 | ||||||||||||||||||||||||||||
| 112 | 34 | ▶ | unless ($RV =~ s/ь$//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 34 | $RV =~ s/іше?//; | |||||||||||||||||||||||||||
| 114 | 34 | $RV =~ s/нн$/н/; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 34 | return $start.$RV; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub stem_caching { | ||||||||||||||||||||||||||
|
Subroutine
stem_caching: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 0 | my $parm_ref; | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | if (ref $_[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | $parm_ref = shift; | |||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||
| 125 | 0 | $parm_ref = { @_ }; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | my $caching_level = $parm_ref->{-level}; | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | if (defined $caching_level) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ($caching_level !~ m/^[012]$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | croak(__PACKAGE__ . "::stem_caching() - Legal values are '0','1' or '2'. '$caching_level' is not a legal value"); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 0 | $Stem_Caching = $caching_level; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 0 | return $Stem_Caching; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub clear_stem_cache { | ||||||||||||||||||||||||||
|
Subroutine
clear_stem_cache: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 0 | $Stem_Cache = {}; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 2 | 1; | |||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||