| 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 | ▶ | use 5.006; # 5.6.0 needed for "our" keyword | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | our @EXPORT = qw(name2verse); | |||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | our @EXPORT_OK = qw( ); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 5 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 5 | 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 | 8 | ▶ | sub name2verse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name2verse: called CC=9 SCAR=22.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 8 | my $name=shift; # grab the name for making the verse | |||||||||||||||||||||||||||||||||||||||||
| 66 | 8 | my ($f, $s, $i, $rest, $verse); | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # need >= two word-chars, including one vowel | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 8 | ▶ | unless ( defined $name and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $name and $name =~ /[a-z][a-z]/i and $name =~ /[aeiouy]/i
defined $name and $name =~ /[a-z][a-z]/i
MC/DC0%0/3
defined $name and $name =~ /[a-z][a-z]/i and $name =~ /[aeiouy]/i
defined $name
$name =~ /[a-z][a-z]/i
$name =~ /[aeiouy]/i
Truth table25%1/4
A and B and C
A= defined $nameB= $name =~ /[a-z][a-z]/iC= $name =~ /[aeiouy]/i
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | $name =~ /[a-z][a-z]/i and | ||||||||||||||||||||||||||||||||||||||||||
| 71 | $name =~/[aeiouy]/i ) { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | carp 'name2verse(): Name needs ', | |||||||||||||||||||||||||||||||||||||||||
| 73 | 'two or more letters ', | ||||||||||||||||||||||||||||||||||||||||||
| 74 | "including one vowel\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 8 | 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 | ▶ | $name =~ /^\s*([b-df-hj-np-tv-z]*)([a-z]+)/i or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 8 | $f="\u$1"; # force upper-case; "first" part (includes initial) | |||||||||||||||||||||||||||||||||||||||||
| 86 | 8 | $s="\L$2"; # force lower-case; "second" part | |||||||||||||||||||||||||||||||||||||||||
| 87 | 8 | $i=substr($f,0,1); # force upper-case; "initial" | |||||||||||||||||||||||||||||||||||||||||
| 88 | 8 | ▶ | $f ne '' and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | $verse="\u$f$s \u$f$s " . indent(); | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # Rule #2: General Case; Rob -> Bo-Bob | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 8 | ▶ | if ($i ne 'B') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 6 | $verse.="bo \uB$s, " . indent(); | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # Rule #2: Special Case For B-names; Bob -> Bo-ob | ||||||||||||||||||||||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | $verse.="${i}o-$rest, " . indent(); | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | # Rule #3: General Case; Tom -> Fo-Fom | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 8 | ▶ | if ($i ne 'F') { # Fo Fom | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 6 | $verse.="Banana Fanna Fo F$s, " . indent(); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # Rule #3: Special Case for F-names; Fred -> Fo-red | ||||||||||||||||||||||||||||||||||||||||||
| 113 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | $verse.="Banana Fanna ${i}o-$rest, " . indent(); | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # Rule #4: General Case; Tom -> Mo-Mom | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 8 | ▶ | if ($i ne 'M') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 6 | $verse.="Fee Fi Mo M$s\n\t"; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # Rule #4: Special Case for M-names; Marsha -> Mo-arsha | ||||||||||||||||||||||||||||||||||||||||||
| 121 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | $verse.="Fee Fi Mo-$s\n\t"; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 8 | $verse.="\U$f$s!"; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 8 | return $verse; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 24 | ▶ | sub indent { return ${SEP}.=' '; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
indent: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | __DATA__ | ||||||||||||||||||||||||||||||||||||||||||