famibeib/Text.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 164 CC 15 SCAR 32.7
CC
15
Cov
0%
parse_string
72.0
parse_done
20.0
as_string
12.0
CRAP 26.2
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.07;
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__
124  
125 =pod
126  
127 =encoding UTF-8
128  
129 =head1 NAME
130  
131 Lingua::famibeib::Text - module to interact with the famibeib texts
132  
133 =head1 VERSION
134  
135 version v0.07
136  
137 =head1 SYNOPSIS
138  
139 use Lingua::famibeib::Text;
140  
141 my Lingua::famibeib::Text $text = Lingua::famibeib::Text->new;
142  
143 $text->parse_string('baba fafoof ba');
144 $text->parse_done;
145  
146 my @fragments = $text->fragments;
147  
148 This module is used to store and parse larger blocks of text.
149  
150 This module inherits from L<Data::Identifier::Interface::Subobjects>.
151  
152 =head1 METHODS
153  
154 =head2 new
155  
156 my Lingua::famibeib::Text $text = Lingua::famibeib::Text->new;
157  
158 (since v0.01)
159  
160 Creates a new text object. No options are currently supported.
161  
162 =head2 fragments
163  
164 my @fragments = $text->fragments;
165  
166 (since v0.01)
167  
168 Returns all complete fragments.
169  
170 Each element returned is a L<Lingua::famibeib::Fragment> or a plain string.
171 Plain strings are returned for parts the parser could not make sense of.
172  
173 =head2 parse_string
174  
175 $text->parse_string($string);
176  
177 (since v0.01)
178  
179 Parses a string and adds it to the input queue.
180 It is fine for the string to contain incomplete sentences.
181 However, it is not permitted to contain incomplete words.
182  
183 This method can be called over and over again to add more text.
184  
185 The parser will try to split the string into useful fragments (sentences),
186 but might generate smaller fragments if can't do it for some reason.
187  
188 B<Note:>
189 The input string (C<$string>) must be a plain perl string, not a byte string.
190 If you need to apply any encoding you must do this before.
191  
192 Returns C<$text> (experimental since v0.01).
193  
194 =head2 parse_done
195  
196 $text->parse_done;
197  
198 (since v0.01)
199  
200 Tell the parser that parsing is done and all buffers should be flushed.
201 Without calling this not all of the parsed text might be visible.
202  
203 It is possible restart parsing new text.
204 In that case the next text will not have any overlapping fragments with the last parse run.
205  
206 Returns C<$text> (experimental since v0.01).
207  
208 =head2 as_string
209  
210 my $str = $text->as_string;
211  
212 (since v0.01)
213  
214 Returns the current text as a string.
215  
216 =head1 AUTHOR
217  
218 Philipp Schafft <lion@cpan.org>
219  
220 =head1 COPYRIGHT AND LICENSE
221  
222 This software is Copyright (c) 2025-2026 by Philipp Schafft <lion@cpan.org>.
223  
224 This is free software, licensed under:
225  
226 The Artistic License 2.0 (GPL Compatible)
227  
228 =cut