using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Text; namespace PhonKey { /// /// New York State Identification and Intelligence System (NYSIIS) Phonetic Encoder /// public class PhonKey : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox inName; private System.Windows.Forms.TextBox outPhonKey; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public PhonKey() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.inName = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.outPhonKey = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // inName // this.inName.Location = new System.Drawing.Point(96, 32); this.inName.MaxLength = 200; this.inName.Name = "inName"; this.inName.Size = new System.Drawing.Size(208, 20); this.inName.TabIndex = 0; this.inName.Text = ""; // // button1 // this.button1.Location = new System.Drawing.Point(136, 88); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(112, 24); this.button1.TabIndex = 1; this.button1.Text = "Generate Code"; this.button1.Click += new System.EventHandler(this.button1_Click); // // outPhonKey // this.outPhonKey.Location = new System.Drawing.Point(96, 152); this.outPhonKey.Name = "outPhonKey"; this.outPhonKey.ReadOnly = true; this.outPhonKey.Size = new System.Drawing.Size(208, 20); this.outPhonKey.TabIndex = 2; this.outPhonKey.Text = ""; // // label1 // this.label1.Location = new System.Drawing.Point(16, 40); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(48, 24); this.label1.TabIndex = 3; this.label1.Text = "Name:"; // // label2 // this.label2.Anchor = System.Windows.Forms.AnchorStyles.Top; this.label2.Location = new System.Drawing.Point(16, 160); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(72, 24); this.label2.TabIndex = 4; this.label2.Text = "NYSIIS code:"; // // PhonKey // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(328, 213); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label2, this.label1, this.outPhonKey, this.button1, this.inName}); this.Name = "PhonKey"; this.Text = "NYSIIS code"; this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new PhonKey()); } private void button1_Click(object sender, System.EventArgs e) { string name = inName.Text.ToString(); int inNameCharOffset = 0; int stopSearchforSZ = 0; int maxLength = 200; int prevCharValue = 0; char firstChar = ' '; char[] inNameArray = new char[maxLength]; char[] phonKeyArray = new char[maxLength]; bool prependVowel = false; StringBuilder MyStringBuilder = new StringBuilder(" "); name = name.ToUpper(); name.CopyTo(0, inNameArray, 0, name.Length); // copy inName to char array firstChar = inNameArray[0]; // remove all 'S' and 'Z' chars from the end of the surname for(inNameCharOffset = (name.Length - 1); ((inNameCharOffset >= 1) && (stopSearchforSZ == 0)); inNameCharOffset--) { if(inNameArray[inNameCharOffset] == 'S' || inNameArray[inNameCharOffset] == 'Z') { inNameArray[inNameCharOffset] = ' '; } else { stopSearchforSZ = 1; } } name = new String(inNameArray, 0, name.Length); // copy char array to string MyStringBuilder = new StringBuilder(name); MyStringBuilder.Replace(" ", ""); name = String.Format(MyStringBuilder + ""); MyStringBuilder = new StringBuilder(name); // transcode initial strings MAC => MC if(MyStringBuilder.Length > 2) { MyStringBuilder.Replace("MAC", "MC", 0, 3); } // transcode initial strings PF => F if(MyStringBuilder.Length > 1) { MyStringBuilder.Replace("PF", "F", 0, 2); } // Transcode trailing strings as follows, IX => IC // EX => EC // YE,EE,IE => Y // NT,ND => D if(MyStringBuilder.Length > 1) { MyStringBuilder.Replace("IX", "IC", (MyStringBuilder.Length - 2), 2); MyStringBuilder.Replace("EX", "EC", (MyStringBuilder.Length - 2), 2); MyStringBuilder.Replace("YE", "Y", (MyStringBuilder.Length - 2), 2); MyStringBuilder.Replace("EE", "Y", (MyStringBuilder.Length - 2), 2); MyStringBuilder.Replace("IE", "Y", (MyStringBuilder.Length - 2), 2); MyStringBuilder.Replace("NT", "D", (MyStringBuilder.Length - 2), 2); MyStringBuilder.Replace("ND", "D", (MyStringBuilder.Length - 2), 2); } // transcode 'EV' to 'EF' if not at start of name if(MyStringBuilder.Length > 1) { MyStringBuilder.Replace("EV", "EF", 1, (MyStringBuilder.Length - 1)); } // remove any 'W' that follows a vowel MyStringBuilder.Replace("AW", "A"); MyStringBuilder.Replace("EW", "E"); MyStringBuilder.Replace("IW", "I"); MyStringBuilder.Replace("OW", "O"); MyStringBuilder.Replace("UW", "U"); // replace all vowels with 'A' MyStringBuilder.Replace("A", "A"); MyStringBuilder.Replace("E", "A"); MyStringBuilder.Replace("I", "A"); MyStringBuilder.Replace("O", "A"); MyStringBuilder.Replace("U", "A"); // transcode 'GHT' to 'GT' MyStringBuilder.Replace("GHT", "GT"); // transcode 'DG' to 'G' MyStringBuilder.Replace("DG", "G"); // transcode 'PH' to 'F' MyStringBuilder.Replace("PH", "F"); // if not first character, eliminate all 'H' preceded or followed by a vowel MyStringBuilder.Replace("AH", "A"); if(MyStringBuilder.Length > 2) { MyStringBuilder.Replace("HA", "A", 1, (MyStringBuilder.Length - 1)); } // change 'KN' to 'N', else 'K' to 'C' MyStringBuilder.Replace("KN", "N"); MyStringBuilder.Replace("K", "C"); // if not first character, change 'M' to 'N' if(MyStringBuilder.Length > 1) { MyStringBuilder.Replace("M", "N", 1, (MyStringBuilder.Length - 1)); } // if not first character, change 'Q' to 'G' if(MyStringBuilder.Length > 1) { MyStringBuilder.Replace("Q", "G", 1, (MyStringBuilder.Length - 1)); } // transcode 'SH' to 'S' MyStringBuilder.Replace("SH", "S"); // transcode 'SCH' to 'S' MyStringBuilder.Replace("SCH", "S"); // transcode 'YW' to 'Y' MyStringBuilder.Replace("YW", "Y"); // if not first or last character, change 'Y' to 'A' if(MyStringBuilder.Length > 2) { MyStringBuilder.Replace("Y", "A", 1, (MyStringBuilder.Length - 2)); } // transcode 'WR' to 'R' MyStringBuilder.Replace("WR", "R"); // if not first character, change 'Z' to 'S' if(MyStringBuilder.Length > 1) { MyStringBuilder.Replace("Z", "S", 1, (MyStringBuilder.Length - 1)); } // transcode terminal 'AY' to 'Y' if(MyStringBuilder.Length > 2) { MyStringBuilder.Replace("AY", "Y", 1, (MyStringBuilder.Length - 1)); } name = String.Format(MyStringBuilder + ""); name.CopyTo(0, inNameArray, 0, name.Length); // copy inName to char array stopSearchforSZ = 0; // remove traling vowels for(inNameCharOffset = (name.Length - 1); ((inNameCharOffset >= 1) && (stopSearchforSZ == 0)); inNameCharOffset--) { if(inNameArray[inNameCharOffset] == 'A') { inNameArray[inNameCharOffset] = ' '; } else { stopSearchforSZ = 1; } } name = new String(inNameArray, 0, MyStringBuilder.Length); // copy char array to string MyStringBuilder = new StringBuilder(name); MyStringBuilder.Replace(" ", ""); name = String.Format(MyStringBuilder + ""); name.CopyTo(0, inNameArray, 0, name.Length); // copy inName to char array // collapse all strings of repeated characters // (also removes non-alpha chars) for(inNameCharOffset = 0; inNameCharOffset < name.Length; inNameCharOffset++) { if(prevCharValue == inNameArray[inNameCharOffset] || inNameArray[inNameCharOffset] < 'A' || inNameArray[inNameCharOffset] > 'Z') { inNameArray[inNameCharOffset] = ' '; } else { prevCharValue = inNameArray[inNameCharOffset]; } } // if first char of original surname is a vowel, prepend to code (or replace initial transcoded 'A') if(firstChar == 'A' || firstChar == 'E' || firstChar == 'I' || firstChar == 'O' || firstChar == 'U') { if(inNameArray[0] == 'A') { inNameArray[0] = firstChar; } else { prependVowel = true; } } name = new String(inNameArray, 0, name.Length); // copy char array to string MyStringBuilder = new StringBuilder(name); MyStringBuilder.Replace(" ", ""); if(prependVowel) { name = String.Format(firstChar.ToString() + MyStringBuilder + ""); } else { name = String.Format(MyStringBuilder + ""); } this.outPhonKey.Text = name; } } }