| 1 | package Lingua::Stem::Ru; | ||||||||||||||||||||||||||||
| 2 | 3 | $Lingua::Stem::Ru::VERSION = '0.04_02'; # TRIAL | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | $Lingua::Stem::Ru::VERSION = '0.0402';use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Exporter 5.57 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | our @EXPORT = (); | |||||||||||||||||||||||||||
| 11 | 3 | our @EXPORT_OK = qw (stem stem_word clear_stem_cache stem_caching); | |||||||||||||||||||||||||||
| 12 | 3 | our %EXPORT_TAGS = (); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | my $Stem_Caching = 0; | |||||||||||||||||||||||||||
| 15 | 3 | my $Stem_Cache = {}; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | my $VOWEL = qr/ÁÅÉÏÕÙÜÀÑ/; | |||||||||||||||||||||||||||
| 18 | 3 | my $PERFECTIVEGROUND = qr/((É×|É×ÛÉ|É×ÛÉÓØ|Ù×|Ù×ÛÉ|Ù×ÛÉÓØ)|((?<=[ÁÑ])(×|×ÛÉ|×ÛÉÓØ)))$/; | |||||||||||||||||||||||||||
| 19 | 3 | my $REFLEXIVE = qr/(Ó[ÑØ])$/; | |||||||||||||||||||||||||||
| 20 | 3 | my $ADJECTIVE = qr/(ÅÅ|ÉÅ|ÙÅ|ÏÅ|ÉÍÉ|ÙÍÉ|ÅÊ|ÉÊ|ÙÊ|ÏÊ|ÅÍ|ÉÍ|ÙÍ|ÏÍ|ÅÇÏ|ÏÇÏ|ÅÍÕ|ÏÍÕ|ÉÈ|ÙÈ|ÕÀ|ÀÀ|ÁÑ|ÑÑ|ÏÀ|ÅÀ)$/; | |||||||||||||||||||||||||||
| 21 | 3 | my $PARTICIPLE = qr/((É×Û|Ù×Û|ÕÀÝ)|((?<=[ÁÑ])(ÅÍ|ÎÎ|×Û|ÀÝ|Ý)))$/; | |||||||||||||||||||||||||||
| 22 | 3 | my $VERB = qr/((ÉÌÁ|ÙÌÁ|ÅÎÁ|ÅÊÔÅ|ÕÊÔÅ|ÉÔÅ|ÉÌÉ|ÙÌÉ|ÅÊ|ÕÊ|ÉÌ|ÙÌ|ÉÍ|ÙÍ|ÅÎ|ÉÌÏ|ÙÌÏ|ÅÎÏ|ÑÔ|ÕÅÔ|ÕÀÔ|ÉÔ|ÙÔ|ÅÎÙ|ÉÔØ|ÙÔØ|ÉÛØ|ÕÀ|À)|((?<=[ÁÑ])(ÌÁ|ÎÁ|ÅÔÅ|ÊÔÅ|ÌÉ|Ê|Ì|ÅÍ|Î|ÌÏ|ÎÏ|ÅÔ|ÀÔ|ÎÙ|ÔØ|ÅÛØ|ÎÎÏ)))$/; | |||||||||||||||||||||||||||
| 23 | 3 | my $NOUN = qr/(Á|Å×|Ï×|ÉÅ|ØÅ|Å|ÉÑÍÉ|ÑÍÉ|ÁÍÉ|ÅÉ|ÉÉ|É|ÉÅÊ|ÅÊ|ÏÊ|ÉÊ|Ê|ÉÑÍ|ÑÍ|ÉÅÍ|ÅÍ|ÁÍ|ÏÍ|Ï|Õ|ÁÈ|ÉÑÈ|ÑÈ|Ù|Ø|ÉÀ|ØÀ|À|ÉÑ|ØÑ|Ñ)$/; | |||||||||||||||||||||||||||
| 24 | 3 | my $RVRE = qr/^(.*?[$VOWEL])(.*)$/; | |||||||||||||||||||||||||||
| 25 | 3 | my $DERIVATIONAL = qr/[^$VOWEL][$VOWEL]+[^$VOWEL]+[$VOWEL].*(?<=Ï)ÓÔØ?$/; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | sub stem { | ||||||||||||||||||||||||||
|
Subroutine
stem: called CC=13 SCAR=32.1
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | return [] if ($#_ == -1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 1 | my $parm_ref; | |||||||||||||||||||||||||||
| 30 | 1 | ▶ | if (ref $_[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 1 | $parm_ref = shift; | |||||||||||||||||||||||||||
| 32 | } else { | ||||||||||||||||||||||||||||
| 33 | 0 | $parm_ref = { @_ }; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | my $words = []; | |||||||||||||||||||||||||||
| 37 | 1 | my $locale = 'ru'; | |||||||||||||||||||||||||||
| 38 | 1 | my $exceptions = {}; | |||||||||||||||||||||||||||
| 39 | 1 | foreach (keys %$parm_ref) { | |||||||||||||||||||||||||||
| 40 | 1 | my $key = lc ($_); | |||||||||||||||||||||||||||
| 41 | 1 | ▶ | if ($key eq '-words') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 42 | 1 | @$words = @{$parm_ref->{$key}}; | |||||||||||||||||||||||||||
| 43 | } elsif ($key eq '-exceptions') { | ||||||||||||||||||||||||||||
| 44 | 0 | $exceptions = $parm_ref->{$key}; | |||||||||||||||||||||||||||
| 45 | } elsif ($key eq '-locale') { | ||||||||||||||||||||||||||||
| 46 | 0 | $locale = $parm_ref->{$key}; | |||||||||||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||||||||||
| 48 | 0 | croak (__PACKAGE__ . "::stem() - Unknown parameter '$key' with value '$parm_ref->{$key}'\n"); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | local( $_ ); | |||||||||||||||||||||||||||
| 53 | 1 | foreach (@$words) { | |||||||||||||||||||||||||||
| 54 | # Flatten case | ||||||||||||||||||||||||||||
| 55 | 49673 | $_ = lc $_; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Check against exceptions list | ||||||||||||||||||||||||||||
| 58 | 49673 | ▶ | if (exists $exceptions->{$_}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | $_ = $exceptions->{$_}; | |||||||||||||||||||||||||||
| 60 | 0 | next; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # Check against cache of stemmed words | ||||||||||||||||||||||||||||
| 64 | 49673 | my $original_word = $_; | |||||||||||||||||||||||||||
| 65 | 49673 | ▶ | 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}
| |||||||||||||||||||||||||||||
| 66 | 0 | $_ = $Stem_Cache->{$original_word}; | |||||||||||||||||||||||||||
| 67 | 0 | next; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 49673 | $_ = stem_word($_); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 49673 | ▶ | $Stem_Cache->{$original_word} = $_ if $Stem_Caching; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 1 | ▶ | $Stem_Cache = {} if ($Stem_Caching < 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1 | return $words; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 49682 | ▶ | sub stem_word { | ||||||||||||||||||||||||||
|
Subroutine
stem_word: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 49682 | my $word = lc shift; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Check against cache of stemmed words | ||||||||||||||||||||||||||||
| 83 | 49682 | ▶ | 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}
| |||||||||||||||||||||||||||||
| 84 | 0 | return $Stem_Cache->{$word}; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 49682 | my ($start, $RV) = $word =~ /$RVRE/; | |||||||||||||||||||||||||||
| 88 | 49682 | ▶ | return $word unless $RV; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Step 1 | ||||||||||||||||||||||||||||
| 91 | 49465 | ▶ | unless ($RV =~ s/$PERFECTIVEGROUND//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 49033 | $RV =~ s/$REFLEXIVE//; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 49033 | ▶ | if ($RV =~ s/$ADJECTIVE//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 15241 | $RV =~ s/$PARTICIPLE//; | |||||||||||||||||||||||||||
| 96 | } else { | ||||||||||||||||||||||||||||
| 97 | 33792 | ▶ | $RV =~ s/$NOUN// unless $RV =~ s/$VERB//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Step 2 | ||||||||||||||||||||||||||||
| 102 | 49465 | $RV =~ s/É$//; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Step 3 | ||||||||||||||||||||||||||||
| 105 | 49465 | ▶ | $RV =~ s/ÏÓÔØ?$// if $RV =~ /$DERIVATIONAL/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Step 4 | ||||||||||||||||||||||||||||
| 108 | 49465 | ▶ | unless ($RV =~ s/Ø$//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 49034 | $RV =~ s/ÅÊÛÅ?//; | |||||||||||||||||||||||||||
| 110 | 49034 | $RV =~ s/ÎÎ$/Î/; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 49465 | return $start.$RV; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub stem_caching { | ||||||||||||||||||||||||||
|
Subroutine
stem_caching: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 0 | my $parm_ref; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | if (ref $_[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | $parm_ref = shift; | |||||||||||||||||||||||||||
| 120 | } else { | ||||||||||||||||||||||||||||
| 121 | 0 | $parm_ref = { @_ }; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 0 | my $caching_level = $parm_ref->{-level}; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | if (defined $caching_level) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ($caching_level !~ m/^[012]$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | croak(__PACKAGE__ . "::stem_caching() - Legal values are '0','1' or '2'. '$caching_level' is not a legal value"); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 0 | $Stem_Caching = $caching_level; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 0 | return $Stem_Caching; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub clear_stem_cache { | ||||||||||||||||||||||||||
|
Subroutine
clear_stem_cache: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 0 | $Stem_Cache = {}; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 3 | 1; | |||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||