Exemplo n.º 1
0
        public void TestComplexSearchExpression()
        {
            Tokens tokens = new Tokens();
            tokens.AddToken(new ParenthesisBeginToken());
            tokens.AddToken(new WordToken("word3"));
            tokens.AddToken(new OrToken());
            tokens.AddToken(new WordToken("word10"));
            tokens.AddToken(new OrToken());
            tokens.AddToken(new WordToken("word9"));
            tokens.AddToken(new ParenthesisEndToken());

            tokens.AddToken(new AndToken());

            tokens.AddToken(new ParenthesisBeginToken());
            tokens.AddToken(new WordToken("word2"));
            tokens.AddToken(new OrToken());
            tokens.AddToken(new WordToken("word6"));
            tokens.AddToken(new ParenthesisEndToken());

            tokens.AddToken(new AndNotToken());

            tokens.AddToken(new ParenthesisBeginToken());
            tokens.AddToken(new WordToken("word12"));
            tokens.AddToken(new ParenthesisEndToken());

            Parser parse = new Parser(tokens);
            Node root = parse.BinaryTree.root;

            Evaluater eval = new Evaluater(root, index);
            List<string> res = eval.result.ToList<string>();
            Assert.IsNotNull(res);
            Assert.Contains("doc1", res);
            Assert.AreEqual(1, res.Count);
        }
Exemplo n.º 2
0
 public void testTokens()
 {
     string queryExpression = "(\"good men\" OR (evil OR mean)) AND (man AND woman)";
     _Tokenizer = new Lexer(queryExpression);
     _Tokenizer.Tokenize();
     _tokens = _Tokenizer.tokens;
     Assert.AreEqual(_tokens.getNextToken().ToString(), "(");
     Assert.AreEqual(((WordToken)_tokens.getNextToken()).ToString(), "good men");
     Assert.AreEqual(_tokens.getNextToken().ToString(), "OR");
 }
Exemplo n.º 3
0
        public Parser(Tokens tokens)
        {
            branchesStack = new Stack<Node>();
            BinaryTree = new BinaryTreeImp();
            _tokens = tokens;

            next = _tokens.PeekToken();
            if (next.ToString() == "(")
                _query();
            else //NOTE: if the first token is not a parenthesis then all tokens are treated as word tokens, not logic tokens or others
                _buildTreeWordsOnly();
        }
Exemplo n.º 4
0
        public void TestParserSimpleLogicExpression()
        {
            Tokens tokens = new Tokens();
            tokens.AddToken(new ParenthesisBeginToken());
            tokens.AddToken(new WordToken("w1"));
            tokens.AddToken(new OrToken());
            tokens.AddToken(new WordToken("w2"));
            tokens.AddToken(new ParenthesisEndToken());

            _parser = new Parser(tokens);
            Node treeRoot = _parser.BinaryTree.root;
            Assert.AreEqual(3, _parser.BinaryTree.count);
        }
Exemplo n.º 5
0
        public void TestAnd()
        {
            Tokens tokens = new Tokens();
            tokens.AddToken(new ParenthesisBeginToken());
            tokens.AddToken(new WordToken("word6"));
            tokens.AddToken(new AndToken());
            tokens.AddToken(new WordToken("word12"));
            tokens.AddToken(new ParenthesisEndToken());

            Parser parse = new Parser(tokens);
            Node root = parse.BinaryTree.root;

            Evaluater eval = new Evaluater(root, index);
            List<string> res = eval.result.ToList<string>();
            Assert.IsNotNull(res);
            Assert.Contains("doc6", res);
            Assert.Contains("doc3", res);
            Assert.AreEqual(2, res.Count);
        }
Exemplo n.º 6
0
        public void TestParserComplexLogicExpression()
        {
            Tokens tokens = new Tokens();
            tokens.AddToken(new ParenthesisBeginToken());
            tokens.AddToken(new WordToken("good men"));
            tokens.AddToken(new OrToken());
            tokens.AddToken(new ParenthesisBeginToken());
            tokens.AddToken(new WordToken("evil"));
            tokens.AddToken(new OrToken());
            tokens.AddToken(new WordToken("bad"));
            tokens.AddToken(new ParenthesisEndToken());
            tokens.AddToken(new ParenthesisEndToken());
            tokens.AddToken(new AndToken());
            tokens.AddToken(new ParenthesisBeginToken());
            tokens.AddToken(new WordToken("women"));
            tokens.AddToken(new AndToken());
            tokens.AddToken(new WordToken("children"));
            tokens.AddToken(new ParenthesisEndToken());

            _parser = new Parser(tokens);
            Node treeRoot = _parser.BinaryTree.root;
            Assert.AreEqual(9, _parser.BinaryTree.count);
        }
Exemplo n.º 7
0
 public Lexer(string queryExpression)
 {
     tokens  = new Tokens();
     _reader = new StringReader(queryExpression);
 }
Exemplo n.º 8
0
 public Lexer(string queryExpression)
 {
     tokens = new Tokens();
     _reader = new StringReader(queryExpression);
 }
Exemplo n.º 9
0
        public void TestParserSingleWordOnly()
        {
            Tokens tokens = new Tokens();
            tokens.AddToken(new WordToken("good men"));

            _parser = new Parser(tokens);
            Node treeRoot = _parser.BinaryTree.root;
            Assert.AreEqual(1, _parser.BinaryTree.count);
        }
Exemplo n.º 10
0
        public void TestParserWordsOnlyNoLogic()
        {
            Tokens tokens = new Tokens();
            tokens.AddToken(new WordToken("w1"));
            tokens.AddToken(new WordToken("w2"));
            tokens.AddToken(new WordToken("w3"));
            tokens.AddToken(new WordToken("w4"));
            tokens.AddToken(new WordToken("w5"));
            tokens.AddToken(new WordToken("w6"));
            tokens.AddToken(new WordToken("w7"));

            _parser = new Parser(tokens);
            Node treeRoot = _parser.BinaryTree.root;
            Assert.AreEqual(13, _parser.BinaryTree.count);
        }
Exemplo n.º 11
0
        public void TestOr()
        {
            Tokens tokens = new Tokens();
            tokens.AddToken(new WordToken("word1"));
            tokens.AddToken(new WordToken("word2"));
            tokens.AddToken(new WordToken("word4"));
            tokens.AddToken(new WordToken("word6"));
            tokens.AddToken(new WordToken("word8"));

            Parser parse = new Parser(tokens);
            Node root = parse.BinaryTree.root;

            Evaluater eval = new Evaluater(root, index);
            List<string> res = eval.result.ToList<string>();
            Assert.IsNotNull(res);
            Assert.Contains("doc1", res);
            Assert.Contains("doc2", res);
            Assert.Contains("doc3", res);
            Assert.Contains("doc4", res);
            Assert.Contains("doc6", res);
            Assert.AreEqual(5, res.Count);
            Assert.AreNotEqual(2, res.Count);
        }