Code Snippet to Convert Word Tense
Here is a useful code snippet to convert the tense of a word from single to plural and vise versa. The code uses a series of regular expression to examine the word and convert it using the correct grammar.
public string MakePlural(string name) { Regex plural1 = new Regex("(?<keep>[^aeiou])y$"); Regex plural2 = new Regex("(?<keep>[aeiou]y)$"); Regex plural3 = new Regex("(?<keep>[sxzh])$"); Regex plural4 = new Regex("(?<keep>[^sxzhy])$");</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(plural1.IsMatch(name)) </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> plural1.Replace(name, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">${keep}ies</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(plural2.IsMatch(name)) </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> plural2.Replace(name, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">${keep}s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(plural3.IsMatch(name)) </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> plural3.Replace(name, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">${keep}es</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(plural4.IsMatch(name)) </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> plural4.Replace(name, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">${keep}s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> name;
}
public string MakeSingle(string name) { Regex single1 = new Regex("(?<keep>[^aeiou])ies$"); Regex single2 = new Regex("(?<keep>[aeiou]y)s$"); Regex single3 = new Regex("(?<keep>[sxzh])es$"); Regex single4 = new Regex("(?<keep>[^sxzhy])s$");</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(single1.IsMatch(name)) </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> single1.Replace(name, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">${keep}y</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(single2.IsMatch(name)) </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> single2.Replace(name, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">${keep}</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(single3.IsMatch(name)) </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> single3.Replace(name, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">${keep}</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(single4.IsMatch(name)) </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> single4.Replace(name, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">${keep}</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> name;
}
~ Paul