protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
        {
            //http://www.urduword.com/search.php?English=test
            string query = "http://www.urduword.com/search.php?English={0}";

            if (languagesPair.From == Language.Urdu)
            {
                query = "http://www.urduword.com/search.php?Roman={0}";
            }

            query = string.Format(CultureInfo.InvariantCulture, query, HttpUtility.UrlEncode(phrase));

            WebRequestHelper helper =
                new WebRequestHelper(result, new Uri(query),
                                     networkSetting,
                                     WebRequestContentType.UrlEncodedGet);

            string responseFromServer = helper.GetResponse();

            if (responseFromServer.Contains("<p>Could not find translation for <b>"))
            {
                result.ResultNotFound = true;
                throw new TranslationException("Nothing found");
            }
            else
            {
                if (responseFromServer.Contains("<table border=\"0\" width=\"100%\" align=\"center\" cellpadding=\"3\" cellspacing=\"0\" >"))
                {
                    result.ArticleUrl        = query;
                    result.ArticleUrlCaption = phrase;

                    string       translation  = StringParser.Parse("<table border=\"0\" width=\"100%\" align=\"center\" cellpadding=\"3\" cellspacing=\"0\" >", "</table>", responseFromServer);
                    StringParser parser       = new StringParser(translation);
                    string[]     translations = parser.ReadItemsList("<tr>", "</tr>");
                    Result       subres       = null;
                    foreach (string str in translations)
                    {
                        if (str.Contains("class=\"tablehead\""))
                        {
                            continue;
                        }

                        string word = StringParser.Parse(">", "<", StringParser.Parse("<a", "/a>", str));
                        translation = StringParser.Parse("align=\"center\">", "<", str);

                        if (languagesPair.From == Language.Urdu)
                        {
                            if (subres == null || subres.Phrase != translation)
                            {
                                subres = CreateNewResult(translation, languagesPair, subject);
                                result.Childs.Add(subres);
                            }
                        }
                        else
                        {
                            if (subres == null || subres.Phrase != word)
                            {
                                subres = CreateNewResult(word, languagesPair, subject);
                                result.Childs.Add(subres);
                            }
                        }

                        if (languagesPair.From == Language.Urdu)
                        {
                            subres.Translations.Add(word);
                        }
                        else
                        {
                            subres.Translations.Add(translation);
                        }
                    }
                }

                //more
                if (responseFromServer.Contains("page=2\">Next"))
                {
                    query = "http://www.urduword.com/search.php?English={0}&page=2";
                    if (languagesPair.From == Language.Urdu)
                    {
                        query = "http://www.urduword.com/search.php?Roman={0}&page=2";
                    }

                    query = string.Format(CultureInfo.InvariantCulture, query, HttpUtility.UrlEncode(phrase));

                    string link = "html!<p><a href=\"{0}\" title=\"{0}\">{1}</a></p>";
                    link = string.Format(link,
                                         query,
                                         "More ...");
                    Result subres = CreateNewResult(link, languagesPair, subject);
                    result.Childs.Add(subres);
                }

                if (result.Childs.Count == 0)
                {
                    result.ResultNotFound = true;
                    throw new TranslationException("Nothing found");
                }
            }
        }
Exemplo n.º 2
0
        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("&nbsp;single words found") < 0)
                ||
                (responseFromServer.IndexOf("спросить в форуме</a>") >= 0 &&
                 responseFromServer.IndexOf("&nbsp; найдены отдельные слова") < 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);
                }
            }
        }
Exemplo n.º 3
0
        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());
                    }
                }
            }
        }
Exemplo n.º 4
0
        protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
        {
            //http://slovnik.seznam.cz/?q=test&lang=en_cz
            string query = "http://slovnik.seznam.cz/?q={0}&lang={1}";

            query = string.Format(CultureInfo.InvariantCulture, query, HttpUtility.UrlEncode(phrase), ConvertLanguagesPair(languagesPair));

            WebRequestHelper helper =
                new WebRequestHelper(result, new Uri(query),
                                     networkSetting,
                                     WebRequestContentType.UrlEncodedGet);

            string responseFromServer = helper.GetResponse();

            if (responseFromServer.Contains("</strong>&quot; nebylo ve Slovníku nic nalezeno.</h2>"))
            {
                result.ResultNotFound = true;
                throw new TranslationException("Nothing found");
            }
            else
            {
                result.ArticleUrl        = query;
                result.ArticleUrlCaption = phrase;

                //translation by self
                if (responseFromServer.Contains("<table id=\"words\">"))
                {
                    string       translation  = StringParser.Parse("<table id=\"words\">", "</table>", responseFromServer);
                    StringParser parser       = new StringParser(translation);
                    string[]     translations = parser.ReadItemsList("<tr>", "</tr>");
                    Result       subres       = null;
                    foreach (string str in translations)
                    {
                        string word = StringParser.Parse(">", "<", StringParser.Parse("<a", "/a>", str));
                        subres = CreateNewResult(word, languagesPair, subject);
                        result.Childs.Add(subres);

                        StringParser subparser       = new StringParser("<td class=\"translated\">", "</td>", str);
                        string[]     subtranslations = subparser.ReadItemsList("<a", "/a>");
                        foreach (string sub_str in subtranslations)
                        {
                            if (!sub_str.Contains("<img src"))
                            {
                                subres.Translations.Add(StringParser.Parse(">", "<", sub_str));
                            }
                        }
                    }
                }

                //phrases
                if (responseFromServer.Contains("<div id=\"collocations\">"))
                {
                    string       translation  = StringParser.Parse("<div id=\"collocations\">", "</div>", responseFromServer);
                    StringParser parser       = new StringParser(translation);
                    string[]     translations = parser.ReadItemsList("<dt>", "</dd>");
                    Result       subres       = null;
                    foreach (string str in translations)
                    {
                        string word = StringParser.Parse(">", "<", StringParser.Parse("<a", "/a>", str));
                        subres = CreateNewResult(word, languagesPair, subject);
                        result.Childs.Add(subres);

                        StringParser subparser       = new StringParser("<dd>", "</dd>", str + "</dd>");
                        string[]     subtranslations = subparser.ReadItemsList("<a", "/a>");
                        foreach (string sub_str in subtranslations)
                        {
                            subres.Translations.Add(StringParser.Parse(">", "<", sub_str));
                        }
                    }

                    if (responseFromServer.Contains("&amp;from=31\">2</a></span>"))
                    {                     //more phrases
                        string link = "html!<p><a href=\"{0}\" title=\"{0}\">{1}</a></p>";
                        link = string.Format(link,
                                             query + "&from=31",
                                             "More phrases ...");
                        subres = CreateNewResult(link, languagesPair, subject);
                        result.Childs.Add(subres);
                    }
                }

                if (result.Childs.Count == 0)
                {
                    result.ResultNotFound = true;
                    throw new TranslationException("Nothing found");
                }
            }
        }
Exemplo n.º 5
0
        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("&nbsp;single words found") < 0)
                ||
                (responseFromServer.IndexOf("спросить в форуме</a>") >= 0 &&
                 responseFromServer.IndexOf("&nbsp; найдены отдельные слова") < 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);
                        }
                    }
                }
            }
        }
Exemplo n.º 6
0
        protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
        {
            string responseFromServer = UlifHelper.GetAntonymsPage(phrase, networkSetting);

            if (string.IsNullOrEmpty(responseFromServer))
            {
                result.ResultNotFound = true;
                throw new TranslationException("Nothing found");
            }

            responseFromServer = StringParser.Parse("<div class=\"p_cl\">", "</div>", responseFromServer);
            responseFromServer = StringParser.RemoveAll("<a ondblclick", ">", responseFromServer);
            responseFromServer = StringParser.RemoveAll("<font color", ">", responseFromServer);
            responseFromServer = StringParser.RemoveAll("<p align", ">", responseFromServer);


            responseFromServer = responseFromServer.Replace("</a>", "");
            responseFromServer = responseFromServer.Replace("<b>", "");
            responseFromServer = responseFromServer.Replace("</b>", "");
            responseFromServer = responseFromServer.Replace("<i>", "");
            responseFromServer = responseFromServer.Replace("</i>", "");
            responseFromServer = responseFromServer.Replace("◘", "");
            responseFromServer = responseFromServer.Replace("◊", "");
            responseFromServer = responseFromServer.Replace("○", "");
            responseFromServer = responseFromServer.Replace("□", "");
            responseFromServer = responseFromServer.Replace("</font>", "");
            responseFromServer = responseFromServer.Replace("</p>", "");


            StringParser parser = new StringParser(responseFromServer);

            string[] blocks = parser.ReadItemsList("<tr>", "</tr>", "3495783-4572385");

            Result subres;

            string left  = "";
            string right = "";

            if (blocks.Length > 0)
            {
                parser = new StringParser(blocks[0].ToLowerInvariant());
                string[] first_line = parser.ReadItemsList("<td>", "</td>", "3495783-4572385");
                result.Translations.Add(first_line[0] + " - " + first_line[1]);
                left  = first_line[0];
                right = first_line[1];
            }

            for (int i = 1; i < blocks.Length; i++)
            {
                if (blocks[i].Contains("<td>"))
                {
                    subres = CreateNewResult("html!<hr style=\"width: 100%; height: 1px;\">", languagesPair, subject);
                    result.Childs.Add(subres);

                    parser = new StringParser(blocks[i].ToLowerInvariant());
                    string[] second_line = parser.ReadItemsList("<td>", "</td>", "3495783-4572385");

                    subres = CreateNewResult(left, languagesPair, subject);
                    result.Childs.Add(subres);
                    subres.Translations.Add(second_line[0]);

                    subres = CreateNewResult(right, languagesPair, subject);
                    result.Childs.Add(subres);
                    subres.Translations.Add(second_line[1]);

                    subres = CreateNewResult("html!<hr style=\"width: 100%; height: 1px;\">", languagesPair, subject);
                    result.Childs.Add(subres);
                }
                else
                {
                    string line = StringParser.Parse("<td colspan=\"2\">", "</td>", blocks[i]);
                    subres = CreateNewResult("", languagesPair, subject);
                    result.Childs.Add(subres);
                    subres.Translations.Add(line);
                }
            }
        }
Exemplo n.º 7
0
        protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
        {
            //http://rosukrdic.iatp.org.ua/search.php?fullname=%E0%E2%EE%F1%FC
            WebRequestHelper helper =
                new WebRequestHelper(result, new Uri("http://www.rosukrdic.ho.ua/search.php"),
                                     networkSetting,
                                     WebRequestContentType.UrlEncoded);

            helper.Encoding = Encoding.GetEncoding(1251);             //koi8-u
            string query = "fullname={0}";

            query = string.Format(CultureInfo.InvariantCulture, query, HttpUtility.UrlEncode(phrase.ToLowerInvariant(), helper.Encoding));
            helper.AddPostData(query);

            string responseFromServer = helper.GetResponse();

            if (responseFromServer.IndexOf("</b>відсутнє в словнику.") >= 0 ||
                responseFromServer.IndexOf("</b> відсутнє в словнику.") >= 0)
            {
                result.ResultNotFound = true;
                throw new TranslationException("Nothing found");
            }
            else
            {
                string translation = StringParser.Parse("<body>", "</body>", responseFromServer);
                translation = translation.Replace("</u>", "");
                translation = translation.Replace("</span>", "");
                translation = translation.Replace("<span class=\"examples_class\">", "");
                translation = translation.Replace("<em>", "");
                translation = translation.Replace("</em>", "");
                translation = translation.Replace("<p>\n", "<p>");
                translation = translation.Replace("<p>\r\n", "<p>");
                translation = translation.Replace("<h2>", "\n<h2>");
                translation = translation.Replace("</p>", "\n</p>");
                translation = translation.Replace("<span class='examples_class'>", "");



                StringParser phrasesParser = new StringParser(translation);
                string[]     phrases       = phrasesParser.ReadItemsList("<h2>", "</h2>", "787654323");

                StringParser translationsParser = new StringParser(translation);
                string[]     translations       = translationsParser.ReadItemsList("<p", "\n", "787654323");

                string subphrase;
                string subtranslation;
                Result subres = null;
                for (int i = 0; i < phrases.Length; i++)
                {
                    subphrase = phrases[i].Trim();
                    if (subphrase.EndsWith("."))
                    {
                        subphrase = subphrase.Substring(0, subphrase.Length - 1);
                    }
                    subtranslation = translations[i].Substring(1).Trim();
                    subtranslation = subtranslation.Replace("<span class=\"style1\">", "");
                    subtranslation = subtranslation.Replace("lass=\"style1\">", "");
                    subtranslation = subtranslation.Replace("</p>", "");

                    subres = CreateNewResult(subphrase, languagesPair, subject);
                    subres.Translations.Add(subtranslation);
                    result.Childs.Add(subres);
                }
            }
        }
Exemplo n.º 8
0
        protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
        {
            //http://dict.linux.org.ua/dict/db/table_adv.php?word_str=help&expr=any&A=on&P=on&O=on
            WebRequestHelper helper =
                new WebRequestHelper(result, new Uri("http://dict.linux.org.ua/db/table_adv.php"),
                                     networkSetting,
                                     WebRequestContentType.UrlEncoded);
            //helper.Encoding = Encoding.GetEncoding(21866); //koi8-u
            string query = "word={0}&expr=any&A=on&P=on&O=on";

            query = string.Format(CultureInfo.InvariantCulture, query, HttpUtility.UrlEncode(phrase, helper.Encoding));
            helper.AddPostData(query);

            string responseFromServer = helper.GetResponse();

            if (responseFromServer.IndexOf("не знайдено<br>") >= 0)
            {
                result.ResultNotFound = true;
                throw new TranslationException("Nothing found");
            }
            else if (responseFromServer.EndsWith("Рядок пошуку містить недозволені символи."))
            {
                result.ResultNotFound = true;
                throw new TranslationException("Query contains extraneous symbols");
            }
            else if (!responseFromServer.Contains("</html>"))
            {
                result.ResultNotFound = true;
                throw new TranslationException("Nothing found");
            }
            else
            {
                string translation = StringParser.Parse("<table BORDER WIDTH=", "</table>", responseFromServer);
                translation = translation.Replace("<B>", "");
                translation = translation.Replace("</B>", "");

                StringParser parser       = new StringParser(translation);
                string[]     translations = parser.ReadItemsList("<tr>", "</td></tr>", "787654323");

                string subpart;
                string subphrase;
                string subtranslation;
                Result subres = null;
                foreach (string part in translations)
                {
                    subpart = part;
                    subpart = StringParser.RemoveAll("<td width=\"2%\">", "</td><td width=\"40%\">", subpart);
                    subpart = subpart.Replace("<td></td><td>", "");

                    if (subpart.StartsWith("<A HREF"))
                    {
                        subphrase      = StringParser.Parse("\">", "</A>", subpart);
                        subtranslation = StringParser.Parse("\"40%\">", "</td>", subpart);

                        if (translations.Length == 1 && string.Compare(subphrase, phrase, true, CultureInfo.InvariantCulture) == 0)
                        {
                            result.Translations.Add(subtranslation);
                            return;
                        }

                        subres = CreateNewResult(subphrase, languagesPair, subject);
                        subres.Translations.Add(subtranslation);
                        result.Childs.Add(subres);
                    }
                    else if (!subpart.StartsWith(" "))
                    {
                        int idx = subpart.IndexOf("</td>");
                        if (idx < 0)
                        {
                            subphrase      = "Parse Error";
                            subtranslation = "Parse Error";
                        }
                        else
                        {
                            subphrase      = subpart.Substring(0, idx);
                            subpart        = subpart.Substring(idx + 5);
                            subpart        = subpart.Replace("<td width=\"40%\">", "");
                            subtranslation = StringParser.Parse("\">", "</A>", subpart);
                        }

                        if (translations.Length == 1 && string.Compare(subphrase, phrase, true, CultureInfo.InvariantCulture) == 0)
                        {
                            result.Translations.Add(subtranslation);
                            return;
                        }

                        subres = CreateNewResult(subphrase, languagesPair, subject);
                        subres.Translations.Add(subtranslation);
                        result.Childs.Add(subres);
                    }
                    else
                    {
                        subtranslation = StringParser.Parse("\"40%\">", "</td>", subpart);
                        if (subres != null)
                        {
                            subres.Translations.Add(subtranslation);
                        }
                    }
                }
            }
        }
Exemplo n.º 9
0
        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");
            }
        }
Exemplo n.º 10
0
        protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
        {
            string query = "http://slovnyk.org/fcgi-bin/dic.fcgi?iw={0}&hn=pre&il={1}&ol={2}&ul=en-us";

            query = string.Format(CultureInfo.InvariantCulture, query, HttpUtility.UrlEncode(phrase), ConvertLanguage(languagesPair.From), ConvertLanguage(languagesPair.To));
            WebRequestHelper helper =
                new WebRequestHelper(result, new Uri(query),
                                     networkSetting,
                                     WebRequestContentType.UrlEncodedGet);

            string responseFromServer = helper.GetResponse().Trim();

            if (string.IsNullOrEmpty(responseFromServer))
            {
                result.ResultNotFound = true;
                throw new TranslationException("Nothing found");
            }
            else
            {
                string translation = StringParser.Parse("<PRE>", "</PRE>", responseFromServer).Trim();
                if (string.IsNullOrEmpty(translation))
                {
                    result.ResultNotFound = true;
                    throw new TranslationException("Nothing found");
                }

                string subphrase, subtranslation;
                int    startIdx   = 0;
                int    newLineIdx = 0;
                int    tabIdx     = translation.IndexOf('\t', startIdx);
                bool   firstRun   = true;
                Result subres     = result;

                while (tabIdx >= 0)
                {
                    newLineIdx = translation.IndexOf('\n', startIdx);
                    if (newLineIdx < 0)
                    {
                        newLineIdx = translation.Length;
                    }
                    subphrase      = translation.Substring(startIdx, tabIdx - startIdx);
                    subtranslation = translation.Substring(tabIdx + 1, newLineIdx - tabIdx - 1);
                    startIdx       = newLineIdx + 1;
                    if (startIdx < translation.Length)
                    {
                        tabIdx = translation.IndexOf('\t', startIdx);
                    }
                    else
                    {
                        tabIdx = -1;
                    }
                    if (firstRun && tabIdx < 0 && string.Compare(subphrase, phrase, true, CultureInfo.InvariantCulture) == 0)
                    {
                        result.Translations.Add(subtranslation);
                        return;
                    }

                    if (firstRun)
                    {
                        subres = CreateNewResult(subphrase, languagesPair, subject);
                        result.Childs.Add(subres);
                    }

                    firstRun = false;

                    if (string.Compare(subphrase, subres.Phrase, true, CultureInfo.InvariantCulture) != 0)
                    {
                        subres = CreateNewResult(subphrase, languagesPair, subject);
                        result.Childs.Add(subres);
                    }


                    subres.Translations.Add(subtranslation);
                }
            }
        }