protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting) { Encoding encoding = Encoding.GetEncoding("iso-8859-1"); string query = "http://wordnetweb.princeton.edu/perl/webwn?s=" + HttpUtility.UrlEncode(phrase, encoding); WebRequestHelper helper = new WebRequestHelper(result, new Uri(query), networkSetting, WebRequestContentType.UrlEncodedGet, encoding); string responseFromServer = helper.GetResponse(); if (responseFromServer.Contains("<h3>Your search did not return any results.</h3>")) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } if (responseFromServer.Contains("<h3>Sorry(?) your search can only contain letters(?)")) { throw new TranslationException("Query contains extraneous symbols"); } result.ArticleUrl = query; result.ArticleUrlCaption = phrase; string[] nodes = StringParser.ParseItemsList("<h3>", "</ul>", responseFromServer); bool first = true; string nodename; Result child = result; string[] subnodes; string translation; foreach (string node in nodes) { nodename = StringParser.ExtractLeft("</h3>", node); if (first && nodes.Length == 1) { child.Abbreviation = nodename; } else { child = new Result(result.ServiceItem, nodename, result.LanguagePair, result.Subject); result.Childs.Add(child); } first = false; subnodes = StringParser.ParseItemsList("<li>", "</li>", node); foreach (string subnode in subnodes) { translation = StringParser.RemoveAll("<", ">", subnode); translation = StringParser.ExtractRight(")", translation); child.Translations.Add(translation); } } }
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting) { string query = "http://openthesaurus.caixamagica.pt/suggestions.php?word={0}&search=1"; query = string.Format(CultureInfo.InvariantCulture, query, HttpUtility.UrlEncode(phrase, encoding) ); result.ArticleUrl = query; result.ArticleUrlCaption = phrase; WebRequestHelper helper = new WebRequestHelper(result, new Uri(query), networkSetting, WebRequestContentType.UrlEncodedGet, encoding); string responseFromServer = helper.GetResponse(); string[] translations = StringParser.ParseItemsList("<li>", "</li>", responseFromServer); if (translations.Length == 0) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } string subtranslation; foreach (string translation in translations) { subtranslation = StringParser.RemoveAll("<", ">", translation); result.Translations.Add(subtranslation); } }
public static void SetResult(Result result, string responseFromServer) { string[] translations = StringParser.ParseItemsList("<li class=\"synsetlist\">", "</li>", responseFromServer); if (translations.Length == 0) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } string subtranslation; foreach (string translation in translations) { subtranslation = StringParser.RemoveAll("<", ">", translation); result.Translations.Add(subtranslation); } }
public static void SetAdditionalLinks(Result result, string data, string host) { string[] urls = StringParser.ParseItemsList("<a href=\"", "</a>", data); string link; string text; List <string> texts = new List <string>(); foreach (string url in urls) { link = StringParser.ExtractLeft("\"", url); link = link.Replace(" ", "+"); if (link.StartsWith("/")) { link = "http://" + (new Uri(host)).Host + link; } else { link = host + link; } text = StringParser.ExtractRight(">", url); if (text.EndsWith(" 1")) { text = StringParser.ExtractLeft(" 1", text); } else if (text.EndsWith(" 2")) { text = StringParser.ExtractLeft(" 2", text); } if (!texts.Contains(text)) { result.RelatedLinks.Add(text, new Uri(link)); texts.Add(text); } } }
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting) { string query = "http://dict.tu-chemnitz.de/dings.cgi?lang=en&service={0}&opterrors=0&optpro=0&query={1}&iservice=&dlink=self&comment="; query = string.Format(CultureInfo.InvariantCulture, query, ConvertLanguagesPair(languagesPair), HttpUtility.UrlEncode(phrase, encoding) ); result.ArticleUrl = query; result.ArticleUrlCaption = phrase; WebRequestHelper helper = new WebRequestHelper(result, new Uri(query), networkSetting, WebRequestContentType.UrlEncodedGet); helper.Encoding = encoding; string responseFromServer = helper.GetResponse(); if(responseFromServer.Contains(">0 <span class=\"h\">similar</span> results for ")) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } string[] translations = StringParser.ParseItemsList("<td align=\"right\"><br", "</tr>", responseFromServer); if(translations.Length == 0) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } if(responseFromServer.Contains("<td class=\"tip b \" width=\"45%\" align=\"left\" valign=\"top\">")) { string full_count = StringParser.Parse("<td class=\"tip b \" width=\"45%\" align=\"left\" valign=\"top\">", " ", responseFromServer); if(!string.IsNullOrEmpty(full_count)) { int count; if(int.TryParse(full_count, out count)) result.MoreEntriesCount = count - translations.Length; } } string en_string, ge_string, en_url, ge_url; bool en_audio, ge_audio; Result child = result; string subphrase = ""; foreach(string translation in translations) { string[] subtranslations = StringParser.ParseItemsList("<td class=\"", "</td>", translation); if(subtranslations.Length != 2) throw new TranslationException("Can't found translations in string : " + translation); en_string = StringParser.ExtractRight(">", subtranslations[1]); if(en_string.Contains("<a href=\"")) en_url = StringParser.Parse("<a href=\"", "\"", en_string); else en_url = ""; en_audio = en_string.Contains("href=\"/dings.cgi?speak="); en_string = StringParser.RemoveAll("<", ">", en_string); ge_string = StringParser.ExtractRight(">", subtranslations[0]); if(ge_string.Contains("<a href=\"")) ge_url = StringParser.Parse("<a href=\"", "\"", ge_string); else ge_url = ""; ge_audio = ge_string.Contains("href=\"/dings.cgi?speak="); ge_string = StringParser.RemoveAll("<", ">", ge_string); if(languagesPair.From == Language.German) { if(subphrase != ge_string) { child = new Result(result.ServiceItem, ge_string, result.LanguagePair, result.Subject); subphrase = ge_string; if(!string.IsNullOrEmpty(ge_url)) child.ArticleUrl = "http://dict.tu-chemnitz.de" + ge_url; child.HasAudio = ge_audio; result.Childs.Add(child); } child.Translations.Add(en_string); } else { if(subphrase != en_string) { child = new Result(result.ServiceItem, en_string, result.LanguagePair, result.Subject); subphrase = en_string; if(!string.IsNullOrEmpty(en_url)) child.ArticleUrl = "http://dict.tu-chemnitz.de" + en_url; child.HasAudio = en_audio; result.Childs.Add(child); } child.Translations.Add(ge_string); } } }
public static void DoTranslate(ServiceItem serviceItem, string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting) { bool synonimsDictionary = languagesPair.From == Language.Polish && languagesPair.To == Language.Polish; string query = "http://megaslownik.pl/slownik/{0}/,{1}"; query = string.Format(query, MegaslownikTools.ConvertLanguagePair(languagesPair), HttpUtility.UrlEncode(phrase)); result.ArticleUrl = query; result.ArticleUrlCaption = phrase; WebRequestHelper helper = new WebRequestHelper(result, new Uri(query), networkSetting, WebRequestContentType.UrlEncodedGet); string responseFromServer = helper.GetResponse(); if (responseFromServer.Contains("<div class=\"slowo\">\r\n Szukanego słowa nie ma w MEGAsłowniku.\r\n")) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } result.HasAudio = responseFromServer.Contains("class=\"ikona_sluchaj2\">"); string[] translations = StringParser.ParseItemsList("<div class=\"definicja\">", "</div>", responseFromServer); if (translations.Length == 0) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } string subsubtranslation; string[] subtranslations; foreach (string translation in translations) { subtranslations = StringParser.ParseItemsList("<a href=\"/slownik", "</a>", translation); foreach (string subtranslation in subtranslations) { subsubtranslation = StringParser.ExtractRight(">", subtranslation); subsubtranslation = StringParser.RemoveAll("<", ">", subsubtranslation); result.Translations.Add(subsubtranslation); } } //synonims translations = StringParser.ParseItemsList("<div class=\"synonim\">synonimy:", "</div>", responseFromServer); foreach (string translation in translations) { subtranslations = StringParser.ParseItemsList("<a href=\"/slownik", "</a>", translation); foreach (string subtranslation in subtranslations) { subsubtranslation = StringParser.ExtractRight(">", subtranslation); subsubtranslation = StringParser.RemoveAll("<", ">", subsubtranslation); if (!result.Translations.Contains(subsubtranslation)) { result.Translations.Add(subsubtranslation); } } } //additional links if (!synonimsDictionary) { string[] links = StringParser.ParseItemsList("<li ><a href=\"/slownik/", "</li>", responseFromServer); string linkUrl, linkText, subphrase, subtrans; Result child; foreach (string link in links) { linkUrl = "http://megaslownik.pl/slownik/" + StringParser.ExtractLeft("\"", link); linkText = StringParser.ExtractRight(">", link); linkText = StringParser.RemoveAll("<", ">", linkText); if (linkText.Contains("»") && linkText.Contains(phrase)) { subphrase = StringParser.ExtractLeft("»", linkText); subtrans = StringParser.ExtractRight("»", linkText); child = serviceItem.CreateNewResult(subphrase, languagesPair, subject); result.Childs.Add(child); child.Translations.Add(subtrans); child.ArticleUrl = linkUrl; child.ArticleUrlCaption = subphrase; } } links = StringParser.ParseItemsList("<li><a href=\"/slownik/", "</li>", responseFromServer); foreach (string link in links) { linkUrl = "http://megaslownik.pl/slownik/" + StringParser.ExtractLeft("\"", link); linkText = StringParser.ExtractRight(">", link); linkText = StringParser.RemoveAll("<", ">", linkText); if (linkText.Contains("»") && linkText.Contains(phrase)) { subphrase = StringParser.ExtractLeft("»", linkText); subtrans = StringParser.ExtractRight("»", linkText); child = serviceItem.CreateNewResult(subphrase, languagesPair, subject); result.Childs.Add(child); child.Translations.Add(subtrans); child.ArticleUrl = linkUrl; child.ArticleUrlCaption = subphrase; } //result.RelatedLinks.Add(linkText, linkUrl); } } else { //synonyms string[] links = StringParser.ParseItemsList("<li ><a href=\"/slownik/", "</li>", responseFromServer); string linkUrl, linkText; foreach (string link in links) { linkUrl = "http://megaslownik.pl/slownik/" + StringParser.ExtractLeft("\"", link); linkText = StringParser.ExtractRight(">", link); linkText = StringParser.RemoveAll("<", ">", linkText); if (linkText.Contains(phrase)) { result.RelatedLinks.Add(linkText, linkUrl); } } links = StringParser.ParseItemsList("<li><a href=\"/slownik/", "</li>", responseFromServer); foreach (string link in links) { linkUrl = "http://megaslownik.pl/slownik/" + StringParser.ExtractLeft("\"", link); linkText = StringParser.ExtractRight(">", link); linkText = StringParser.RemoveAll("<", ">", linkText); if (linkText.Contains(phrase)) { result.RelatedLinks.Add(linkText, linkUrl); } } } }
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting) { lock (cookieContainer) { if (coockieTime < DateTime.Now.AddHours(-1)) { coockieTime = DateTime.Now; WebRequestHelper helper_cookie = new WebRequestHelper(result, new Uri("http://multiwordnet.itc.it/online/multiwordnet-head.php"), networkSetting, WebRequestContentType.UrlEncodedGet); helper_cookie.CookieContainer = cookieContainer; helper_cookie.GetResponse(); } } string query = "http://multiwordnet.itc.it/online/mwn-main.php?language={0}&field=word&word={1}&wntype=Overview&pos="; query = string.Format(query, ConvertLanguage(languagesPair.From), HttpUtility.UrlEncode(phrase)); WebRequestHelper helper = new WebRequestHelper(result, new Uri(query), networkSetting, WebRequestContentType.UrlEncodedGet); helper.CookieContainer = cookieContainer; string responseFromServer = helper.GetResponse(); if (responseFromServer.Contains("<b> No data found </b>")) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } string table = StringParser.Parse("<table border=1 WIDTH=100% CELLPADDING=1 class=bgpos>", "</table>", responseFromServer); string[] nodes = StringParser.ParseItemsList("<tr>", "</tr>", table); string nodename, nodeval; Result child = result; foreach (string node in nodes) { if (node.Contains("<td class=bg_posbody COLSPAN=2 >")) { nodename = StringParser.RemoveAll("<", ">", node); child = new Result(result.ServiceItem, nodename, result.LanguagePair, result.Subject); result.Childs.Add(child); } else { nodeval = StringParser.RemoveAll("<", ">", node); child.Translations.Add(nodeval); } } }
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting) { string query = "http://www.dict.cc/?s={0}"; query = string.Format(CultureInfo.InvariantCulture, query, HttpUtility.UrlEncode(phrase, encoding)); result.ArticleUrl = query; result.ArticleUrlCaption = phrase; WebRequestHelper helper = new WebRequestHelper(result, new Uri(query), networkSetting, WebRequestContentType.UrlEncodedGet); helper.Encoding = encoding; string responseFromServer = helper.GetResponse(); if (responseFromServer.Contains("<b>Sorry, no results!</b>")) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } string[] translations = StringParser.ParseItemsList("<tr id='tr", "</tr>", responseFromServer); if (translations.Length == 0) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } if (responseFromServer.Contains("</b> von <b>")) { string full_count = StringParser.Parse("</b> von <b>", "</b>", responseFromServer); if (!string.IsNullOrEmpty(full_count)) { int count; if (int.TryParse(full_count, out count)) { result.MoreEntriesCount = count - translations.Length; } } } string en_string, ge_string, en_url, ge_url; Result child = result; string subphrase = ""; foreach (string translation in translations) { string[] subtranslations = StringParser.ParseItemsList("<td class=td7nl>", "</td>", translation); if (subtranslations.Length != 2) { throw new TranslationException("Can't found translations in string : " + translation); } en_string = subtranslations[0]; if (en_string.Contains("<a href=\"")) { en_url = StringParser.Parse("<a href=\"", "\"", en_string); } else { en_url = ""; } en_string = StringParser.RemoveAll("<", ">", en_string); ge_string = subtranslations[1]; if (ge_string.Contains("<a href=\"")) { ge_url = StringParser.Parse("<a href=\"", "\"", ge_string); } else { ge_url = ""; } ge_string = StringParser.RemoveAll("<", ">", ge_string); if (languagesPair.From == Language.German) { if (subphrase != ge_string) { child = new Result(result.ServiceItem, ge_string, result.LanguagePair, result.Subject); subphrase = ge_string; if (!string.IsNullOrEmpty(ge_url)) { child.ArticleUrl = "http://www.dict.cc" + ge_url; } result.Childs.Add(child); } child.Translations.Add(en_string); } else { if (subphrase != en_string) { child = new Result(result.ServiceItem, en_string, result.LanguagePair, result.Subject); subphrase = en_string; if (!string.IsNullOrEmpty(en_url)) { child.ArticleUrl = "http://www.dict.cc" + en_url; } result.Childs.Add(child); } child.Translations.Add(ge_string); } } }
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting) { WebRequestHelper helper = new WebRequestHelper(result, new Uri("http://www.dicts.info/ud.php"), networkSetting, WebRequestContentType.UrlEncoded); string fromLanguage = ConvertLanguage(languagesPair.From); string toLanguage = ConvertLanguage(languagesPair.To); int fromLangColumn, toLangColumn; string lang_query = "lan1={0}&lan2={1}"; if (string.IsNullOrEmpty(fromLanguage)) { fromLangColumn = 0; toLangColumn = 1; lang_query = string.Format(lang_query, toLanguage, ""); } else if (string.IsNullOrEmpty(toLanguage)) { fromLangColumn = 1; toLangColumn = 0; lang_query = string.Format(lang_query, fromLanguage, ""); } else { fromLangColumn = 1; toLangColumn = 2; lang_query = string.Format(lang_query, fromLanguage, toLanguage); } string query = "{0}&word={1}&rad=ftsearch&go=Search"; query = string.Format(CultureInfo.InvariantCulture, query, lang_query, HttpUtility.UrlEncode(phrase) ); helper.AddPostData(query); string responseFromServer = helper.GetResponse(); if (!responseFromServer.Contains("<table class=\"t1\"")) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } //offcet fromLangColumn += 2; toLangColumn += 2; string translation = StringParser.Parse("<table class=\"t1\"", "</table>", responseFromServer); string[] translations = StringParser.ParseItemsList("<tr bgcolor=\"", "</tr>", translation); if (translations.Length == 0) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } string sub_phrase, sub_translation; Result subres = null; foreach (string subtranslation in translations) { string[] columns = StringParser.ParseItemsList("<td", "</td>", subtranslation); sub_phrase = StringParser.ExtractRight(">", columns[fromLangColumn]).Trim(); sub_phrase = StringParser.RemoveAll("<", ">", sub_phrase); sub_translation = StringParser.ExtractRight(">", columns[toLangColumn]).Trim(); sub_translation = StringParser.RemoveAll("<", ">", sub_translation); if (!string.IsNullOrEmpty(sub_phrase) && !string.IsNullOrEmpty(sub_translation)) { if (subres == null || subres.Phrase != sub_phrase) { subres = CreateNewResult(sub_phrase, languagesPair, subject); result.Childs.Add(subres); } if (!subres.Translations.Contains(sub_translation)) { subres.Translations.Add(sub_translation); } } } if (result.Childs.Count == 0) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } }