예제 #1
0
        public static IExpression ParseExactPhrase(string expression, ModificationFlags flags)
        {
            if (expression == null)
            {
                return(null);
            }

            expression = expression.Trim();

            if (expression.Trim().Length == 0)
            {
                return(null);
            }

            if (expression.IndexOfAny(TextUtil.WhitespaceChars) != -1)
            {
                // Add quotes to ensure exact phrase
                if (!expression.StartsWith(LiteralTerm.Quote.ToString()))
                {
                    expression = LiteralTerm.Quote + expression;
                }

                if (!expression.EndsWith(LiteralTerm.Quote.ToString()))
                {
                    expression = expression + LiteralTerm.Quote;
                }
            }

            IExpression parsed = new LiteralTerm(expression);

            return(flags == ModificationFlags.None ? parsed : new ModifierExpression(parsed, flags));
        }
예제 #2
0
        private static bool TrySplitSubExpressions(IExpression expression, ref string allWords,
                                                   ref string exactPhrase, ref string anyWord, ref string withoutWords)
        {
            var commutative = expression as CommutativeExpression;

            if (commutative == null || commutative.Operator != BinaryOperator.And)
            {
                return(false);
            }

            // We have some ANDed sub-expressions. See if we can still split them.

            UnaryExpression       not           = null;
            var                   andedLiterals = new List <IExpression>();
            LiteralTerm           exact         = null;
            CommutativeExpression oredLiterals  = null;

            foreach (IExpression commTerm in commutative.Terms)
            {
                IExpression term = commTerm;

                // Is it a literal?

                var literal = term as LiteralTerm;
                if (literal != null)
                {
                    if (exact == null && literal.IsExact)
                    {
                        exact = literal;
                    }
                    else
                    {
                        andedLiterals.Add(literal);
                    }
                    continue;
                }

                // Is it a NOT?

                var unary = term as UnaryExpression;
                if (unary != null)
                {
                    if (not != null || unary.Operator != UnaryOperator.Not)
                    {
                        return(false); // More than one NOT or something other than NOT - cannot be split.
                    }
                    if (unary.Term is LiteralTerm || (unary.Term is CommutativeExpression &&
                                                      ((CommutativeExpression)unary.Term).Operator == BinaryOperator.Or))
                    {
                        not = unary;
                        continue;
                    }

                    return(false);
                }

                var innerAndOr = term as CommutativeExpression;
                if (innerAndOr != null)
                {
                    if (innerAndOr.Operator != BinaryOperator.Or || !AreAllLiterals(innerAndOr.Terms))
                    {
                        return(false); // A sub-expression - cannot be split.
                    }
                    if (oredLiterals != null)
                    {
                        return(false); // More than one OR - cannot be split.
                    }
                    oredLiterals = innerAndOr;
                    continue;
                }

                return(false);
            }

            if (andedLiterals.Count > 0)
            {
                allWords = andedLiterals.Count == 1 ? andedLiterals[0].GetUserExpression() : JoinLiterals(andedLiterals);
            }

            if (not != null)
            {
                if (not.Term is LiteralTerm)
                {
                    withoutWords = not.Term.GetUserExpression();
                }
                else
                {
                    Debug.Assert(not.Term is CommutativeExpression, "not.Term is CommutativeExpression");
                    withoutWords = JoinLiterals(((CommutativeExpression)not.Term).Terms);
                }
            }
            if (exact != null)
            {
                exactPhrase = exact.Value.Trim(LiteralTerm.Quote);
            }

            if (oredLiterals != null)
            {
                anyWord = JoinLiterals(oredLiterals.Terms);
            }

            return(true);
        }