Пример #1
0
        public override string[] SplitPhraseToSubqueries(string phrase)
        {
            List <string> words  = StringParser.SplitToParts(phrase);
            List <string> result = new List <string>();

            for (int i = 0; i < MaxCountOfSubQueries; i++)
            {
                if (i * WordsLimit >= words.Count)
                {
                    break;
                }

                string subres = "";
                for (int sub_idx = 0; sub_idx < WordsLimit; sub_idx++)
                {
                    if (i * WordsLimit + sub_idx >= words.Count)
                    {
                        if (!string.IsNullOrEmpty(subres))
                        {
                            result.Add(subres);
                        }
                        break;
                    }
                    subres += words[i * WordsLimit + sub_idx] + " ";
                }
            }

            return(result.ToArray());
        }
Пример #2
0
        string BuildQuery(string phrase, ReadOnlyServiceSettingCollection settings, ReadOnlyResultCollection results)
        {
            if (phrase == null || settings == null)
            {
                return("");
            }
            else
            {
                string result;

                if (wBrowser.Width > 740)
                {
                    result = "w=728&h=90";
                }
                else if (wBrowser.Width > 480)
                {
                    result = "w=468&h=60";
                }
                else if (wBrowser.Width > 246)
                {
                    result = "w=234&h=60";
                }
                else if (wBrowser.Width > 137)
                {
                    result = "w=125&h=125";
                }
                else
                {
                    return(null);
                }

                //selecting words from query and result
                List <string> phrasewords = StringParser.SplitToParts(phrase.Replace("́", ""));
                int           count       = ExtractAdWords(phrasewords, 25);

                string resultstr = "";
                foreach (Result r in results)
                {
                    resultstr += GetResultString(r);
                }

                List <string> resultwords = StringParser.SplitToParts(resultstr.Replace("́", ""));
                count = ExtractAdWords(resultwords, 60 - count);

                if (phrasewords.Count + resultwords.Count == 0)
                {
                    return(null);
                }

                string phraseStr = "";
                foreach (string s in resultwords)
                {
                    phraseStr += s + " ";
                }

                foreach (string s in phrasewords)
                {
                    if (!resultwords.Contains(s))
                    {
                        phraseStr += s + " ";
                    }
                }

                phraseStr = phraseStr.Substring(0, phraseStr.Length - 1);

                result += "&s=" + HttpUtility.UrlEncode(phraseStr);

                List <string> subjects = new List <string>();
                foreach (ServiceItemSetting ss in settings)
                {
                    if (ss.Subject != SubjectConstants.Common && !subjects.Contains(ss.Subject))
                    {
                        subjects.Add(ss.Subject);
                    }
                }

                int idx = 0;
                foreach (string sub in subjects)
                {
                    result += "&sub" + idx.ToString(CultureInfo.InvariantCulture) + "=" + HttpUtility.UrlEncode(sub);
                    idx++;
                }
                return(result);
            }
        }
Пример #3
0
        string GetResultHtml(Result result, double indent)
        {
            StringBuilder htmlString = new StringBuilder();

            if (result.Error == null || result.ResultNotFound)
            {
                if (!string.IsNullOrEmpty(result.EditArticleUrl))
                {
                    string link_f = "<a href=\"{0}\" title=\"{0}\">{1} \"{2}\"</a><br><br>";
                    htmlString.AppendFormat(link_f, result.EditArticleUrl,
                                            result.ResultNotFound ?
                                            LangPack.TranslateString("Create article") :
                                            LangPack.TranslateString("Open article"),
                                            result.Phrase);
                }
            }

            if (result.Error == null)
            {
                if (result.Childs.Count != 0)
                {
                    //we has childs list
                    if (result.Translations.Count > 0 && !string.IsNullOrEmpty(result.Translations[0]))
                    {
                        htmlString.AppendFormat("<b>{0}</b>  ",
                                                HttpUtility.HtmlEncode(result.Translations[0]));
                    }

                    if (!string.IsNullOrEmpty(result.Abbreviation))
                    {
                        htmlString.Append(" " + HttpUtility.HtmlEncode(result.Abbreviation) + " ");
                    }

                    if (result.Translations.Count > 0 || !string.IsNullOrEmpty(result.Abbreviation))
                    {
                        htmlString.Append("<br>");
                    }

                    foreach (Result child in result.Childs)
                    {
                        if (/*r.Phrase != result.Phrase && */ !string.IsNullOrEmpty(child.Phrase))
                        {
                            if (!child.Phrase.StartsWith("html!"))
                            {
                                if (indent != 0)
                                {
                                    htmlString.Append(GetParagraphFormat(indent, result));
                                }

                                if (string.IsNullOrEmpty(child.ArticleUrl))
                                {
                                    htmlString.AppendFormat("<b>{0}</b> ",
                                                            HttpUtility.HtmlEncode(child.Phrase.Replace('\u00A0', ' ')));
                                }
                                else
                                {                                 //integrate url
                                    string icon = "";
                                    if (child.HasAudio)
                                    {
                                        icon = " " + string.Format(HtmlHelper.IconOfAudioFormat,
                                                                   LangPack.TranslateString("Pronunciation"));
                                    }
                                    htmlString.AppendFormat("<a href=\"{0}\" title=\"{0}\"><b>{1}</b>{2}</a> ",
                                                            child.ArticleUrl,
                                                            HttpUtility.HtmlEncode(child.Phrase.Replace('\u00A0', ' ')),
                                                            icon);
                                }
                                if (indent != 0)
                                {
                                    htmlString.Append("</p>");
                                }
                            }
                            else
                            {                              //append html directly
                                htmlString.Append(child.Phrase.Substring(5));
                            }
                        }

                        if (!string.IsNullOrEmpty(child.Abbreviation) && child.Childs.Count == 0)
                        {
                            htmlString.Append(" " + HttpUtility.HtmlEncode(child.Abbreviation) + " ");
                        }

                        if (/*r.Phrase != result.Phrase && */ indent > 0.5 && (!string.IsNullOrEmpty(child.Phrase) || !string.IsNullOrEmpty(child.Abbreviation)))
                        {
                            htmlString.Append("<br>");
                        }

                        htmlString.AppendFormat("{0}",
                                                GetResultHtml(child, indent + 0.5));
                    }
                    return(htmlString.ToString());
                }

                if (result.Parent == null)
                {                 //abreviations
                    if (!string.IsNullOrEmpty(result.Abbreviation))
                    {
                        htmlString.Append(" " + HttpUtility.HtmlEncode(result.Abbreviation) + " ");
                    }

                    if (result.Translations.Count > 0 || !string.IsNullOrEmpty(result.Abbreviation))
                    {
                        htmlString.Append("<br>");
                    }
                }
                string topPhrase;
                if (result.Parent != null)
                {
                    topPhrase = result.Parent.Phrase;
                }
                else
                {
                    topPhrase = result.Phrase;
                }
                topPhrase = topPhrase.ToLowerInvariant();

                foreach (string s in result.Translations)
                {
                    if (indent > 0)
                    {
                        htmlString.Append("<li>");
                    }

                    if (!s.StartsWith("html!"))
                    {
                        htmlString.Append(GetParagraphFormat(indent, result));

                        List <string> words = StringParser.SplitToParts(s);

                        foreach (string word in words)
                        {
                            if (string.IsNullOrEmpty(word))
                            {
                                continue;
                            }

                            bool IsDelimiter = false;
                            bool IsError     = word.Length > 1;
                            if (!IsError && word.Length == 1)
                            {
                                IsError     = StringParser.DelimiterCharsList.BinarySearch(word[0]) < 0;
                                IsDelimiter = true;
                            }
                            if (IsError)
                            {
                                IsError = topPhrase.IndexOf(word.ToLowerInvariant()) >= 0;
                            }

                            if (!IsDelimiter)
                            {
                                if (IsError)
                                {
                                    htmlString.AppendFormat("<span style=\"" + HtmlHelper.ErrorTextStyle + "\">{0}</span>", HttpUtility.HtmlEncode(word));
                                }
                                else
                                {
                                    htmlString.Append(HttpUtility.HtmlEncode(word));
                                }
                            }
                            else if (word[0] == '\u00A0')
                            {
                                htmlString.Append(' ');                                 //avoid adding &nbsp;
                            }
                            else if (word[0] == '\n')
                            {
                                htmlString.Append("<br />");
                            }
                            else
                            {
                                htmlString.Append(HttpUtility.HtmlEncode(word));
                            }
                        }
                        htmlString.Append("</p>");
                    }
                    else
                    {                     //append html directly
                        int allowedWidth = (int)((double)wBrowser.Width * .95) - 26;
                        if (vScrollBar.Visible)
                        {
                            allowedWidth += SystemInformation.VerticalScrollBarWidth;
                        }

                        htmlString.Append(s.Substring(5).Replace("{allowed_width}", allowedWidth.ToString()));
                    }

                    if (indent > 0)
                    {
                        htmlString.Append("</li>");
                    }
                }

                //Additional links
                if (result.RelatedLinks.Count > 0)
                {
                    if (indent > 0)
                    {
                        htmlString.Append("<li>");
                    }

                    htmlString.Append(GetParagraphFormat(indent, result));
                    htmlString.Append(LangPack.TranslateString("related links") + " : ");
                    bool first = true;
                    foreach (Link lnk in result.RelatedLinks)
                    {
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            htmlString.Append(", ");
                        }
                        htmlString.AppendFormat("<a href=\"{0}\" title=\"{0}\">{1}</a>",
                                                lnk.Uri,
                                                HttpUtility.HtmlEncode(lnk.Text.Replace('\u00A0', ' ')));
                    }

                    htmlString.Append("</p>");
                    if (indent > 0)
                    {
                        htmlString.Append("</li>");
                    }
                }
            }
            else
            {
                htmlString.Append(HttpUtility.HtmlEncode(LangPack.TranslateString(result.Error.Message)));
                if (!result.ResultNotFound)
                {
                    string trackMessage = string.Format("Error on translating phrase : {0}, direction : {1}, subject : {2}, service : {3}", result.Phrase, result.LanguagePair.ToString(), result.Subject, result.ServiceItem.FullName);
                    AnalyticsMonitor.OnThreadException(result.Error, trackMessage);
                    Trace.TraceException(result.Error);
                }
            }
            return(htmlString.ToString());
        }