Chatbot/ScriptParser.pm

filter: Statementstmt 100% 66 / 66 Branchbran 86.3% 19 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 96.8% 92 / 95 SCAR 27.1
CC
15
Cov
97%
parse_script_data
12.0
_open_script_file
3.0
_unique_words
2.0
CRAP 15.0
« Chatbot/Brain.pm ↑ Summary Chatbot/Option.pm »
1 package Eliza::Chatbot::ScriptParser;
2  
3 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use MooX::LazierAttributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 attributes (
7 script_file => [rw, q{}],
8 [qw/quit initial final/] => [rw, [], {lzy}],
9 [qw/decomp reasmb reasmb_for_memory pre post synon key unique_words/] => [rw, { }, { lzy }],
10 );
11  
12 sub parse_script_data {
13 24 ▶ my $self = shift;
Subroutine parse_script_data: called CC=12 SCAR=24.9
Pod documented
14 24 my @script_lines = $self->_open_script_file($self->script_file);
15 24 my ($thiskey, $decomp);
16 # Examine each line of the script data
17 24 for my $line (@script_lines) {
18
19 # Skip comments and lines with only whitespace
20 8388 ▶ next if $line =~ /^[\s*#|\s*]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^[\s*#|\s*]$/ 18 8370 8388
21
22 # mehhh may be slow who knows
23 8370 $self->_unique_words($line);
24  
25 # Split entrytype and entry, using a colon as the delimiter
26 8370 my ($entry_type, $entry) = split /:/, $line;
27 # remove the whitespace
28 8370 $entry_type = _trim_string($entry_type);
29 8370 $entry = _trim_string($entry);
30
31 8370 for ($entry_type) {
32 8370 ▶ /quit|initial|final/ and do { push @{$self->$_}, $entry; last; };
Branch: 1 branch
BranchTrueFalseTotal
/quit|initial|final/ and do { push @{$self->$_;}, $entry; last } 234 8136 8370
33 8136 ▶ /decomp/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/decomp/ and do { die "$0: error parsing script: decomp rule with no keyword. \n" unless $thiskey; $decomp = join($;, $thiskey, $entry); push @{$self->$_->{$thiskey};}, $entry; last } 1386 6750 8136
34 1386 ▶ die "$0: error parsing script: decomp rule with no keyword. \n"
Branch: 1 branch
BranchTrueFalseTotal
unless $thiskey 0 1386 1386
35 unless $thiskey;
36 1386 $decomp = join($;, $thiskey, $entry);
37 1386 push @{$self->$_->{$thiskey}}, $entry;
38 1386 last;
39 };
40 6750 ▶ /reasmb|reasmb_for_mempory/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/reasmb|reasmb_for_mempory/ and do { die "$0: error parsing scrip reassembly rule with no decomposition rule" unless $decomp; push @{$self->$_->{$decomp};}, $entry; last } 5130 1620 6750
41 5130 ▶ die "$0: error parsing scrip reassembly rule with no decomposition rule"
Branch: 1 branch
BranchTrueFalseTotal
unless $decomp 0 5130 5130
42 unless $decomp;
43 5130 push @{$self->$_->{$decomp}}, $entry;
44 5130 last;
45 };
46 # everything else we have a key - split on first space
47 1620 my ($key, $value) = split(/\s/, $entry);
48 1620 ▶ /pre|post/ and do { $self->$_->{$key} = $value; last; };
Branch: 1 branch
BranchTrueFalseTotal
/pre|post/ and do { $self->$_->{$key} = $value; last } 522 1098 1620
49 1098 ▶ /synon/ and do { $self->$_->{$key} = [ split /\ /, $value ]; last; };
Branch: 1 branch
BranchTrueFalseTotal
/synon/ and do { $self->$_->{$key} = [split(/\ /, $value, 0)]; last } 144 954 1098
50 954 ▶ /key/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/key/ and do { $thiskey = $key; $decomp = ""; $self->$_->{$key} = $value; last } 882 72 954
51 882 $thiskey = $key;
52 882 $decomp = "";
53 882 $self->$_->{$key} = $value;
54 882 last;
55 };
56 }
57 }
58 }
59  
60 sub _unique_words {
61 8373 ▶ my ($self, $line) = @_;
Subroutine _unique_words: called CC=2 SCAR=6.9
62 8373 $line =~ s/[^a-zA-Z\'\s+]//g;
63 8373 my @words = split(' ', $line);
64 8373 foreach my $word ( @words ) {
65 40927 $self->unique_words->{$word}++;
66 }
67 8373 return;
68 };
69  
70 sub _trim_string {
71 16740 ▶ my $string = shift;
Subroutine _trim_string: called CC=1 SCAR=0.0
72 16740 $string =~ s/^\s+|\s+$//g;
73 16740 return $string;
74 }
75  
76 sub _open_script_file {
77 24 ▶ my ($self, $script_file) = @_;
Subroutine _open_script_file: called CC=3 SCAR=11.0
78 24 my @script_lines;
79 24 ▶ if ($script_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($script_file) { } 6 18 24
80 # If we have an external script file, open it
81 6 ▶ open (my $fh, "<", $script_file)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $script_file or die "Could not read from file $script_file : $!\n" 0 6 6
82 or die "Could not read from file $script_file : $!\n";
83
84 6 @script_lines = <$fh>;
85 6 close ($fh);
86  
87 6 $self->script_file($script_file);
88 }
89 else {
90 # Otherwise, read in the data from the bottom of this file.
91 # This data might be read several times, so we save the offset pointer
92 18 my $where = tell(DATA);
93 18 @script_lines = <DATA>;
94
95 # and reset it when we're done.
96 18 seek(DATA, $where, 0);
97 18 $self->script_file('none');
98 }
99 24 return @script_lines;
100 }
101  
102 12 1;
103  
104 =head1 Name
105  
106 Eliza::Chatbot::ScriptParser
107  
108 =head1 Version
109  
110 Version 0.08
111  
112 =head1 Options
113  
114 =over
115  
116 =item script_file
117  
118 =item quit
119  
120 =item initial
121  
122 =item final
123  
124 =item decomp
125  
126 =item reasmb
127  
128 =item reasmb_for_memory
129  
130 =item pre
131  
132 =item post
133  
134 =item synon
135  
136 =item key
137  
138 =item unique_words
139  
140 =back
141  
142 =head1 SUBROUTINES/METHODS
143  
144 =head2 parse_script_data()
145
146 $self->parse_script_data;
147 $self->parse_script_data( $script_file );
148  
149 parse_script_data() is invoked from the _initialize() method, which is called from
150 the new() function. However, you can also call this method at any time against
151 an already-instantiated Eliza instance. In that case, the new script data is I<added>
152 to the old script data. The old script data is not deleted.
153  
154 You can pass a parameter to this function, which is the name of the script file,
155 and it will read in and parse that file. If you do not pass any parameter to
156 this method, then it will read the data embedded at the end of the module as its
157 default script data.
158  
159 If you pass the name of a script file to parse_script_data(), and that file is
160 not available for reading, then the module dies.
161  
162 =head1 Format of the script file
163  
164 This module includes a default script file within itself, so it is not necessary
165 to explicitly specify a script file when instantiating an Eliza object. Each line
166 in the script file can specify a key, a decomposition rule, or a reassembly rule.
167  
168 key: remember 5
169 decomp: * i remember *
170 reasmb: Do you often think of (2) ?
171 reasmb: Does thinking of (2) bring anything else to mind ?
172 decomp: * do you remember *
173 reasmb: Did you think I would forget (2) ?
174 reasmb: What about (2) ?
175 reasmb: goto what
176 pre: equivalent alike
177 synon: belief feel think believe wish
178  
179 The number after the key specifies the rank. If a user's input contains the keyword, then
180 the transform() function will try to match one of the decomposition rules for that keyword.
181 If one matches, then it will select one of the reassembly rules at random. The number
182 (2) here means "use whatever set of words matched the second asterisk in the decomposition
183 rule." If you specify a list of synonyms for a word, the you should use a "@" when you use that
184 word in a decomposition rule:
185
186 decomp: * i @belief i *
187 reasmb: Do you really think so ?
188 reasmb: But you are not sure you (3).
189  
190 Otherwise, the script will never check to see if there are any synonyms for that keyword.
191 Reassembly rules should be marked with I<reasm_for_memory> rather than I<reasmb> when it is appropriate for use when a user's comment has been extracted from memory.
192
193 key: my 2
194 decomp: * my *
195 reasm_for_memory: Let's discuss further why your (2).
196 reasm_for_memory: Earlier you said your (2).
197 reasm_for_memory: But your (2).
198 reasm_for_memory: Does that have anything to do with the fact that your (2) ?
199  
200 =head1 AUTHOR
201  
202 LNATION email@lnation.org
203  
204 =head1 LICENSE AND COPYRIGHT
205  
206 Copyright 2017 Robert Acock.
207  
208 This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
209  
210 http://www.perlfoundation.org/artistic_license_2_0
211  
212 Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
213  
214 If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
215  
216 This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
217  
218 This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.
219  
220 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
221  
222 =cut
223  
224 __DATA__
« Chatbot/Brain.pm ↑ Summary Chatbot/Option.pm »