示例#1
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);
                }
            }
        }
示例#2
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);
                }
            }
        }
        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");
            }
        }
示例#4
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);
                        }
                    }
                }
            }
        }
        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);
                }
            }
        }