NYSIIS: Difference between revisions

2,861 bytes added ,  11 years ago
→‎{{header|Perl 6}}: Added a Perl 6 entry
(tidy up task description, tidy up current implementation (conventionally, example goes after implementation))
(→‎{{header|Perl 6}}: Added a Perl 6 entry)
Line 193:
USER>Write code
MCLAGLAN
</pre>
 
 
=={{header|Perl 6}}==
This implementation removes common name suffixes similar to the reference implementation, even though it is not specified in the task description or on the linked [[wp:New York State Identification and Intelligence System|NYSIIS]] page. This algorithm isn't too friendly to certain French kings. :)
 
<lang perl6>
sub no_suffix ($name is copy) {
$name .= uc;
$name.subst( /\h (<[JS]>R) | (<[IVX]>+) $/, '' );
}
 
sub nysiis ($name is copy) {
$name .= uc;
$name.=subst( /<-[A..Z]>/, '', :g )\
.=subst( /^MAC/, 'MCC' )\
.=subst( /^P<[FH]>/, 'FF' )\
.=subst( /^SCH/, 'SSS' )\
.=subst( /^KN/, 'N' )\
.=subst( /<[IE]>E$/, 'Y' )\
.=subst( /<[DRN]>T$/, 'D' )\
.=subst( /<[RN]>D$/, 'D' );
my $first = substr-rw( $name, 0, 1 ) = '';
$name.=subst( /EV/, 'AF', :g )\
.=subst( /<[AEIOU]>+/, 'A', :g )\
.=subst( /Q/, 'G', :g )\
.=subst( /Z/, 'S', :g )\
.=subst( /M/, 'N', :g )\
.=subst( /KN/, 'N', :g )\
.=subst( /K/, 'C', :g )\
.=subst( /SCH/, 'S', :g )\
.=subst( /PF/, 'F', :g )\
.=subst( /K/ , 'C', :g )\
.=subst( /H(<-[AEIOU]>)/, -> $/ {$0}, :g );
$name = $first ~ $name;
$name.subst( /(<-[AEIOU]>)H/, -> $/ {$0}, :g )\
.subst( /(<[AEIOU]>)W/, -> $/ {$0}, :g )\
.subst( /AY$/, 'Y', :g )\
.subst( /S$/ , '' )\
.subst( /A$/ , '' )\
.subst( /(.)$0+/, -> $/ {$0}, :g );
}
 
my $nysiis;
 
printf "%10s, %s\n", $_,
( $nysiis = nysiis no_suffix $_ ).chars > 6
?? $nysiis.subst( / (.**6) /, -> $/ { $0 ~ '[' } ) ~ ']'
!! $nysiis
for (
"knight", "mitchell", "o'daniel", "brown sr", "browne III",
"browne IV", "O'Banion", "Mclaughlin", "McCormack", "Chapman",
"Silva", "McDonald", "Lawson", "Jacobs", "Greene",
"O'Brien", "Morrison", "Larson", "Willis", "Mackenzie",
"Carr", "Lawrence", "Matthews", "Richards", "Bishop",
"Franklin", "McDaniel", "Harper", "Lynch", "Watkins",
"Carlson", "Wheeler" , "Louis XVI"
);
 
</lang>
 
Output:
 
<pre>
knight, NAGT
mitchell, MATCAL
o'daniel, ODANAL
brown sr, BRAN
browne III, BRAN
browne IV, BRAN
O'Banion, OBANAN
Mclaughlin, MCLAGL[AN]
McCormack, MCARNA[C]
Chapman, CAPNAN
Silva, SALV
McDonald, MCDANA[LD]
Lawson, LASAN
Jacobs, JACAB
Greene, GRAN
O'Brien, OBRAN
Morrison, MARASA[N]
Larson, LARSAN
Willis, WAL
Mackenzie, MCANSY
Carr, CAR
Lawrence, LARANC
Matthews, MAT
Richards, RACARD
Bishop, BASAP
Franklin, FRANCL[AN]
McDaniel, MCDANA[L]
Harper, HARPAR
Lynch, LYNC
Watkins, WATCAN
Carlson, CARLSA[N]
Wheeler, WALAR
Louis XVI, L
</pre>
10,351

edits