| 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 texts | ||
| 6 | |||
| 7 | |||
| 8 | package Lingua::famibeib::Text; | ||
| 9 | |||
| 10 | use v5.16; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | use List::Util qw(any); | ||
| 16 | |||
| 17 | use Lingua::famibeib::Word; | ||
| 18 | use Lingua::famibeib::Fragment; | ||
| 19 | use Lingua::famibeib::Sentence; | ||
| 20 | |||
| 21 | our $VERSION = v0.08; | ||
| 22 | |||
| 23 | use parent 'Data::Identifier::Interface::Subobjects'; | ||
| 24 | |||
| 25 | my @_sentence_ends = map {Lingua::famibeib::Word->new(string => $_)->register} qw(ba be bi bo); | ||
| 26 | |||
| 27 | |||
| 28 | sub new { | ||
| 29 | my ($pkg, @opts) = @_; | ||
| 30 | my $self = bless {fragments => [], words => [], queue => ''}, $pkg; | ||
| 31 | |||
| 32 | croak 'Stray options passed' if scalar @opts; | ||
| 33 | |||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub fragments { | ||
| 39 | my ($self, @opts) = @_; | ||
| 40 | |||
| 41 | croak 'Stray options passed' if scalar @opts; | ||
| 42 | |||
| 43 | return @{$self->{fragments}}; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | #@returns __PACKAGE__ | ||
| 48 | sub parse_string { | ||
| 49 | my ($self, $str, @opts) = @_; | ||
| 50 | my $words = $self->{words}; | ||
| 51 | my $queue; | ||
| 52 | |||
| 53 | croak 'Stray options passed' if scalar @opts; | ||
| 54 | croak 'No string given' unless defined $str; | ||
| 55 | |||
| 56 | # First parse string into words: | ||
| 57 | $queue = $self->{queue}.' '.$str; | ||
| 58 | $self->{queue} = ''; | ||
| 59 | |||
| 60 | foreach my $wstr (split/[\s\r\n]/, $queue) { | ||
| 61 | my ($prefix, $ws, $suffix) = $wstr =~ /^([^bfklmst]*)((?:[bfklmst][aeiou]|[aeiou][bfklmst])*)([^bfklmstaeiou]*)$/; | ||
| 62 | push(@{$words}, $prefix) if defined($prefix) && length($prefix); | ||
| 63 | push(@{$words}, Lingua::famibeib::Word->new(string => $ws)) if defined($ws) && length($ws); | ||
| 64 | push(@{$words}, $suffix) if defined($suffix) && length($suffix); | ||
| 65 | } | ||
| 66 | |||
| 67 | # Now try to convert words into fragments: | ||
| 68 | outer: | ||
| 69 | while (scalar @{$words}) { | ||
| 70 | for (my $i = 0; $i < scalar(@{$words}); $i++) { | ||
| 71 | my $w = $words->[$i]; | ||
| 72 | |||
| 73 | if (!ref($w)) { | ||
| 74 | push(@{$self->{fragments}}, Lingua::famibeib::Fragment->new(words => [splice(@{$words}, 0, $i)])); | ||
| 75 | push(@{$self->{fragments}}, shift(@{$words})); | ||
| 76 | next outer; | ||
| 77 | } elsif ($w->is_application && any {$w->eq($_)} @_sentence_ends) { | ||
| 78 | my $f = Lingua::famibeib::Fragment->new(words => [splice(@{$words}, 0, $i + 1)]); | ||
| 79 | eval { Lingua::famibeib::Sentence->_upgrade($f) }; | ||
| 80 | push(@{$self->{fragments}}, $f); | ||
| 81 | next outer; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | last; | ||
| 86 | } | ||
| 87 | |||
| 88 | return $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | #@returns __PACKAGE__ | ||
| 93 | sub parse_done { | ||
| 94 | my ($self, @opts) = @_; | ||
| 95 | |||
| 96 | croak 'Stray options passed' if scalar @opts; | ||
| 97 | |||
| 98 | $self->parse_string(''); # flush all buffers | ||
| 99 | |||
| 100 | croak 'BUG' if length($self->{queue}); | ||
| 101 | |||
| 102 | if (scalar @{$self->{words}}) { | ||
| 103 | push(@{$self->{fragments}}, Lingua::famibeib::Fragment->new(words => $self->{words})); | ||
| 104 | $self->{words} = []; | ||
| 105 | } | ||
| 106 | |||
| 107 | return $self; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | sub as_string { | ||
| 112 | my ($self, @opts) = @_; | ||
| 113 | |||
| 114 | croak 'Stray options passed' if scalar @opts; | ||
| 115 | |||
| 116 | return join(' ', map {ref ? $_->as_string : $_ } @{$self->{fragments}}); | ||
| 117 | } | ||
| 118 | |||
| 119 | # ---- Private helpers ---- | ||
| 120 | |||
| 121 | 1; | ||
| 122 | |||
| 123 | __END__ |