Changeset 359
- Timestamp:
- 01/04/07 20:45:43 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
pyndexter/branches/refactoring/pyndexter/hyperestraier.py
r357 r359 24 24 Indexer.bind(self, framework) 25 25 26 self.path = os.path.join(framework.path, 'hyperestraier.db') 26 self.path = os.path.join(framework.path, 'hyperestraier.db').encode('utf-8') 27 27 28 28 if framework.mode == READWRITE: … … 39 39 40 40 def fetch(self, uri): 41 uri = uri.encode('utf-8') 41 42 id = self.db.uri_to_id(uri) 43 if id == -1: 44 raise DocumentNotFound(uri) 42 45 doc = self.db.get_doc(id, 0) 43 if doc is None:44 raise DocumentNotFound(uri)45 46 attributes = self._translate_attributes(doc) 46 47 return Document(content=''.join(doc.texts()), source=self.framework.source, **attributes) … … 55 56 56 57 def discard(self, uri): 57 doc = self.db.get_doc_by_uri(uri) 58 if not doc: 58 uri = uri.encode('utf-8') 59 id = self.db.uri_to_id(uri) 60 if id == -1: 59 61 raise DocumentNotFound(uri) 60 self.db.out_doc( doc, HyperEstraier.Database.ODCLEAN)62 self.db.out_doc(id, HyperEstraier.Database.ODCLEAN) 61 63 62 64 def search(self, query): 63 raise NotImplementedError 65 phrase = self._compile_query(query) 66 return self.hype_search(phrase, simple=False) 64 67 # def search(self, phrase, flags=0, order_by=None, 65 68 # order_ascending=True, order_type=str): … … 94 97 def hype_search(self, phrase, simple=True, order=None): 95 98 """ Full Hyperestraier search phrase. """ 96 search = self.db.search(phrase, simple=simple) 97 if order is not None: 98 search = search.order(order) 99 cond = HyperEstraier.Condition() 100 cond.set_phrase(phrase) 101 search = self.db.search(cond, 0) 102 # if order is not None: 103 # search = search.order(order) 99 104 return HyperestraierSearch(self, phrase, search) 100 105 101 106 # Internal methods 107 def _compile_query(self, node): 108 if not node or node.type == node.NULL: 109 return '' 110 if node.type == node.AND: 111 return '%s AND %s' % (self._compile_query(node.left), 112 self._compile_query(node.right)) 113 elif node.type == node.OR: 114 return '%s OR %s' % (self._compile_query(node.left), 115 self._compile_query(node.right)) 116 elif node.type == node.NOT: 117 return '!%s' % self._compile_query(node.left) 118 elif node.type == node.TERM: 119 return node.value 120 else: 121 raise NotImplementedError 122 102 123 def _translate_attributes(self, hdoc): 103 124 attributes = {} … … 112 133 class HyperestraierSearch(Search): 113 134 def __iter__(self): 114 for doc in self.context: 135 for id in self.context: 136 doc = self.indexer.db.get_doc(id, 0) 115 137 # How do we get the score? 116 138 yield Hit(document=self.indexer.fetch, pyndexter/branches/refactoring/pyndexter/__init__.py
r357 r359 244 244 """ A query parse node. """ 245 245 246 TERM = 0 247 NOT = 1 248 AND = 2 249 OR = 3 246 NULL = 0 247 TERM = 1 248 NOT = 2 249 AND = 3 250 OR = 4 250 251 251 252 __slots__ = ('type', 'value', 'left', 'right') … … 258 259 259 260 def __repr__(self): 260 if self.type is None: 261 return '' 262 type_map = ('term', 'not', 'and', 'or') 261 type_map = ('null', 'term', 'not', 'and', 'or') 263 262 def show(node, depth=0): 264 263 if node.type == QueryNode.TERM: … … 312 311 nil)) 313 312 314 >>> Query('brie cheese or camembert cheese') 315 (and 316 ("brie") 317 (or 318 ("cheese") 319 (and 320 ("camembert") 321 ("cheese")))) 313 >>> Query('"brie cheese" or "camembert cheese"') 314 (or 315 ("brie cheese") 316 ("camembert cheese")) 317 322 318 """ 323 319 … … 326 322 'ex': QueryNode.NOT, 'or': QueryNode.OR} 327 323 328 def __init__(self, query):324 def __init__(self, phrase): 329 325 QueryNode.__init__(self, None) 330 326 tokens = [(self._group_map[token.lastgroup], token.group(token.lastindex)) 331 for token in self._tokenise.finditer( query)]327 for token in self._tokenise.finditer(phrase)] 332 328 root = self.parse(tokens) 329 self.phrase = phrase 333 330 if root: 334 331 for k in self.__slots__: … … 389 386 def search(self, query): 390 387 """ Search with the given Query. """ 391 raise NotImplementedError 392 388 # TODO Add support for result ordering 389 raise NotImplementedError 390 391 # Optional methods 393 392 def __iter__(self): 394 """ Iterate over all documents in the index. """ 395 raise NotImplementedError 396 397 # Optional methods 393 """ Iterate over all URI's in the index. """ 394 raise NotImplementedError 395 398 396 def bind(self, framework): 399 397 """ Bind the `Indexer` to the given framework. """ … … 412 410 413 411 class Framework(object): 414 """ The glue. Ties `Indexer` and `Source` together and provides a415 convenient interface. """412 """ The glue. Ties `Indexer` and `Source` together, performs housekeeping 413 tasks and provides a convenient interface to it all. """ 416 414 def __init__(self, path, indexer, source=None, mode=READWRITE): 417 415 self.path = path … … 518 516 object. """ 519 517 520 def __init__(self, indexer, phrase, context):518 def __init__(self, indexer, query, context): 521 519 self.indexer = indexer 522 self. phrase = phrase520 self.query = query 523 521 self.context = context 524 522
