def _hit_context(self, hit): qparser = self._searcher.parser query = self._searcher.query contents = read_file(self.path) lines = [] # For each query word, for queryWord in set(query): # Reverse map query words to document words documentWords = list(qparser.unstemlist(queryWord)) # If the query word is not in the document, skip it if not documentWords: continue # Prepare regular expression using matching document words searchExpression = r'|'.join(documentWords) pattern = re.compile(searchExpression, re.IGNORECASE) for match in pattern.finditer(contents): token = self.Token() token.startchar = match.start() token.endchar = match.end() # get the context line context = fragment_text(token, contents) self.append_line(lines, context) if len(lines) >= self.max_sub_results: break return u''.join(lines)
def _hit_context(self, hit): qparser = self._searcher.parser query = self._searcher.query contents = read_file(self.path) lines = [] # For each query word, for queryWord in set(query): # Reverse map query words to document words documentWords = list(qparser.unstemlist(queryWord)) # If the query word is not in the document, skip it if not documentWords: continue # Prepare regular expression using matching document words searchExpression = r'|'.join(_ensure_str(word) for word in documentWords) pattern = re.compile(searchExpression, re.IGNORECASE) for match in pattern.finditer(contents): token = self.Token() token.startchar = match.start() token.endchar = match.end() # get the context line context = fragment_text(token, contents) self.append_line(lines, context) if len(lines) >= self.max_sub_results: break return u''.join(lines)
def format(self, fragments, replace=False): lines = [] for fragment in fragments: context = fragment_text(fragment, fragment.text) lines.append(context) if len(lines) >= self.max_sub_results: break final_text = u''.join(lines) return final_text