| 1 | # Copyright (c) 2025-2026 Philipp Schafft | ||
| 2 | |||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||
| 4 | |||
| 5 | # ABSTRACT: module to interact with the famibeib sentences | ||
| 6 | |||
| 7 | |||
| 8 | package Lingua::famibeib::Sentence; | ||
| 9 | |||
| 10 | use v5.16; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | use List::Util qw(any); | ||
| 16 | |||
| 17 | our $VERSION = v0.07; | ||
| 18 | |||
| 19 | use parent 'Lingua::famibeib::Fragment'; | ||
| 20 | |||
| 21 | use constant { | ||
| 22 | TYPE_STATEMENT => Lingua::famibeib::Word->new(string => 'ba')->register, | ||
| 23 | TYPE_QUESTION => Lingua::famibeib::Word->new(string => 'be')->register, | ||
| 24 | TYPE_COMMAND => Lingua::famibeib::Word->new(string => 'bi')->register, | ||
| 25 | TYPE_EXCLAMATION => Lingua::famibeib::Word->new(string => 'bo')->register, | ||
| 26 | }; | ||
| 27 | |||
| 28 | my @_sentence_ends = (TYPE_STATEMENT, TYPE_QUESTION, TYPE_COMMAND, TYPE_EXCLAMATION); | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my ($pkg, @opts) = @_; | ||
| 32 | return __PACKAGE__->_upgrade($pkg->SUPER::new(@opts)); | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | #@returns Lingua::famibeib::Word | ||
| 37 | sub type { | ||
| 38 | my ($self, @opts) = @_; | ||
| 39 | |||
| 40 | croak 'Stray options passed' if scalar @opts; | ||
| 41 | |||
| 42 | return $self->{type}; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | sub is_statement { | ||
| 47 | my ($self, @opts) = @_; | ||
| 48 | |||
| 49 | croak 'Stray options passed' if scalar @opts; | ||
| 50 | |||
| 51 | return $self->{type}->eq(TYPE_STATEMENT); | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub is_question { | ||
| 56 | my ($self, @opts) = @_; | ||
| 57 | |||
| 58 | croak 'Stray options passed' if scalar @opts; | ||
| 59 | |||
| 60 | return $self->{type}->eq(TYPE_QUESTION); | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | sub is_command { | ||
| 65 | my ($self, @opts) = @_; | ||
| 66 | |||
| 67 | croak 'Stray options passed' if scalar @opts; | ||
| 68 | |||
| 69 | return $self->{type}->eq(TYPE_COMMAND); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | sub is_exclamation { | ||
| 74 | my ($self, @opts) = @_; | ||
| 75 | |||
| 76 | croak 'Stray options passed' if scalar @opts; | ||
| 77 | |||
| 78 | return $self->{type}->eq(TYPE_EXCLAMATION); | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | #@returns Lingua::famibeib::Word | ||
| 83 | sub main_verb { | ||
| 84 | my ($self, @opts) = @_; | ||
| 85 | |||
| 86 | croak 'Stray options passed' if scalar @opts; | ||
| 87 | |||
| 88 | return $self->{main_verb}; | ||
| 89 | } | ||
| 90 | |||
| 91 | # ---- Private helpers ---- | ||
| 92 | |||
| 93 | sub _upgrade { | ||
| 94 | my ($pkg, $self) = @_; | ||
| 95 | my $words = $self->{words}; | ||
| 96 | my $type = $words->[-1]; | ||
| 97 | my $sublevel = 0; | ||
| 98 | my $main_verb; | ||
| 99 | |||
| 100 | croak 'Not a sentence, bad type: '.$type unless any {$type->eq($_)} @_sentence_ends; | ||
| 101 | |||
| 102 | for (my $i = 0; $i < (scalar(@{$words}) - 1); $i++) { | ||
| 103 | my $word = $words->[$i]; | ||
| 104 | |||
| 105 | if ($word->is_verb && $sublevel == 0) { | ||
| 106 | $main_verb //= $word; | ||
| 107 | } elsif ($word->is_application && any {$word->eq($_)} @_sentence_ends) { | ||
| 108 | croak 'Not a well formed sentence: inner sentence type found: '.$word; | ||
| 109 | } elsif ($word->as_string =~ /^tus/ && $word->is_application) { | ||
| 110 | $sublevel++; | ||
| 111 | } elsif ($word->as_string =~ /^tut/ && $word->is_application) { | ||
| 112 | $sublevel--; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | $self->{type} = $type; | ||
| 117 | $self->{main_verb} = $main_verb; | ||
| 118 | |||
| 119 | bless($self, $pkg); | ||
| 120 | |||
| 121 | return $self; | ||
| 122 | } | ||
| 123 | |||
| 124 | 1; | ||
| 125 | |||
| 126 | __END__ | ||
| 127 | |||
| 128 | =pod | ||
| 129 | |||
| 130 | =encoding UTF-8 | ||
| 131 | |||
| 132 | =head1 NAME | ||
| 133 | |||
| 134 | Lingua::famibeib::Sentence - module to interact with the famibeib sentences | ||
| 135 | |||
| 136 | =head1 VERSION | ||
| 137 | |||
| 138 | version v0.07 | ||
| 139 | |||
| 140 | =head1 SYNOPSIS | ||
| 141 | |||
| 142 | use Lingua::famibeib::Sentence; | ||
| 143 | |||
| 144 | This module implements a fragment that is a proper well formed sentence. | ||
| 145 | This is ensured at construction time. | ||
| 146 | |||
| 147 | This module inherits from L<Lingua::famibeib::Fragment>. | ||
| 148 | |||
| 149 | =head1 METHODS | ||
| 150 | |||
| 151 | =head2 type | ||
| 152 | |||
| 153 | my Lingua::famibeib::Word $type = $sentence->type; | ||
| 154 | |||
| 155 | (since v0.01) | ||
| 156 | |||
| 157 | Returns the type of the sentence as a L<Lingua::famibeib::Word>. | ||
| 158 | |||
| 159 | =head2 is_statement | ||
| 160 | |||
| 161 | my $bool = $sentence->is_statement; | ||
| 162 | |||
| 163 | (since v0.01) | ||
| 164 | |||
| 165 | Returns whether the sentence is a statment (C<tubaik>). | ||
| 166 | |||
| 167 | =head2 is_question | ||
| 168 | |||
| 169 | my $bool = $sentence->is_question; | ||
| 170 | |||
| 171 | (since v0.01) | ||
| 172 | |||
| 173 | Returns whether the sentence is a question (C<tubeik>). | ||
| 174 | |||
| 175 | =head2 is_command | ||
| 176 | |||
| 177 | my $bool = $sentence->is_command; | ||
| 178 | |||
| 179 | (since v0.01) | ||
| 180 | |||
| 181 | Returns whether the sentence is a command (C<tubiik>). | ||
| 182 | |||
| 183 | =head2 is_exclamation | ||
| 184 | |||
| 185 | my $bool = $sentence->is_exclamation; | ||
| 186 | |||
| 187 | (since v0.01) | ||
| 188 | |||
| 189 | Returns whether the sentence is an exclamation (C<tuboik>). | ||
| 190 | |||
| 191 | =head2 main_verb | ||
| 192 | |||
| 193 | my Lingua::famibeib::Word $main_verb = $sentence->main_verb; | ||
| 194 | |||
| 195 | (since v0.01) | ||
| 196 | |||
| 197 | Returns the main verb of the sentence as a L<Lingua::famibeib::Word>. | ||
| 198 | |||
| 199 | B<Note:> | ||
| 200 | This method requires a well formed sentence to work correctly. | ||
| 201 | For non-well formed sentences the wrong verb might be returned. | ||
| 202 | |||
| 203 | =head1 AUTHOR | ||
| 204 | |||
| 205 | Philipp Schafft <lion@cpan.org> | ||
| 206 | |||
| 207 | =head1 COPYRIGHT AND LICENSE | ||
| 208 | |||
| 209 | This software is Copyright (c) 2025-2026 by Philipp Schafft <lion@cpan.org>. | ||
| 210 | |||
| 211 | This is free software, licensed under: | ||
| 212 | |||
| 213 | The Artistic License 2.0 (GPL Compatible) | ||
| 214 | |||
| 215 | =cut |