Changeset 359

Show
Ignore:
Timestamp:
01/04/07 20:45:43 (2 years ago)
Author:
athomas
Message:

pyndexter: Hyperestraier adapter is now basically working.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pyndexter/branches/refactoring/pyndexter/hyperestraier.py

    r357 r359  
    2424        Indexer.bind(self, framework) 
    2525 
    26         self.path = os.path.join(framework.path, 'hyperestraier.db') 
     26        self.path = os.path.join(framework.path, 'hyperestraier.db').encode('utf-8') 
    2727 
    2828        if framework.mode == READWRITE: 
     
    3939 
    4040    def fetch(self, uri): 
     41        uri = uri.encode('utf-8') 
    4142        id = self.db.uri_to_id(uri) 
     43        if id == -1: 
     44            raise DocumentNotFound(uri) 
    4245        doc = self.db.get_doc(id, 0) 
    43         if doc is None: 
    44             raise DocumentNotFound(uri) 
    4546        attributes = self._translate_attributes(doc) 
    4647        return Document(content=''.join(doc.texts()), source=self.framework.source, **attributes) 
     
    5556 
    5657    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: 
    5961            raise DocumentNotFound(uri) 
    60         self.db.out_doc(doc, HyperEstraier.Database.ODCLEAN) 
     62        self.db.out_doc(id, HyperEstraier.Database.ODCLEAN) 
    6163 
    6264    def search(self, query): 
    63         raise NotImplementedError 
     65        phrase = self._compile_query(query) 
     66        return self.hype_search(phrase, simple=False) 
    6467#    def search(self, phrase, flags=0, order_by=None, 
    6568#               order_ascending=True, order_type=str): 
     
    9497    def hype_search(self, phrase, simple=True, order=None): 
    9598        """ 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) 
    99104        return HyperestraierSearch(self, phrase, search) 
    100105 
    101106    # 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 
    102123    def _translate_attributes(self, hdoc): 
    103124        attributes = {} 
     
    112133class HyperestraierSearch(Search): 
    113134    def __iter__(self): 
    114         for doc in self.context: 
     135        for id in self.context: 
     136            doc = self.indexer.db.get_doc(id, 0) 
    115137            # How do we get the score? 
    116138            yield Hit(document=self.indexer.fetch, 
  • pyndexter/branches/refactoring/pyndexter/__init__.py

    r357 r359  
    244244    """ A query parse node. """ 
    245245 
    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 
    250251 
    251252    __slots__ = ('type', 'value', 'left', 'right') 
     
    258259 
    259260    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') 
    263262        def show(node, depth=0): 
    264263            if node.type == QueryNode.TERM: 
     
    312311        nil)) 
    313312 
    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 
    322318    """ 
    323319 
     
    326322                  'ex': QueryNode.NOT, 'or': QueryNode.OR} 
    327323 
    328     def __init__(self, query): 
     324    def __init__(self, phrase): 
    329325        QueryNode.__init__(self, None) 
    330326        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)] 
    332328        root = self.parse(tokens) 
     329        self.phrase = phrase 
    333330        if root: 
    334331            for k in self.__slots__: 
     
    389386    def search(self, query): 
    390387        """ Search with the given Query. """ 
    391         raise NotImplementedError 
    392  
     388        # TODO Add support for result ordering 
     389        raise NotImplementedError 
     390 
     391    # Optional methods 
    393392    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 
    398396    def bind(self, framework): 
    399397        """ Bind the `Indexer` to the given framework. """ 
     
    412410 
    413411class Framework(object): 
    414     """ The glue. Ties `Indexer` and `Source` together and provides a 
    415     convenient interface. """ 
     412    """ The glue. Ties `Indexer` and `Source` together, performs housekeeping 
     413    tasks and provides a convenient interface to it all. """ 
    416414    def __init__(self, path, indexer, source=None, mode=READWRITE): 
    417415        self.path = path 
     
    518516    object. """ 
    519517 
    520     def __init__(self, indexer, phrase, context): 
     518    def __init__(self, indexer, query, context): 
    521519        self.indexer = indexer 
    522         self.phrase = phrase 
     520        self.query = query 
    523521        self.context = context 
    524522