line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Namegame.pm module |
2
|
|
|
|
|
|
|
#########################################################+ |
3
|
|
|
|
|
|
|
# From the CONSULTIX "Perl Programming" class #+ |
4
|
|
|
|
|
|
|
# POB 70563, Seattle WA 98107 USA tim@consultix-inc.com #+ |
5
|
|
|
|
|
|
|
# Copyright 1997-2001, Tim Maher. All Rights Reserved #+ |
6
|
|
|
|
|
|
|
#########################################################+ |
7
|
|
|
|
|
|
|
# Tim Maher, tim@teachmeperl.com |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package Lingua::EN::Namegame; |
10
|
|
|
|
|
|
|
|
11
|
5
|
|
|
5
|
|
23306
|
use 5.006; # 5.6.0 needed for "our" keyword |
|
5
|
|
|
|
|
15
|
|
|
5
|
|
|
|
|
254
|
|
12
|
5
|
|
|
5
|
|
28
|
use strict; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
180
|
|
13
|
5
|
|
|
5
|
|
33
|
use warnings; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
123
|
|
14
|
5
|
|
|
5
|
|
24
|
use Carp; |
|
5
|
|
|
|
|
20
|
|
|
5
|
|
|
|
|
3798
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
require Exporter; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
19
|
|
|
|
|
|
|
our @EXPORT = qw(name2verse); |
20
|
|
|
|
|
|
|
our @EXPORT_OK = qw( ); |
21
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our ($SEP); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# name_game |
26
|
|
|
|
|
|
|
# A program that generates a rhyming verse from a name, |
27
|
|
|
|
|
|
|
# as in the famous song "The Name Game", by Shirley Ellis |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Inspired by "One-Liner #40", in TPJ #15, Fall 1999, p. 11. |
30
|
|
|
|
|
|
|
# by Sean M. Burke: |
31
|
|
|
|
|
|
|
# s<^([bcdfghjklmnpqrstvwxyz]*)(\w+).*> |
32
|
|
|
|
|
|
|
# <$1$2 $1$2 $1o $1$2 / Bonana-Fanna-Fo-F$2 / Fe Fi Mo M$2 / $1$2!>i; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# Reworked by Tim Maher to comply with the |
35
|
|
|
|
|
|
|
# rhyming rules of the song, as documented in |
36
|
|
|
|
|
|
|
# http://www.geocities.com/SunsetStrip/Palladium/1306/shirley.htm. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# GENERAL RULES: |
39
|
|
|
|
|
|
|
# 1) Say the name twice |
40
|
|
|
|
|
|
|
# "Dave Dave" |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# 2) Say "bo" and Name again, replacing Name's first letter by B |
43
|
|
|
|
|
|
|
# "Bo Bave" |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# 3) Say Banana Fanna Fo Name, replacing Name's first letter by F |
46
|
|
|
|
|
|
|
# "banana Fanna Fo Fave" |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# 4) Say Fee Fi Mo Name, replacing Name's first letter by M |
49
|
|
|
|
|
|
|
# "Fee Fi Mo Mave" |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# 5) Say Name once more |
52
|
|
|
|
|
|
|
# "Dave" |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# ALTERNATE RULES, for Names Starting with B, F, or M |
56
|
|
|
|
|
|
|
# (e.g., Marsha) |
57
|
|
|
|
|
|
|
# 2. B-names; instead of bo Bob, use Bo-ob |
58
|
|
|
|
|
|
|
# 3. F-names; instead of Banana Fanna Fo Fred |
59
|
|
|
|
|
|
|
# -- Banana Fanna Fo-red |
60
|
|
|
|
|
|
|
# 4. M-names; instead of Fee Fi Mo Marsha -- Fee Fi Mo-arsha |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub name2verse { |
64
|
|
|
|
|
|
|
|
65
|
8
|
|
|
8
|
0
|
1296
|
my $name=shift; # grab the name for making the verse |
66
|
8
|
|
|
|
|
24
|
my ($f, $s, $i, $rest, $verse); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# need >= two word-chars, including one vowel |
69
|
8
|
50
|
33
|
|
|
117
|
unless ( defined $name and |
|
|
|
33
|
|
|
|
|
70
|
|
|
|
|
|
|
$name =~ /[a-z][a-z]/i and |
71
|
|
|
|
|
|
|
$name =~/[aeiouy]/i ) { |
72
|
0
|
|
|
|
|
0
|
carp 'name2verse(): Name needs ', |
73
|
|
|
|
|
|
|
'two or more letters ', |
74
|
|
|
|
|
|
|
"including one vowel\n"; |
75
|
0
|
|
|
|
|
0
|
return undef; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
8
|
|
|
|
|
18
|
local $SEP="\n"; # meant to leak into sub indent() below |
79
|
|
|
|
|
|
|
# /^([bcdfghjklmnpqrstvwxyz]*)([a-zA-Z]+)/ |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Grab leading consonants (if present), and following vowels |
82
|
|
|
|
|
|
|
|
83
|
8
|
50
|
|
|
|
44
|
$name =~ /^\s*([b-df-hj-np-tv-z]*)([a-z]+)/i or return undef; |
84
|
|
|
|
|
|
|
|
85
|
8
|
|
|
|
|
36
|
$f="\u$1"; # force upper-case; "first" part (includes initial) |
86
|
8
|
|
|
|
|
28
|
$s="\L$2"; # force lower-case; "second" part |
87
|
8
|
|
|
|
|
17
|
$i=substr($f,0,1); # force upper-case; "initial" |
88
|
8
|
50
|
|
|
|
29
|
$f ne '' and |
89
|
|
|
|
|
|
|
$rest=substr($f,1) . $s; # "rest" of first part |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
#print "I, rest, F, S = $i, $rest, $f, $s\n"; |
92
|
|
|
|
|
|
|
# print "Using rule #2, for $f/$s\n"; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Now construct the verse for this name |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Rule #1: State name twice |
97
|
8
|
|
|
|
|
46
|
$verse="\u$f$s \u$f$s " . indent(); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# Rule #2: General Case; Rob -> Bo-Bob |
100
|
8
|
100
|
|
|
|
31
|
if ($i ne 'B') { |
101
|
6
|
|
|
|
|
25
|
$verse.="bo \uB$s, " . indent(); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
# Rule #2: Special Case For B-names; Bob -> Bo-ob |
104
|
|
|
|
|
|
|
else { |
105
|
2
|
|
|
|
|
10
|
$verse.="${i}o-$rest, " . indent(); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# Rule #3: General Case; Tom -> Fo-Fom |
109
|
8
|
100
|
|
|
|
33
|
if ($i ne 'F') { # Fo Fom |
110
|
6
|
|
|
|
|
19
|
$verse.="Banana Fanna Fo F$s, " . indent(); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
# Rule #3: Special Case for F-names; Fred -> Fo-red |
113
|
|
|
|
|
|
|
else { |
114
|
2
|
|
|
|
|
8
|
$verse.="Banana Fanna ${i}o-$rest, " . indent(); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
# Rule #4: General Case; Tom -> Mo-Mom |
117
|
8
|
100
|
|
|
|
27
|
if ($i ne 'M') { |
118
|
6
|
|
|
|
|
14
|
$verse.="Fee Fi Mo M$s\n\t"; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
# Rule #4: Special Case for M-names; Marsha -> Mo-arsha |
121
|
|
|
|
|
|
|
else { |
122
|
2
|
|
|
|
|
7
|
$verse.="Fee Fi Mo-$s\n\t"; |
123
|
|
|
|
|
|
|
} |
124
|
8
|
|
|
|
|
27
|
$verse.="\U$f$s!"; |
125
|
8
|
|
|
|
|
58
|
return $verse; |
126
|
|
|
|
|
|
|
} |
127
|
24
|
|
|
24
|
0
|
66
|
sub indent { return ${SEP}.=' '; } |
128
|
|
|
|
|
|
|
1; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
__DATA__ |