Click here to Skip to main content
Click here to Skip to main content

A Snippet that Transforms Spanish Text into Simple American English Phonetics

, 5 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

// 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:

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:

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)

Share

About the Author

B. Clay Shannon
Founder "Across Time & Space"
United States United States
Ideaman and Coder at Across Time & Space, creator of the Windows Store App "Photrax", which can be downloaded as a trial (7 days) from http://apps.microsoft.com/windows/en-us/app/photrax/75c18e6c-96bd-4607-ac43-531aab098ab4
 
Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.
 
I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction: http://www.lulu.com/spotlight/blackbirdcraven
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionNice! PinprofessionalVolynsky Alex7-Jun-14 10:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 5 Jun 2014
Article Copyright 2014 by B. Clay Shannon
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid