NYSIIS: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 25:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V _vowels = ‘AEIOU’
 
F replace_at(String text; position, fromlist, tolist)
Line 73:
‘knight’, ‘mitchell’, ‘o'daniel’]
L(name) names
print(‘#15: #.’.format(name, nysiis(name)))</langsyntaxhighlight>
 
{{out}}
Line 115:
Implementation based on Wikipedia description of the algorithm.
 
<syntaxhighlight lang="c">
<lang c>
#include <iostream> // required for debug code in main() only
#include <iomanip> // required for debug code in main() only
Line 259:
}
 
</syntaxhighlight>
</lang>
{{out|Example output}}
<pre>
Line 300:
Refactored code based on other examples to reduce footprint.
 
<langsyntaxhighlight lang="cos">
Class Utils.Phonetic [ Abstract ]
{
Line 413:
 
}
</syntaxhighlight>
</lang>
{{out|Examples}}
<pre>
Line 459:
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.regex, std.algorithm, std.range, std.string;
 
string replaceAt(in string text, in uint pos, in string[] fromList,
Line 518:
foreach (immutable name; names)
writefln("%11s: %s", name, name.nysiis);
}</langsyntaxhighlight>
{{out}}
<pre> Bishop: BASAP
Line 556:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 696:
fmt.Printf("%-16s : %s\n", name, name2)
}
}</langsyntaxhighlight>
 
{{out}}
Line 746:
=={{header|Java}}==
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import static java.util.Arrays.*;
import static java.lang.System.out;
 
Line 857:
return Vowels.indexOf(c) != -1;
}
}</langsyntaxhighlight>
 
<pre>Carr -> CAR
Line 897:
{{trans|Python}}
 
<langsyntaxhighlight lang="julia">function replaceat(text::AbstractString, position::Int, fromlist, tolist)
for (f, t) in zip(fromlist, tolist)
if startswith(text[position:end], f)
Line 954:
"knight", "mitchell", "o'daniel"]
@printf("%15s: %s\n", name, nysiis(name))
end</langsyntaxhighlight>
 
{{out}}
Line 992:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
val fStrs = listOf("MAC" to "MCC", "KN" to "N", "K" to "C", "PH" to "FF",
Line 1,081:
println("${name.padEnd(16)} : $name2")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,131:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import strutils
 
const
Line 1,211:
if name2.len > 6:
name2 = "$1($2)".format(name2[0..5], name2[6..^1])
echo name1.alignLeft(16), ": ", name2</langsyntaxhighlight>
 
{{out}}
Line 1,259:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">sub no_suffix {
my($name) = @_;
$name =~ s/\h([JS]R)|([IVX]+)$//i;
Line 1,310:
printf "%10s, %s\n", $_, $nysiis;
}
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:35ex"> knight, NAGT
Line 1,348:
=={{header|Phix}}==
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">isVowel</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">byte</span><span style="color: #0000FF;">)</span>
Line 1,499:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"All tests completed, %d errors\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">errors</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
Note: After some careful consideration, I have decided that all three (see note) tests <i>are</i> in fact correct, or at least follow wp, specifically step 6 <i>before</i> step 7.
{{out}}
Line 1,508:
=={{header|Python}}==
A literal translation of the algorithm from the [[wp:New York State Identification and Intelligence System|Wikipedia article]].
<langsyntaxhighlight lang="python">import re
 
_vowels = 'AEIOU'
Line 1,561:
'knight', 'mitchell', "o'daniel"]
for name in names:
print('%15s: %s' % (name, nysiis(name)))</langsyntaxhighlight>
{{out}}
<pre> Bishop: BASAP
Line 1,609:
verbose.
 
<langsyntaxhighlight lang="racket">#lang racket/base
(require racket/string racket/match)
(define (str-rplc-at str replacement start (end (string-length str)))
Line 1,688:
 
(for ((n names) (p py-nysiis-names))
(check-equal? (nysiis n) p (format "(nysiis ~s) = ~s" n p))))</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 1,695:
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. :)
 
<syntaxhighlight lang="raku" perl6line>sub no_suffix ($name) {
$name.uc.subst: /\h (<[JS]>R) | (<[IVX]>+) $/, '';
}
Line 1,745:
}
printf "%10s, %s\n", $_, $nysiis;
}</langsyntaxhighlight>
 
Output:
Line 1,794:
If the rule of only returning (up to) six characters is to be enforced, then the last REXX statement should be
replaced with:
<langsyntaxhighlight lang="rexx">return strip( left(key, 6) ) /*return the leftmost six characters. */</langsyntaxhighlight>
<langsyntaxhighlight lang="rexx">/*REXX program implements the NYSIIS phonetic algorithm (for various test names). */
@@= "Bishop brown_sr browne_III browne_IV Carlson Carr Chapman D'Souza de_Sousa Franklin",
"Greene Harper Hoyle-Johnson Jacobs knight Larson Lawrence Lawson Louis_XVI Lynch",
Line 1,845:
if right(key, 2)=='AY' then key= left(key, length(key) -2)"Y" /* " A in AY*/
if right(key, 1)=='A' then key= left(key, max(1, length(key) -1)) /* " A */
return strip(key) /*return the whole key (all of it). */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,890:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc nysiis {name {truncate false}} {
# Normalize to first word, uppercased, without non-letters
set name [regsub -all {[^A-Z]+} [string toupper [regexp -inline {\S+} $name]] ""]
Line 1,917:
}
return $name
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">foreach name {
knight mitchell "o'daniel" "brown sr" "browne III"
"browne IV" "O'Banion" Mclaughlin McCormack Chapman
Line 1,929:
} {
puts "$name -> [nysiis $name]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,972:
{{libheader|Wren-pattern}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/str" for Str
import "/pattern" for Pattern
import "/fmt" for Fmt
Line 2,065:
if (name2.count > 6) name2 = Fmt.swrite("$s($s)", name2[0..5], name2[6..-1])
Fmt.print("$-16s : $s", name, name2)
}</langsyntaxhighlight>
 
{{out}}
Line 2,115:
=={{header|zkl}}==
{{trans|Python}}
<langsyntaxhighlight lang="zkl">fcn replaceAt(text,pos,fromList,toList){
foreach f,t in (fromList.zip(toList)){
if(0==text[pos,*].find(f)) return(text.set(pos,f.len(),t));
Line 2,150:
}
replaceEnd(key, T("S", "AY", "A"), T("", "Y", ""));
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">names := T("Bishop", "Carlson", "Carr", "Chapman",
"Franklin", "Greene", "Harper", "Jacobs", "Larson", "Lawrence",
"Lawson", "Louis, XVI", "Lynch", "Mackenzie", "Matthews",
Line 2,159:
"knight", "mitchell", "o'daniel");
foreach name in (names){ println("%11s: %s".fmt(name, nysiis(name))) }</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits