private bool MatchAgainstDocText(string matchExpression, ScanPages scanPages, ref double matchFactorTotal, List<DocMatchingTextLoc> matchingTextLocs) { int curExpressionIdx = 0; StringTok st = new StringTok(matchExpression); return EvalMatch(matchExpression, st, scanPages, ref matchFactorTotal, ref curExpressionIdx, matchingTextLocs); }
private bool EvalMatch(string matchExpression, StringTok st, ScanPages scanPages, ref double matchFactorTotal, ref int curExpressionIdx, List<DocMatchingTextLoc> matchingTextLocs) { bool result = false; string token = ""; bool curOpIsOr = true; bool opIsInverse = false; DocRectangle docRectPercent = new DocRectangle(0, 0, 100, 100); int docRectValIdx = 0; double matchFactorForTerm = 0; #if TEST_PERF_EVALMATCH Stopwatch stopWatch1 = new Stopwatch(); stopWatch1.Start(); #endif while((token = st.GetNextToken()) != null) { if (token.Trim() == "") continue; else if (token == ")") return result; else if (token == "(") { bool tmpRslt = EvalMatch(matchExpression, st, scanPages, ref matchFactorTotal, ref curExpressionIdx, matchingTextLocs); if (opIsInverse) tmpRslt = !tmpRslt; if (curOpIsOr) result |= tmpRslt; else result &= tmpRslt; } else if (token == "&") curOpIsOr = false; else if (token == "|") curOpIsOr = true; else if (token == "!") opIsInverse = true; else { // We've reached a terminal token (string to match to text in the document) string stringToMatch = token; // Check for matchFactor - must have some text before it if (token == ":") return result; // See if there is a location defined by the next token while ((st.PeekNextToken() != null) && (st.PeekNextToken() == "")) st.GetNextToken(); if ((st.PeekNextToken() != null) && (st.PeekNextToken() == ":")) { matchFactorForTerm = 0; st.GetNextToken(); while ((st.PeekNextToken() != null) && (st.PeekNextToken() == "")) st.GetNextToken(); token = st.GetNextToken(); if (token != null) Double.TryParse(token, out matchFactorForTerm); } // Check for location on empty string if (token == "{") return result; // See if there is a location defined by the next token while ((st.PeekNextToken() != null) && (st.PeekNextToken() == "")) st.GetNextToken(); if ((st.PeekNextToken() != null) && (st.PeekNextToken() == "{")) { while ((token = st.GetNextToken()) != null) { if (token == "") continue; else if (token == "{") docRectValIdx = 0; else if (token == ",") docRectValIdx++; else if (token == "}") break; else { double rectVal = 0; Double.TryParse(token, out rectVal); docRectPercent.SetVal(docRectValIdx, rectVal); } } } // Process the match string using the location rectangle // The check for curOpIsOr || result is to avoid unnecessary work if the expression is already false and we're doing a AND if ((stringToMatch.Trim().Length >= 0) && (curOpIsOr || result)) { bool tmpRslt = MatchString(stringToMatch, docRectPercent, scanPages, curExpressionIdx, matchingTextLocs); if (opIsInverse) tmpRslt = !tmpRslt; if (curOpIsOr) result |= tmpRslt; else result &= tmpRslt; // Clear the inverse operator after 1 use opIsInverse = false; // Handle match factor if (tmpRslt) matchFactorTotal += matchFactorForTerm; } // Set the docRect to the entire page (ready for next term) docRectPercent = new DocRectangle(0,0,100,100); matchFactorForTerm = 0; curExpressionIdx++; } } #if TEST_PERF_EVALMATCH stopWatch1.Stop(); logger.Info("EvalMatch : {0:0.00} uS, expr {1}", stopWatch1.ElapsedTicks * 1000000.0 / Stopwatch.Frequency, matchExpression); #endif return result; }