| 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.08; | ||
| 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__ |