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
 
 
 

3 Comments

  • It's &quot;number&quot;, not &quot;tense&quot;. Tense is a verb thing.

    And it's &quot;singular&quot;, not &quot;single&quot;.

    And this is really about spelling, not grammar.

    And this code won't work for some nouns (&quot;passerby&quot; for example). You'd need a dictionary of nonstandard ones in addition to your existing code to really get this right.

  • Thanks Paul. This code works quite well when converting the most common plural words to the singular form.

  • Hi Paul,
    Some words such as gasses, phases, phrases etc are not trimmed correctly. I am trying to improve your code.

Comments have been disabled for this content.