Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

A Snippet that Transforms Spanish Text into Simple American English Phonetics

5.00/5 (4 votes)
5 Jun 2014CPOL2 min read 12.2K  
A C# snippet that converts Spanish text into a phonetic representation of how to pronounce it (in Mexican Spanish, not Spaniard Spanish) for speakers of American English

Phonetically Morphing Symbols

I have been learning Spanish lately.

People say learning Spanish is easy. I disagree. I am glad that I learned German half my life ago, otherwise I would feel at this point that I'm just not cut out to learn a "foreign" language. I find Spanish at least as difficult to master as German. Perhaps because I am a native English speaker, and English is a Germanic language (not a Latin-based, "romance" language).

At any rate, one of the roadblocks to speaking Spanish correctly is that the pronunciation, while much more internally consistent than English, differs significantly from that of my mother tongue ("h" is always silent, "J" is pronounced as "h", etc.). As an aid to visualize how Spanish text should be pronounced, I wrote a method that receives Spanish text and returns the phonetic equivalent.

The code is this:

C#
// When a vowel is capitalized, it indicates that its "long" sound should be used
private string getPhoneticSpanish(string spanTxt)
{
    String spanishText = spanTxt;
    spanishText = spanishText.Replace("cc", "ks");
    spanishText = spanishText.Replace("ll", "y");
    spanishText = spanishText.Replace("H", string.Empty);
    // Capitalize so that not changed to "k" or "s" later
    spanishText = spanishText.Replace("ch", "CH");
    spanishText = spanishText.Replace("h", string.Empty);
    spanishText = spanishText.Replace("que", "kay");
    //spanishText = spanishText.Replace("a", "ah");
    spanishText = spanishText.Replace("e", "A");
    spanishText = spanishText.Replace(" y ", " E ");
    spanishText = spanishText.Replace("i", "E");
    spanishText = spanishText.Replace("j", "h");
    spanishText = spanishText.Replace("ñ", "ny");
    spanishText = spanishText.Replace("q", "k");
    spanishText = spanishText.Replace("u", "oo");
    spanishText = spanishText.Replace("z", "s");
    // Some notes from http://spanish.stackexchange.com/questions/6883/are-there-consistent-rules-for-pronouncing-c-and-g
    // "C" normally sounds like "k"
    spanishText = spanishText.Replace("c", "k");
    //Before E and I, "C" sounds SSS, like in Celsius, Civil, 
    //so change "ce" and "ci" that were changed above to "ke" and "ki"
    spanishText = spanishText.Replace("ke", "se");
    spanishText = spanishText.Replace("ki", "si");
    // "G" normally is like an English "g"; however, before E and I, like "H"
    spanishText = spanishText.Replace("ge", "h");
    spanishText = spanishText.Replace("gi", "h");
    // "ch" were changed to "CH" to prevent them from being changed; now change them back
    spanishText = spanishText.Replace("CH", "ch");
    return spanishText;
}

...and you can call it like so:

C#
string spanishText = "Hablo mucho. . .";
MessageBox.Show(getPhoneticSpanish(spanishText));

So, if you pass this text (from here):

Huckleberry Finn comienza donde se quedaron las cosas después de Las Aventuras de Tom Sawyer. Huck y Tom Sawyer se habían hecho ricos por todo el tesoro que descubrieron y la Viuda Douglas adoptó a Huck. Ella trata de civilizar a Huck en muchas formas diferentes, incluyendo darle ropa limpia y nueva, enseñándole acerca de Dios y la Biblia, y tratando de educarlo en cosas como la escritura y la lectura. La hermana de la Viuda Douglas, la Señorita Watson, incluso tratar de ayudar. Huck no se sienten cómodo con sus costumbres civilizadas, él sólo quiere estar con sus trapos viejos y fumar.

...this is what is returned:

Image 1

It looks like some sort of haywire Finnish dialect, but it's really American English-ized Mexican Spanish.

This snippet can no doubt be improved; feel free to do so, and post your improvements back here or email them to me at b clay shannon (all one word) at att dot net

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)