Changeset 366

Show
Ignore:
Timestamp:
01/16/07 18:39:45 (2 years ago)
Author:
athomas
Message:

pyndexter: Bit of refactoring and clean up of slicing and indexing on Hit subclasses.

Files:

Legend:

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

    r364 r366  
    188188            else: 
    189189                all_uris.intersection_update(word_uris) 
    190         return DefaultSearch(self, phrase, all_uris) 
     190        return DefaultResult(self, all_uris) 
    191191    search = synchronised(search) 
    192192 
     
    238238 
    239239 
    240 class DefaultSearch(Search): 
     240class DefaultResult(Result): 
    241241    def __iter__(self): 
    242242        for uri in self.context: 
  • pyndexter/branches/refactoring/pyndexter/indexers/hype.py

    r365 r366  
    5757#        if order is not None: 
    5858#            search = search.order(order) 
    59         return HypeResult(self, query, search, self.enable_scoring) 
     59        return HypeResult(self, search, self.enable_scoring) 
    6060 
    6161    def optimise(self): 
     
    7272 
    7373    # Internal methods 
    74     def _translate_attributes(self, hdoc): 
    75         attributes = {} 
    76         for k in hdoc.attributes: 
    77             if k[0] == '@': 
    78                 attributes[k[1:]] = hdoc.get(k) 
    79             else: 
    80                 attributes[k] = hdoc.get(k) 
    81         return attributes 
    82  
    8374    def _compile_query(self, node): 
    8475        if not node or node.type == node.NULL: 
     
    10394 
    10495class HypeResult(Result): 
    105     def __init__(self, indexer, phrase, context, enable_scoring=True): 
     96    def __init__(self, indexer, context, enable_scoring=True): 
     97        self.enable_scoring = enable_scoring 
    10698        if enable_scoring: 
    10799            context = context.scores().option(hype.ESTCONDSCFB) 
    108         Result.__init__(self, indexer, phrase, context) 
     100        Result.__init__(self, indexer, context) 
    109101 
    110102    def __iter__(self): 
    111         for doc, score in self.context: 
    112             yield Hit(document=self.indexer.framework.fetch, 
    113                       score=score, 
    114                       **self.indexer._translate_attributes(doc)) 
     103        if self.enable_scoring: 
     104            for doc, score in self.context: 
     105                yield self._translate(doc, score) 
     106        else: 
     107            for doc in self.context: 
     108                yield self._translate(doc) 
    115109 
    116110    def __len__(self): 
     
    118112 
    119113    def __getitem__(self, index): 
    120         return self.context[index][0]['@uri'] 
     114        doc = self.context[index][0] 
     115        if self.enable_scoring: 
     116            score = self.context.get_score(index) 
     117        else: 
     118            score = None 
     119        return self._translate(doc, score) 
    121120 
     121    # Internal methods 
     122    def _translate(self, doc, score=None): 
     123        attrs = self._translate_attributes(doc) 
     124        if self.enable_scoring: 
     125            if score is None: 
     126                score = self.context.get_score(index) 
     127            attrs['score'] = score 
     128        return Hit(document=self.indexer.framework.fetch, **attrs) 
     129 
     130    def _translate_attributes(self, hdoc): 
     131        attributes = {} 
     132        for k in hdoc.attributes: 
     133            if k[0] == '@': 
     134                attributes[k[1:]] = hdoc.get(k) 
     135            else: 
     136                attributes[k] = hdoc.get(k) 
     137        return attributes 
     138 
  • pyndexter/branches/refactoring/pyndexter/indexers/hyperestraier.py

    r365 r366  
    4242        self.db.open(self.path, self.hype_mode) 
    4343 
    44     def fetch(self, uri): 
    45         uri = uri.encode('utf-8') 
    46         id = self.db.uri_to_id(uri) 
    47         if id == -1: 
    48             raise DocumentNotFound(uri) 
    49         doc = self.db.get_doc(id, 0) 
    50         attributes = self._translate_attributes(doc) 
    51         return Document(content=''.join(doc.texts()), source=self.framework.source, **attributes) 
    52  
    5344    def index(self, document): 
    5445        hdoc = HyperEstraier.Document() 
     
    7263        phrase = self._compile_query(query) 
    7364        return self.hype_search(phrase, simple=False) 
    74 #    def search(self, phrase, flags=0, order_by=None, 
    75 #               order_ascending=True, order_type=str): 
    76 #        phrase = ((not flags & SEARCH_UNION) and ' ' or '|').join(phrase.split()) 
    77 #        order = None 
    78 #        if order_by is not None: 
    79 #            if order_type is int: 
    80 #                order_type = 'NUM' 
    81 #            else: 
    82 #                order_type = 'STR' 
    83 #            order = u'@%s %s%s' % (order_by, order_type, 
    84 #                                   order_ascending and 'A' or 'D') 
    85 #        if not flags & SEARCH_ASTERISK: 
    86 #            phrase = phrase.replace('*', '\\*') 
    87 #        if not flags & SEARCH_QUESTION: 
    88 #            phrase = phrase.replace('?', '\\?') 
    89 #        if not flags & SEARCH_WHOLEWORD: 
    90 #            phrase = '*' + '* *'.join(phrase.split()) + '*' 
    91 #        return self.hype_search(phrase, order=order) 
    9265 
    9366    def optimise(self): 
     
    10982#        if order is not None: 
    11083#            search = search.order(order) 
    111         return HyperestraierResult(self, phrase, search) 
     84        return HyperestraierResult(self, search) 
    11285 
    11386    # Internal methods 
     
    128101            raise NotImplementedError 
    129102 
     103 
     104indexer_factory = ComponentFactory(HyperestraierIndexer, hype_mode=int) 
     105 
     106 
     107class HyperestraierResult(Result): 
     108    def __iter__(self): 
     109        for id in self.context: 
     110            yield self._translate(id) 
     111 
     112    def __len__(self): 
     113        return len(self.context) 
     114 
     115    def __getitem__(self, index): 
     116        return self._translate(self.context[index]) 
     117 
     118    # Internal methods 
     119    def _translate(self, id): 
     120        doc = self.indexer.db.get_doc(id, 0) 
     121        return Hit(document=self.indexer.framework.fetch, 
     122                   **self._translate_attributes(doc)) 
     123 
    130124    def _translate_attributes(self, hdoc): 
    131125        attributes = {} 
     
    137131        return attributes 
    138132 
    139  
    140 indexer_factory = ComponentFactory(HyperestraierIndexer, hype_mode=int) 
    141  
    142  
    143 class HyperestraierResult(Result): 
    144     def __iter__(self): 
    145         for id in self.context: 
    146             doc = self.indexer.db.get_doc(id, 0) 
    147             yield Hit(document=self.indexer.framework.fetch, 
    148                       **self.indexer._translate_attributes(doc)) 
    149  
    150     def __len__(self): 
    151         return len(self.context) 
    152  
    153     def __getitem__(self, index): 
    154         print type(self.context) 
    155         print dir(self.context) 
    156         return self.context[index]['@uri'] 
  • pyndexter/branches/refactoring/pyndexter/indexers/lupy.py

    r364 r366  
    4646        searcher = lupy.search.indexsearcher.IndexSearcher(self.db_path) 
    4747        hits = searcher.search(lupy_query) 
    48         return LupyResult(self, query, hits) 
     48        return LupyResult(self, hits) 
    4949 
    5050    def optimise(self): 
  • pyndexter/branches/refactoring/pyndexter/indexers/xapian.py

    r364 r366  
    7474        enquire = xapian.Enquire(self.db) 
    7575        enquire.set_query(query) 
    76         return XapianResult(self, query, enquire) 
     76        return XapianResult(self, enquire) 
    7777 
    7878    def state_store(self): 
     
    103103        matches = self.context.get_mset(0, 20) 
    104104        for hit in matches: 
    105             doc = hit[xapian.MSET_DOCUMENT] 
    106             terms = doc.termlist() 
    107             terms.skip_to('Q') 
    108             uri = terms.next()[0][1:] 
    109             assert uri, 'uniQue term (URI) not found in document term list' 
    110             yield Hit(uri, document=self.indexer.framework.fetch, 
    111                       did=hit[xapian.MSET_DID], 
    112                       score=float(hit[xapian.MSET_PERCENT]) / 100.0) 
     105            yield self._translate(hit) 
     106 
     107    def __getitem__(self, index): 
     108        matches = self.context.get_mset(index, 1) 
     109        for hit in matches: 
     110            return self._translate(hit) 
     111        return matches.next() 
     112 
     113    def __getslice__(self, i, j): 
     114        for hit in self.context.get_mset(i, j - i): 
     115            yield self._translate(hit) 
    113116 
    114117    def __len__(self): 
    115118        return len(self.context) 
     119 
     120    # Internal methods 
     121    def _translate(self, hit): 
     122        doc = hit[xapian.MSET_DOCUMENT] 
     123        terms = doc.termlist() 
     124        terms.skip_to('Q') 
     125        uri = terms.next()[0][1:] 
     126        assert uri, 'uniQue term (URI) not found in document term list' 
     127        return Hit(uri, document=self.indexer.framework.fetch, 
     128                   did=hit[xapian.MSET_DID], 
     129                   score=float(hit[xapian.MSET_PERCENT]) / 100.0) 
     130 
  • pyndexter/branches/refactoring/pyndexter/__init__.py

    r365 r366  
    603603    object. """ 
    604604 
    605     def __init__(self, indexer, query, context): 
     605    def __init__(self, indexer, context): 
    606606        self.indexer = indexer 
    607         self.query = query 
    608607        self.context = context 
    609608 
     
    649648        return key in self.attributes 
    650649 
     650    def __repr__(self): 
     651        return '<Hit %s>' % ' '.join(['%s=%s' % (k, repr(v)) for k, v in 
     652                                              self.attributes.iteritems()]) 
     653 
    651654    def _get_document(self): 
    652655        if callable(self._document):