| 1 | # Copyright (c) 2026 Philipp Schafft | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Perl module to provide generic, language independent, interfaces to language data | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package Lingua::Generic::Interface; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use v5.20; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Data::Identifier v0.30; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Data::Identifier::Generate; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | our $VERSION = v0.03; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | #@returns Lingua::Generic::Interface::Word | ||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub new_word { | ||||||||||||||||||||||||||
|
Subroutine
new_word: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 0 | my ($pkg, $language, $string, @opts) = @_; | |||||||||||||||||||||||||||
| 24 | 0 | my $ret; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | croak 'Stray options passed' if scalar @opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | $language = _make_language($language); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if (defined($language)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ($language->eq('1d668738-8aef-4cb4-a4ed-9368e872a93f')) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | return Lingua::famibeib::Word->new(string => $string) if state $have_famibeib = eval {require Lingua::famibeib::Word}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | } elsif ($language->eq('f21986c8-baa1-5b7e-b357-2a76285c4778')) { | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | return Lingua::TokiPona::Word->new(string => $string) if state $have_TokiPona = eval {require Lingua::TokiPona::Word}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } elsif ($language->eq('8ca63437-1b1e-4a85-8512-02ba5c15a412')) { | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | return Lingua::Lapine::Word->new(string => $string) if state $have_Lapine = eval {require Lingua::Lapine::Word}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | $ret = Lingua::Generic::Interface::_IMPL::GenericWord->new(string => $string); | |||||||||||||||||||||||||||
| 41 | 0 | $ret->{natural_language} = $language; | |||||||||||||||||||||||||||
| 42 | 0 | return $ret; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | package Lingua::Generic::Interface::_IMPL::GenericWord { | |||||||||||||||||||||||||||
| 46 | 1 | ▶ | use parent 'Lingua::Generic::Interface::Word'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub natural_language { | ||||||||||||||||||||||||||
|
Subroutine
natural_language: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 50 | 0 | my ($self, @opts) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | croak 'Stray options passed' if scalar @opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return $self->{natural_language} // croak 'Unknown language'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"natural_language"} // croak("Unknown language")
MC/DC0%0/1
$self->{"natural_language"} // croak("Unknown language")
$self->{"natural_language"}
Truth table0%0/2
A
A= $self->{"natural_language"}
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | #@returns Lingua::Generic::Interface::Modifier | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub new_modifier { | ||||||||||||||||||||||||||
|
Subroutine
new_modifier: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 0 | my ($pkg, $language, $input, @opts) = @_; | |||||||||||||||||||||||||||
| 61 | 0 | my $string; | |||||||||||||||||||||||||||
| 62 | my $id; | ||||||||||||||||||||||||||||
| 63 | 0 | my $ret; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | croak 'Stray options passed' if scalar @opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if (ref $input) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $id = Data::Identifier->new(from => $input); | |||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||
| 70 | 0 | $string = $input; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | $ret = bless { | |||||||||||||||||||||||||||
| 74 | string => $string, | ||||||||||||||||||||||||||||
| 75 | id => $id, | ||||||||||||||||||||||||||||
| 76 | }, 'Lingua::Generic::Interface::_IMPL::GenericModifier'; | ||||||||||||||||||||||||||||
| 77 | 0 | $ret->{natural_language} = _make_language($language); | |||||||||||||||||||||||||||
| 78 | 0 | return $ret; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | package Lingua::Generic::Interface::_IMPL::GenericModifier { | |||||||||||||||||||||||||||
| 82 | 1 | ▶ | use parent 'Lingua::Generic::Interface::Modifier'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | *natural_language = *Lingua::Generic::Interface::_IMPL::GenericWord::natural_language; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub eq { | ||||||||||||||||||||||||||
|
Subroutine
eq: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 87 | 0 | my ($self, $other, @opts) = @_; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | if (eval{$other->isa(__PACKAGE__)} && $self->isa(__PACKAGE__)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
eval {
do {
$other->isa(__PACKAGE__)
}
} and $self->isa(__PACKAGE__)
MC/DC0%0/2
eval {
do {
$other->isa(__PACKAGE__)
}
} and $self->isa(__PACKAGE__)
eval {
do {
$other->isa(__PACKAGE__)
}
}
$self->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= eval {
do {
$other->isa(__PACKAGE__)
}
}B= $self->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if (defined($self->{id}) && defined($other->{id})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"id"} and defined $other->{"id"}
MC/DC0%0/2
defined $self->{"id"} and defined $other->{"id"}
defined $self->{"id"}
defined $other->{"id"}
Truth table0%0/3
A and B
A= defined $self->{"id"}B= defined $other->{"id"}
| |||||||||||||||||||||||||||||
| 90 | 0 | return $self->{id}->eq($other->{id}); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 0 | return $self->SUPER::eq($other, @opts); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub as { | ||||||||||||||||||||||||||
|
Subroutine
as: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 97 | 0 | my ($self, $as, @opts) = @_; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if (defined(my Data::Identifier $id = $self->{id})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | return $id if $as eq 'Data::Identifier' && scalar(@opts) == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$as eq "Data::Identifier" and scalar @opts == 0
MC/DC0%0/2
$as eq "Data::Identifier" and scalar @opts == 0
$as eq "Data::Identifier"
scalar @opts == 0
Truth table0%0/3
A and B
A= $as eq "Data::Identifier"B= scalar @opts == 0
| |||||||||||||||||||||||||||||
| 101 | 0 | return $id->as($as, @opts); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | return $self->SUPER::as($as, @opts); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub ise { | ||||||||||||||||||||||||||
|
Subroutine
ise: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 108 | 0 | my ($self, @opts) = @_; | |||||||||||||||||||||||||||
| 109 | 0 | return $self->{id}->ise(@opts); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub displayname { | ||||||||||||||||||||||||||
|
Subroutine
displayname: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 113 | 0 | my ($self, @opts) = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if (defined($self->{id}) && !defined($self->{string})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"id"} and not defined $self->{"string"}
MC/DC0%0/2
defined $self->{"id"} and not defined $self->{"string"}
defined $self->{"id"}
not defined $self->{"string"}
Truth table0%0/3
A and not(B)
A= defined $self->{"id"}B= not defined $self->{"string"}
| |||||||||||||||||||||||||||||
| 116 | 0 | return $self->{id}->displayname(@opts); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | return $self->SUPER::displayname(@opts); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | #@returns Lingua::Generic::Interface::Fragment | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub new_fragment { | ||||||||||||||||||||||||||
|
Subroutine
new_fragment: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | 0 | my ($pkg, $language, $input, @opts) = @_; | |||||||||||||||||||||||||||
| 127 | 0 | my $string; | |||||||||||||||||||||||||||
| 128 | my $id; | ||||||||||||||||||||||||||||
| 129 | 0 | my $ret; | |||||||||||||||||||||||||||
| 130 | 0 | my @words; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | croak 'Stray options passed' if scalar @opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if (ref $input) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | @words = grep {$_->isa('Lingua::Generic::Interface::Word') or croak 'Invalid input'} @{$input}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_->isa('Lingua::Generic::Interface::Word') || croak('Invalid input')
MC/DC0%0/1
$_->isa('Lingua::Generic::Interface::Word') || croak('Invalid input')
$_->isa('Lingua::Generic::Interface::Word')
Truth table0%0/2
A
A= $_->isa('Lingua::Generic::Interface::Word')
| |||||||||||||||||||||||||||||
| 136 | } elsif ($input =~ /^(?:[a-zA-Z\N{U+00E4}\N{U+00F6}\N{U+00FC}\N{U+00DF}\N{U+00E9}]+\s+)*[a-zA-Z\N{U+00E4}\N{U+00F6}\N{U+00FC}\N{U+00DF}\N{U+00E9}]+\z/i) { | ||||||||||||||||||||||||||||
| 137 | 0 | @words = map {$pkg->new_word($language => $_)} split /\s+/, $input; | |||||||||||||||||||||||||||
| 138 | } else { | ||||||||||||||||||||||||||||
| 139 | 0 | croak 'Invalid input'; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | $ret = bless { | |||||||||||||||||||||||||||
| 143 | words => \@words, | ||||||||||||||||||||||||||||
| 144 | }, 'Lingua::Generic::Interface::_IMPL::GenericFragmentOfWords'; | ||||||||||||||||||||||||||||
| 145 | 0 | $ret->{natural_language} = _make_language($language); | |||||||||||||||||||||||||||
| 146 | 0 | return $ret; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 1 | package Lingua::Generic::Interface::_IMPL::GenericFragmentOfWords { | |||||||||||||||||||||||||||
| 149 | 1 | ▶ | use parent 'Lingua::Generic::Interface::Fragment'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub words { | ||||||||||||||||||||||||||
|
Subroutine
words: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 153 | 0 | my ($self, @opts) = @_; | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | croak 'Stray options passed' if scalar @opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | return @{$self->{words}}; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub natural_language { | ||||||||||||||||||||||||||
|
Subroutine
natural_language: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 159 | 0 | my ($self, @opts) = @_; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | croak 'Stray options passed' if scalar @opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | return $self->{natural_language} // $self->SUPER::natural_language(@opts); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"natural_language"} // $self->SUPER::natural_language(@opts)
MC/DC0%0/2
$self->{"natural_language"} // $self->SUPER::natural_language(@opts)
$self->{"natural_language"}
$self->SUPER::natural_language(@opts)
Truth table0%0/3
A // B
A= $self->{"natural_language"}B= $self->SUPER::natural_language(@opts)
| |||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # ---- Private helpers ---- | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | sub _make_language { | ||||||||||||||||||||||||||
|
Subroutine
_make_language: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 168 | 0 | my ($language) = @_; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | if (defined $language) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | if (ref $language) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | return Data::Identifier->new(from => $language)->null_to_undef; | |||||||||||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if ($language eq 'tok') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | # workaround for I18N::LangTags::List | ||||||||||||||||||||||||||||
| 176 | 0 | return state $tok = do { | |||||||||||||||||||||||||||
| 177 | 0 | eval { Data::Identifier::Generate->language('tok') }; # try to, maybe it is fixed at some point | |||||||||||||||||||||||||||
| 178 | 0 | Data::Identifier->new(uuid => 'f21986c8-baa1-5b7e-b357-2a76285c4778')->register; | |||||||||||||||||||||||||||
| 179 | }; | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | 0 | return Data::Identifier::Generate->language($language); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | return undef; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 1 | 1; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||||||||