Home > Uncategorized > Translation API without an API Key

Translation API without an API Key

Over the last few days, I’ve been on the hunt for a good unlimited , free translation API. I’ve stumbled upon Glosbe.com, which does not require an API key, although they do mention it may be limited by request per IP to prevent abuse, and I’ll test that out on Monday.

But, as always, here is the C# code required to access this translation API:

First, we get the JSON data from their API as follows:

string strUrl = “http://glosbe.com/gapi/translate?from=eng&dest={1}&format=json&phrase={0}&pretty=true&tm=false”;
strUrl = string.Format(strUrl, word, language);
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
var strJson = wc.DownloadString(strUrl);

The JSON object returned contains much more than just one word, it provides some sample usage, sentences, alternative translations, etc. But I’ll just assume the first translation is valid;

Here is the a sample translation of “cat” into italian

{
  "result" : "ok",
  "authors" : {
    "1" : {
      "U" : "http://en.wiktionary.org",
      "id" : 1,
      "N" : "en.wiktionary.org"
    },
    "86" : {
      "U" : null,
      "id" : 86,
      "N" : "wiki"
    },
    "25018" : {
      "U" : "http://glosbe.com",
      "id" : 25018,
      "N" : "GlosbeResearch"
    },
    "2695" : {
      "U" : "http://dumps.wikimedia.org/enwiktionary/latest/enwiktionary-latest-pages-articles.xml.bz2",
      "id" : 2695,
      "N" : "Wiktionary"
    },
    "2771" : {
      "U" : "http://dizionario.dejudicibus.it/",
      "id" : 2771,
      "N" : "Dizionario-generale-Inglese"
    }
  },
  "dest" : "ita",
  "phrase" : "cat",
  "tuc" : [ {
    "authors" : [ 1 ],
    "meaningId" : -2324632015665027181,
    "meanings" : [ {
      "text" : "domestic species",
      "language" : "eng"
    }, {
      "text" : "Mammifero carnivoro, felino di taglia media dal muso corto, domestico o allo stato selvatico (Felis Silvestris)",
      "language" : "ita"
    }, {
      "text" : "A common four-legged animal (Felis silvestris) that is often kept as a household pet.",
      "language" : "eng"
    } ],
    "phrase" : {
      "text" : "gatto",
      "language" : "ita"
    }
  }, {
    "authors" : [ 1 ],
    "meaningId" : 3792577973885107081,
    "meanings" : [ {
      "text" : "domestic species",
      "language" : "eng"
    } ],
    "phrase" : {
      "text" : "gatta",
      "language" : "ita"
    }
  }, {
    "authors" : [ 1 ],
    "meaningId" : -8655742666001237549,
    "meanings" : [ {
      "text" : "domestic species",
      "language" : "eng"
    }, {
      "text" : "Mammifero carnivoro, felino di taglia media dal muso corto, domestico o allo stato selvatico (Felis Silvestris)",
      "language" : "ita"
    }, {
      "text" : "A common four-legged animal (Felis silvestris) that is often kept as a household pet.",
      "language" : "eng"
    } ],
    "phrase" : {
      "text" : "micio",
      "language" : "ita"
    }
  }, {
    "authors" : [ 1 ],
    "meaningId" : 7605985072771688493,
    "meanings" : [ {
      "text" : "domestic species",
      "language" : "eng"
    } ],
    "phrase" : {
      "text" : "micia",
      "language" : "ita"
    }
  }, {
    "authors" : [ 1 ],
    "meaningId" : 4835673442782652847,
    "meanings" : [ {
      "text" : "member of '''Felidae'''",
      "language" : "eng"
    } ],
    "phrase" : {
      "text" : "felina",
      "language" : "ita"
    }
  }, {
    "authors" : [ 1 ],
    "meaningId" : -797550926637310842,
    "meanings" : [ {
      "text" : "member of '''Felidae'''",
      "language" : "eng"
    } ],
    "phrase" : {
      "text" : "felino",
      "language" : "ita"
    }
  }, {
    "authors" : [ 1 ],
    "meaningId" : -1699184890059243019,
    "meanings" : [ {
      "text" : "raise anchor to cathead",
      "language" : "eng"
    } ],
    "phrase" : {
      "text" : "caponare",
      "language" : "ita"
    }
  }, {
    "authors" : [ 2771 ],
    "meaningId" : 3378167150074910555,
    "phrase" : {
      "text" : "caponare l’ancor",
      "language" : "ita"
    }
  }, {
    "authors" : [ 2771 ],
    "meaningId" : -6368320918367433051,
    "phrase" : {
      "text" : "capone",
      "language" : "ita"
    }
  }, {
    "authors" : [ 2771 ],
    "meaningId" : -6436875142752376744,
    "phrase" : {
      "text" : "doppio tripode",
      "language" : "ita"
    }
  }, {
    "authors" : [ 86 ],
    "meaningId" : 6190890005020929990,
    "phrase" : {
      "text" : "felis silvestris catus",
      "language" : "ita"
    }
  }, {
    "authors" : [ 2771 ],
    "meaningId" : 4594590012605235113,
    "phrase" : {
      "text" : "flagellare",
      "language" : "ita"
    }
  }, {
    "authors" : [ 2771 ],
    "meaningId" : -3077370652934689664,
    "phrase" : {
      "text" : "frustare",
      "language" : "ita"
    }
  }, {
    "authors" : [ 2771 ],
    "meaningId" : 6355132814983896932,
    "phrase" : {
      "text" : "fustigare",
      "language" : "ita"
    }
  }, {
    "authors" : [ 25018 ],
    "meaningId" : -8207633928534718191,
    "phrase" : {
      "text" : "gatto selvatico",
      "language" : "ita"
    }
  }, {
    "authors" : [ 2771 ],
    "meaningId" : 2464893808723829126,
    "phrase" : {
      "text" : "lippa",
      "language" : "ita"
    }
  }, {
    "authors" : [ 2695 ],
    "meaningId" : -599611544161509388,
    "phrase" : {
      "text" : "micio micia",
      "language" : "ita"
    }
  }, {
    "authors" : [ 2771 ],
    "meaningId" : 9131410793497437392,
    "phrase" : {
      "text" : "ragazza maliziosa",
      "language" : "ita"
    }
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "An enthusiast or player of jazz.",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(computing) A ‘catenate’ program and command in Unix that reads one or more files and directs their content to an output device.",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(slang, vulgar, African American Vernacular) A vagina; female external genitalia",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(slang) To vomit something.",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(Irish, informal) terrible, disastrous.",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(archaic, uncountable) The game of "trap and ball" (also called "cat and dog").",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(slang) Any of a variety of earth-moving machines. (from their manufacturer Caterpillar Inc.)",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(nautical) To flog with a cat-o'-nine-tails.",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "vomit",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(archaic) A sturdy merchant sailing vessel (now only in "catboat").",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(slang) Prostitute. [from at least early 15th c.]",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "Any similar animal of the family Felidae, which includes lions, tigers, etc.",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(nautical) Contraction of cat-o'-nine-tails.",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "guy, fellow",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(archaic, uncountable) The trap of the game of "trap and ball".",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(slang) A person (usually male).",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(nautical) To hoist (the anchor) by its ring so that it hangs at the cathead.",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(computing slang) To dump large amounts of data on (an unprepared target) usually with no intention of browsing it carefully.",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(derogatory) A spiteful or angry woman. [from earlier 13th c.]",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "A catfish.",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "A domesticated subspecies (Felis silvestris catus) of feline animal, commonly kept as a house pet. [from 8th c.]",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(nautical) A strong tackle used to hoist an anchor to the cathead of a ship.",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "(computing) To apply the <b>cat</b> command to (a file).",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "strong tackle used to hoist an anchor to the cathead of a ship",
      "language" : "eng"
    } ]
  }, {
    "authors" : [ 1 ],
    "meaningId" : null,
    "meanings" : [ {
      "text" : "A catamaran.",
      "language" : "eng"
    } ]
  } ],
  "from" : "eng"
}

So, using this JSON, I created a C# class, omitting the “Authors” section that didn’t convert well, and I didn’t need anyway;

 

public class Meaning
{
public string text { get; set; }
public string language { get; set; }
}

public class Phrase
{
public string text { get; set; }
public string language { get; set; }
}

public class Tuc
{
public List<int> authors { get; set; }
public object meaningId { get; set; }
public List<Meaning> meanings { get; set; }
public Phrase phrase { get; set; }
}

public class RootObject
{
public string result { get; set; }
public string dest { get; set; }
public string phrase { get; set; }
public List<Tuc> tuc { get; set; }
public string from { get; set; }
}

and finally, de-serializing it, and returning the one word I need;

System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
RootObject ro = js.Deserialize<RootObject>(strJson);
return ro.tuc[0].phrase.text;

 

About these ads
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: