Esempio n. 1
0
 protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
 {
     MegaslownikTools.DoTranslate(this, phrase, languagesPair, subject, result, networkSetting);
 }
Esempio n. 2
0
        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);
                    }
                }
            }
        }