Introduction
This application as the title says allows text searching based on string expressions built using boolean operators.
Background
I was unable to find any native libraries in .NET for evaluating boolean expressions. Not sure if regular expression library supports evaluation of boolean expressions.
This code matches user supplied keywords against a block of text in its simplest form. However, users can build complex keyword expressions to match using operators like AND
, OR
and NOT
. Users can also override precedence of operators using parenthesis. Operator precedence is very similar to the precedence rules commonly found in all programming languages. NOT
operator precedes AND
operator which in tun precedes OR
operator.
Expression evaluation logic is simple. First, the infix expression is converted to postfix followed by the actual evaluation.
Sample Text :- Hello, World!
Input infix expression :- Hello AND World!
Equivalent postfix expression :- Hello World! AND
Result :- True
The code does not use any Recursion for postfix conversion as well as for the final evaluation. Postfix conversion uses the C# stack container to arrange keywords and operators based on precedence. Expression evaluation logic simply runs through this converted postfix expression and evaluates the keyword based on operator type - binary vs unary. Again, C# stack container is used here for easier evaluation.
Methods of interest:
ConvertToPostFix()
EvaluateExpression()
You may override operators or nest any number of expressions using ( and ) parenthesis.
Using the Code
The application starts up with a test text message string and few expressions. You may go ahead and hit evaluate to view the equivalent postfix strings and the evaluation results. You can also customize by entering your own text and expressions.
Points of Interest
Interesting aspect of the application was the use of non-recursive expression evaluation approach. Also, C# and .NET's stacks and dictionary objects were perfect to build the solution in no time.
As always, very much interested in hearing your constructive criticisms and suggestions.
History
- 4th June, 2008: Initial post
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.