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); } } } }
public static void DoTranslateSentences(ServiceItem serviceItem, string url, string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting) { string query = string.Format(url, HttpUtility.UrlEncode(phrase, Encoding.GetEncoding(1251))); query += ConvertLanguagesPair(languagesPair); WebRequestHelper helper = new WebRequestHelper(result, new Uri(query), networkSetting, WebRequestContentType.UrlEncodedGet, Encoding.GetEncoding(1251)); string responseFromServer = helper.GetResponse(); if (responseFromServer.IndexOf("</form>") < 0) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } else { result.EditArticleUrl = query; string translation = StringParser.Parse("</form>", "<table", responseFromServer); translation = translation.Replace("<p>", "<end><begin>"); translation += "<end>"; StringParser parser = new StringParser(translation); string[] translations = parser.ReadItemsList("<begin>", "<end>"); string subpart; string subphrase = ""; string subtranslation; Result subsubres = null; foreach (string part in translations) { subpart = part; if (subpart.Contains("title=\"")) { subphrase = StringParser.Parse("title=\"", "\"", subpart); } else { subphrase = phrase; } subtranslation = StringParser.RemoveAll("<span", ">", subpart); subtranslation = StringParser.RemoveAll("<a", ">", subtranslation); subtranslation = subtranslation.Replace("</a>", ""); subtranslation = subtranslation.Replace("<i>", ""); subtranslation = subtranslation.Replace("</i>", ""); subtranslation = subtranslation.Replace("</span>", ""); subtranslation = subtranslation.Replace("<sub>", ""); subtranslation = subtranslation.Replace("</sub>", ""); subtranslation = subtranslation.Replace("<b>", ""); subtranslation = subtranslation.Replace("</b>", ""); subtranslation = subtranslation.Replace(">>", ""); subsubres = serviceItem.CreateNewResult(subphrase, languagesPair, subject); result.Childs.Add(subsubres); int idx = subtranslation.IndexOf("\n"); if (idx < 0) { subsubres.Translations.Add("Parse error. Can't found '\\n' in " + subtranslation); } else { subsubres.Translations.Add(subtranslation.Substring(0, idx)); subsubres.Translations.Add(subtranslation.Substring(idx + 1).Trim()); } } } }
public static void DoTranslateDictionary(ServiceItem serviceItem, string url, string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting) { string query = string.Format(url, EncodePhrase(phrase, languagesPair)); query += ConvertLanguagesPair(languagesPair); WebRequestHelper helper = new WebRequestHelper(result, new Uri(query), networkSetting, WebRequestContentType.UrlEncodedGet, GetEncoding(languagesPair)); if (languagesPair.From == Language.Japanese || languagesPair.To == Language.Japanese) { helper.StreamConvertor = new EucJPStreamFixer(); } string responseFromServer = helper.GetResponse(); if ((responseFromServer.IndexOf("ask in forum</a>") >= 0 && responseFromServer.IndexOf(" single words found") < 0) || (responseFromServer.IndexOf("спросить в форуме</a>") >= 0 && responseFromServer.IndexOf(" найдены отдельные слова") < 0) || responseFromServer.IndexOf("<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">") < 0) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } else { string translation = StringParser.Parse("createAutoComplete();", "</table>", responseFromServer); translation = translation.Replace("</TD>", "</td>"); translation = translation.Replace("<TD width", "<td width"); translation = translation.Replace("</td><tr>", "</td></tr><tr>"); translation = translation.Replace("</a><tr>", "</a></td></tr><tr>"); translation = translation.Replace("\"><tr>", "\"></td></tr><tr>"); translation = translation.Replace("</td>\r\n </tr>", "</td></tr>"); StringParser parser = new StringParser(translation); string[] translations = parser.ReadItemsList("<tr>", "</td></tr>"); string subpart; string subphrase = ""; string subtranslation; string abbreviation; Result subres = null; Result subsubres = null; foreach (string part in translations) { subpart = part; if (subpart.StartsWith("<td bgcolor=\"#DBDBDB\" width=\"100%\" colspan=\"2\">")) { //new subres subpart = subpart.Replace("<td bgcolor=\"#DBDBDB\" width=\"100%\" colspan=\"2\">", ""); subphrase = StringParser.Parse("\">", "</a>", subpart); subphrase = StringParser.RemoveAll("<span", ">", subphrase); subphrase = StringParser.RemoveAll("<a", ">", subphrase); subphrase = subphrase.Replace("<FONT SIZE=2>", ""); subphrase = subphrase.Replace("</FONT>", ""); if (subpart.Contains("<em>")) { abbreviation = StringParser.Parse("<em>", "</em>", subpart); } else { abbreviation = ""; } subres = serviceItem.CreateNewResult(subphrase, languagesPair, subject); subres.Abbreviation = abbreviation; subres.ArticleUrl = query; //subres.ArticleUrlCaption = phrase; result.Childs.Add(subres); } else { if (!subpart.Contains("title=\"")) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } abbreviation = StringParser.Parse("title=\"", "\"", subpart); abbreviation += "("; abbreviation += StringParser.Parse("<i>", "</i>", subpart); abbreviation += ")"; //subsubres = serviceItem.CreateNewResult("", languagesPair, subject); //subsubres.Abbreviation = abbreviation; subsubres = serviceItem.CreateNewResult(abbreviation, languagesPair, subject); subsubres.ArticleUrl = "http://www.multitran.ru/c/" + StringParser.Parse("href=\"", "\"", subpart); subres.Childs.Add(subsubres); subtranslation = subpart.Substring(subpart.IndexOf("<td>") + 4); subtranslation = StringParser.RemoveAll("<span", ">", subtranslation); subtranslation = subtranslation.Replace("<a href=\"m.exe?t=", "<end><begin><a"); subtranslation = StringParser.RemoveAll("<a", ">", subtranslation); subtranslation = subtranslation.Replace("</a>", ""); subtranslation = subtranslation.Replace("<i>", ""); subtranslation = subtranslation.Replace("</i>", ""); subtranslation = subtranslation.Replace("</span>", ""); subtranslation = subtranslation.Replace("<sub>", ""); subtranslation = subtranslation.Replace("</sub>", ""); subtranslation = subtranslation.Replace("<b>", ""); subtranslation = subtranslation.Replace("</b>", ""); subtranslation += "<end>"; parser = new StringParser(subtranslation); string[] subtranslations = parser.ReadItemsList("<begin>", "<end>"); foreach (string sub in subtranslations) { subtranslation = sub.Trim(); if (subtranslation.EndsWith(";")) { subtranslation = subtranslation.Substring(0, subtranslation.Length - 1); } subsubres.Translations.Add(subtranslation); } } } } }
public static void DoTranslatePhrases(ServiceItem serviceItem, string url, string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting) { string query = string.Format(url, EncodePhrase(phrase, languagesPair)); query += ConvertLanguagesPair(languagesPair); WebRequestHelper helper = new WebRequestHelper(result, new Uri(query), networkSetting, WebRequestContentType.UrlEncodedGet, GetEncoding(languagesPair)); result.ArticleUrl = query; result.ArticleUrlCaption = phrase; if (languagesPair.From == Language.Japanese || languagesPair.To == Language.Japanese) { helper.StreamConvertor = new EucJPStreamFixer(); } string responseFromServer = helper.GetResponse(); if ((responseFromServer.IndexOf("ask in forum</a>") >= 0 && responseFromServer.IndexOf(" single words found") < 0) || (responseFromServer.IndexOf("спросить в форуме</a>") >= 0 && responseFromServer.IndexOf(" найдены отдельные слова") < 0) || responseFromServer.IndexOf("<table cellspacing=\"0\" border=\"0\" width=\"100%\">") < 0) { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } else { string translation = StringParser.Parse("createAutoComplete();", "<script><!--", responseFromServer); translation = translation.Replace("</TD>", "</td>"); translation = translation.Replace("<TD width", "<td width"); translation = translation.Replace("</td><tr>", "</td></tr><tr>"); translation = translation.Replace("</a><tr>", "</a></td></tr><tr>"); translation = translation.Replace("\"><tr>", "\"></td></tr><tr>"); translation = translation.Replace("</tr></td>", "</td></tr>"); StringParser parser = new StringParser(translation); string[] translations = parser.ReadItemsList("<tr>", "</td></tr>"); string subpart; string subphrase = ""; string subtranslation; string abbreviation; Result subsubres = null; string subres_url; foreach (string part in translations) { if (!part.Contains("<td width=\"5%\">")) { continue; } subpart = part; subphrase = StringParser.Parse("<td width=\"5%\">", "</td>", subpart); subres_url = StringParser.Parse("href=\"", "\"", subphrase); subphrase = StringParser.RemoveAll("<span", ">", subphrase); subphrase = StringParser.RemoveAll("<a", ">", subphrase); subphrase = subphrase.Replace("</a>", ""); subphrase = subphrase.Replace("<i>", ""); subphrase = subphrase.Replace("</i>", ""); subphrase = subphrase.Replace("</span>", ""); subphrase = subphrase.Replace("<sub>", ""); subphrase = subphrase.Replace("</sub>", ""); subphrase = subphrase.Replace("<b>", ""); subphrase = subphrase.Replace("</b>", ""); abbreviation = StringParser.Parse("<i>", "</i>", subpart); subsubres = serviceItem.CreateNewResult(subphrase, languagesPair, subject); subsubres.Abbreviation = abbreviation; subsubres.ArticleUrl = "http://www.multitran.ru/c/" + subres_url; result.Childs.Add(subsubres); subtranslation = subpart + "<end>"; subtranslation = StringParser.Parse("<td width=\"20%\">", "</td>", subtranslation); subtranslation = StringParser.RemoveAll("<span", ">", subtranslation); subtranslation = StringParser.RemoveAll("<a", ">", subtranslation); subtranslation = StringParser.RemoveAll("<td", ">", subtranslation); subtranslation = subtranslation.Replace("</td>", " "); subtranslation = subtranslation.Replace("</a>", ""); subtranslation = subtranslation.Replace("<i>", ""); subtranslation = subtranslation.Replace("</i>", ""); subtranslation = subtranslation.Replace("</span>", ""); subtranslation = subtranslation.Replace("<sub>", ""); subtranslation = subtranslation.Replace("</sub>", ""); subtranslation = subtranslation.Replace("<b>", ""); subtranslation = subtranslation.Replace("</b>", ""); subsubres.Translations.Add(subtranslation); subtranslation = subpart + "<end>"; subtranslation = StringParser.Parse("<td width=\"75%\">", "<end>", subtranslation); subtranslation = StringParser.RemoveAll("<span", ">", subtranslation); subtranslation = StringParser.RemoveAll("<a", ">", subtranslation); subtranslation = StringParser.RemoveAll("<td", ">", subtranslation); subtranslation = subtranslation.Replace("</td>", " "); subtranslation = subtranslation.Replace("</a>", ""); subtranslation = subtranslation.Replace("<i>", ""); subtranslation = subtranslation.Replace("</i>", ""); subtranslation = subtranslation.Replace("</span>", ""); subtranslation = subtranslation.Replace("<sub>", ""); subtranslation = subtranslation.Replace("</sub>", ""); subtranslation = subtranslation.Replace("<b>", ""); subtranslation = subtranslation.Replace("</b>", ""); subsubres.Translations.Add(subtranslation); } } }
public static void DoTranslate(ServiceItem serviceItem, string host, string bookName, string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting) { string query = host + "/{0}"; //ISO-8859-1 query = string.Format(query, HttpUtility.UrlEncode(phrase, System.Text.Encoding.UTF8)); WebRequestHelper helper = new WebRequestHelper(result, new Uri(query), networkSetting, WebRequestContentType.UrlEncodedGet); //helper.UseGoogleCache = true; string responseFromServer = helper.GetResponse(); if (responseFromServer.IndexOf("The word you've entered isn't in the dictionary.") >= 0 || responseFromServer.IndexOf("No entries found.\n<br/>") >= 0 || responseFromServer.IndexOf("The word you've entered isn't in the thesaurus.") >= 0 ) { if (responseFromServer.IndexOf("<PRE>") >= 0) { //suggestions StringParser suggestions_parser = new StringParser("<PRE>", "</PRE>", responseFromServer); string[] suggestions = suggestions_parser.ReadItemsList(">", "</a>"); foreach (string item in suggestions) { string part = item; string link = "html!<p><a href=\"" + host + "/{0}\" title=\"" + host + "/{0}\">{0}</a></p>"; link = string.Format(link, part); result.Translations.Add(link); } return; } else { result.ResultNotFound = true; throw new TranslationException("Nothing found"); } } result.ArticleUrl = host + "/" + phrase; if (responseFromServer.Contains("One entry found.\n<br/>") || responseFromServer.Contains("One entry found.\n<br />")) { SetResult(result, responseFromServer, host + "/"); } else { if (responseFromServer.Contains("'list' value=\"va:")) { string count_str = StringParser.Parse("'list' value=\"va:", ",", responseFromServer); int count; if (int.TryParse(count_str, out count)) { result.MoreEntriesCount = count; } } StringParser parser = new StringParser("<ol class=\"results\"", "</ol>", responseFromServer); string[] items = parser.ReadItemsList("href=\"/" + bookName.ToLower() + "/", "\""); bool first = true; foreach (string item in items) { string part = item; string part_name = StringParser.RemoveAll("[", "]", part); if (string.Compare(part_name, phrase, true) == 0) { Result subres = serviceItem.CreateNewResult(part_name, languagesPair, subject); result.Childs.Add(subres); subres.ArticleUrl = host + "/" + part; if (first) { SetResult(subres, responseFromServer, host + "/"); first = false; } else { //get translation //jump=blood%5B1%2Cnoun%5D&book=Dictionary&quer=blood&list=45%2C1%2C3602592%2C0%3Bblood%5B1%2Cnoun%5D%3D113554%3Bblood%5B2%2Ctransitive+verb%5D%3D113572%3BABO+blood+group%3D2000002431%3Bbad+blood%3D2000074812%3Bblood-and-guts%3D2000113598%3Bblood-brain+barrier%3D2000113627%3Bblood+brother%3D2000113664%3Bblood+cell%3D2000113685%3Bblood+count%3D2000113697%3Bblood+doping%3D2000113725 /*string quer_value = StringParser.Parse("<input type='hidden' name='quer' value=\"", "\"", responseFromServer); * string list_value = StringParser.Parse("<input type='hidden' name='list' value=\"", "\"", responseFromServer); * string post_data_value = "jump={0}&book=" + bookName + "&quer={1}&list={2}"; * post_data_value = string.Format(post_data_value , * HttpUtility.UrlEncode(part), * HttpUtility.UrlEncode(quer_value), * HttpUtility.UrlEncode(list_value) * ); */ helper = new WebRequestHelper(result, new Uri(subres.ArticleUrl), networkSetting, WebRequestContentType.UrlEncodedGet); //helper.AddPostData(post_data_value); //helper.UseGoogleCache = true; responseFromServer = helper.GetResponse(); SetResult(subres, responseFromServer, host + "/"); } } } if (result.MoreEntriesCount != 0) { result.MoreEntriesCount -= result.Childs.Count; } } }