Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C# 4.0
Tip/Trick

A Snippet that Transforms Spanish Text into Simple American English Phonetics

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
5 Jun 2014CPOL2 min read 11.8K   2   1
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)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

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, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
QuestionNice! Pin
Volynsky Alex7-Jun-14 9:37
professionalVolynsky Alex7-Jun-14 9:37 

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

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