blib/lib/Text/Match.pm

filter: Statementstmt 65.7% 23 / 35 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 8 / 10 Podpod n/a totaltotal 55.3% 31 / 56 CC 5 SCAR 24.4
CC
5
Cov
52%
span
30.0
normalizeText
2.0
BEGIN
1.0
CRAP 11.4
↑ Summary
1 #!/usr/bin/perl -I/home/phil/perl/cpan/DataTableText/lib/
2 #-------------------------------------------------------------------------------
3 # Match text question against possible answer strings
4 # Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2021
5 #-------------------------------------------------------------------------------
6 # podDocumentation
7 package Text::Match;
8 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 our $VERSION = 20201221; # Version
10 1 ▶use warnings FATAL => qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Table::Text qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Math::Permute::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use feature qw(say current_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 sub normalizeText($) # Normalize a string of text
19 0 ▶ {my ($s) = @_; # String to normalize
Subroutine normalizeText: not called CC=1 SCAR=6.9
20 0 split /\s+/, lc $s =~ s(\W) ( )gsr
21 }
22  
23 sub span($$) # Return the length of the span if the first array is spanned by the second array otherwise undef
24 0 ▶ {my ($Q, $A) = @_; # Question, Answer
Subroutine span: not called CC=5 SCAR=34.0
25 0 my @m; my $n = 0;
26 0 ▶ while(@$A and @$Q) # Each answer word
Branch: 1 branch
BranchTrueFalseTotal
if (@$A and @$Q) 0 0 0
Condition0%0/3
@$A and @$Q
!ll&&!rl&&r
000
MC/DC0%0/2
@$A and @$Q
@$A @$Q
Truth table0%0/3
A and B
A= @$AB= @$Q
ABresult
0X0
100
111
27 0 ▶ {if ($$A[0] eq $$Q[0])
Branch: 1 branch
BranchTrueFalseTotal
if ($A->[0] eq $Q->[0]) { } 0 0 0
28 0 {shift @$Q;
29 0 shift @$A;
30 }
31 else
32 0 {++$n;
33 0 shift @$A;
34 }
35 }
36 0 ▶ @$Q ? undef : $n
Branch: 1 branch
BranchTrueFalseTotal
@$Q ? : 0 0 0
37 }
38  
39 sub randomizeArray(@) # Randomize an array
40 {my (@a) = @_; # Array
41 for my $i(keys @a)
42 {my $j = int ($#a * rand);
43 my $s = $a[$i]; my $t = $a[$j]; $a[$i] = $t; $a[$j] = $s;
44 }
45 @a
46 }
47  
48 sub score # Respond to a question with a similar answer
49 {my ($Q, $A) = @_; # Question, Answer
50 my @q = normalizeText $Q;
51 my @a = normalizeText $A;
52 my @m;
53 while(@a) # Each answer word
54 {my $s = span([@q], [@a]) // span([reverse @q], [@a]); # Normal sequence or reversed
55 if (defined $s)
56 {push @m, [$s, $A];
57 }
58 else # All permutations if necessary
59 {permute
60 {my $s = span([@_], [@a]);
61 push @m, [$s, $A] if defined $s;
62 } @q;
63 }
64 shift @a;
65 }
66 @m
67 }
68  
69 #D1 Match Text # Match some text against possible answers
70 sub response($$) # Respond to a question with a plausible answer
71 {my ($Q, $A) = @_; # Question, possible answers
72 my @m;
73 for my $A(@$A) # Each possible answer
74 {push @m, score($Q, $A);
75 }
76 return '' unless @m;
77 my ($m) =
78 sort { $$a[0] <=> $$b[0]} # Smallest score first
79 sort {length($$a[1]) <=> length($$b[1])} @m; # Shortest string first
80 $$m[1]
81 }
82 #d
83 #-------------------------------------------------------------------------------
84 # Export - eeee
85 #-------------------------------------------------------------------------------
86  
87 use Exporter qw(import);
88  
89 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
90  
91 @ISA = qw(Exporter);
92 @EXPORT = qw();
93 @EXPORT_OK = qw(
94 response
95 );
96 %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]);
97  
98 # podDocumentation
99 =pod
100  
101 =encoding utf-8
102  
103 =head1 Name
104  
105 Text::Match - Match text question against possible answer strings
106  
107 =head1 Synopsis
108  
109 =head1 Description
110  
111 Match text question against possible answer strings
112  
113  
114 Version 20201221.
115  
116  
117 The following sections describe the methods in each functional area of this
118 module. For an alphabetic listing of all methods by name see L<Index|/Index>.
119  
120  
121  
122 =head1 Match Text
123  
124 Match some text against possible answers
125  
126 =head2 response($Q, $A)
127  
128 Respond to a question with a plausible answer
129  
130 Parameter Description
131 1 $Q Question
132 2 $A Possible answers
133  
134 B<Example:>
135  
136  
137  
138 is_deeply response("a c", ["a b c", "a b c d"]), "a b c"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
139  
140  
141 is_deeply response("a c", ["a b c", "a b c d"]), "a b c"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
142  
143  
144 is_deeply response("a d", ["a b c", "a b c d"]), "a b c d"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
145  
146  
147  
148 is_deeply response("b d", ["a b c d", "a b c d e"]), "a b c d"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
149  
150  
151 is_deeply response("b d", ["a b c d", "a b c d e"]), "a b c d"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
152  
153  
154 is_deeply response("b e", ["a b c d", "a b c d e"]), "a b c d e"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
155  
156  
157  
158 is_deeply response("c a", ["a b c", "a b c d"]), "a b c"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
159  
160  
161 is_deeply response("c a", ["a b c", "a b c d"]), "a b c"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
162  
163  
164 is_deeply response("d a", ["a b c", "a b c d"]), "a b c d"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
165  
166  
167  
168 is_deeply response("d b", ["a b c d", "a b c d e"]), "a b c d"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
169  
170  
171 is_deeply response("d b", ["a b c d", "a b c d e"]), "a b c d"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
172  
173  
174 is_deeply response("e b", ["a b c d", "a b c d e"]), "a b c d e"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
175  
176  
177  
178 is_deeply response("c a b", ["a b c", "a b c d"]), "a b c"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
179  
180  
181 is_deeply response("c a d", ["a b c", "a b c d"]), "a b c d"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
182  
183  
184 is_deeply response("c a b d", ["a b c", "a b c d", "C a b d"]), "a b c d"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
185  
186  
187  
188  
189 =head1 Index
190  
191  
192 1 L<response|/response> - Respond to a question with a plausible answer
193  
194 =head1 Installation
195  
196 This module is written in 100% Pure Perl and, thus, it is easy to read,
197 comprehend, use, modify and install via B<cpan>:
198  
199 sudo cpan install TextMatch
200  
201 =head1 Author
202  
203 L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com>
204  
205 L<http://www.appaapps.com|http://www.appaapps.com>
206  
207 =head1 Copyright
208  
209 Copyright (c) 2016-2021 Philip R Brenan.
210  
211 This module is free software. It may be used, redistributed and/or modified
212 under the same terms as Perl itself.
213  
214 =cut
215  
216  
217  
218 # Tests and documentation
219  
220 sub test
221 {my $p = __PACKAGE__;
222 binmode($_, ":utf8") for *STDOUT, *STDERR;
223 return if eval "eof(${p}::DATA)";
224 my $s = eval "join('', <${p}::DATA>)";
225 $@ and die $@;
226 eval $s;
227 $@ and die $@;
228 1
229 }
230  
231 test unless caller;
232  
233 1;
234 # podDocumentation
235 __DATA__
↑ Summary