Пример #1
0
 public QueryTerms(string phrase)
 {
     Terms = QueryTerms.parse(phrase);
 }
Пример #2
0
        private static List <string> parse(string phrase)
        {
            List <string> terms = new List <string>();

            if (string.IsNullOrEmpty(phrase))
            {
                return(terms);
            }

            phrase = " " + PublicMethods.verify_string(phrase) + " ";

            string pattern = "\\s[\\+\\-]?\"[^\"]*\"\\s";

            Dictionary <string, string> quotesDic = new Dictionary <string, string>();

            MatchCollection matches = null;

            while (true)
            {
                matches = string.IsNullOrEmpty(phrase) ? (MatchCollection)null : (new Regex(pattern)).Matches(phrase);

                if (matches == null || matches.Count == 0)
                {
                    break;
                }

                for (int i = 0; i < matches.Count; i++)
                {
                    string key = PublicMethods.random_string(10);
                    quotesDic[key] = matches[i].Value;
                    phrase         = phrase.Replace(matches[i].Value.Trim(), " " + key + " ");
                }
            }

            phrase.Split(' ')
            .Select(x => x.Trim())
            .Where(x => !string.IsNullOrEmpty(x))
            .Select(x => quotesDic.ContainsKey(x) ? quotesDic[x].Trim() : x)
            .ToList()
            .ForEach(x =>
            {
                string prefix = new[] { "+", "-" }.Where(c => x.StartsWith(c)).FirstOrDefault();

                if (!string.IsNullOrEmpty(prefix))
                {
                    x = x.Length > 1 ? x.Substring(1) : string.Empty;
                }

                if (string.IsNullOrEmpty(x))
                {
                    return;
                }

                bool isQuote = x[0] == '"' && x[x.Length - 1] == '"';

                if (isQuote)
                {
                    if (x.Length <= 2)
                    {
                        return;
                    }
                    x = x.Substring(1, x.Length - 2).Trim();
                }

                new[] {
                    "+", "-", "&", "|", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", ":", "/",
                    "\\", ".", ",", "،", "؛", "؟"
                }.ToList().ForEach(c => x = x.Replace(c, " "));

                x = x.Trim();
                if (string.IsNullOrEmpty(x))
                {
                    return;
                }

                isQuote = isQuote && x.IndexOf(" ") > 0;
                if (isQuote)
                {
                    x = "\"" + x + "\"";
                }

                if (!string.IsNullOrEmpty(prefix))
                {
                    x = prefix + x;
                }

                if (!isQuote && x.IndexOf(" ") > 0)
                {
                    terms.AddRange(QueryTerms.parse(x));
                }
                else
                {
                    if (!isQuote && !(new[] { "?", "*" }).Any(c => x.IndexOf(c) >= 0))
                    {
                        x += "~1";
                    }
                    else if (isQuote)
                    {
                        x += "^";
                    }

                    terms.Add(x);
                }
            });

            return(terms.Distinct().Where(t => !string.IsNullOrEmpty(t)).ToList());
        }