~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/support/lupy/search/camelcase.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# This module is part of the Lupy project and is Copyright 2005 Florian
2
 
# Festi. This is free software; you can redistribute
3
 
# it and/or modify it under the terms of version 2.1 of the GNU Lesser
4
 
# General Public License as published by the Free Software Foundation.
5
 
 
6
 
from term import TermQuery
7
 
from boolean import BooleanQuery, BooleanScorer
8
 
from phrase import PhraseQuery
9
 
from MoinMoin.support.lupy.index.term import Term
10
 
 
11
 
class CamelCaseQuery(TermQuery):
12
 
    """
13
 
    XXX write new comment
14
 
    A Query that matches documents that contains words
15
 
       the term starts with. This is usefull for CamelCase
16
 
       words. You need to filter the results to make shure
17
 
       the camel case words are really contained within the
18
 
       document.
19
 
    """
20
 
    def sumOfSquaredWeights(self, searcher):
21
 
        self.query = BooleanQuery()
22
 
        self.reader = searcher.reader
23
 
        self.splitToWords(self.term, self.reader, [])
24
 
        return self.query.sumOfSquaredWeights(searcher)
25
 
 
26
 
    def scorer(self, reader):
27
 
        return self.query.scorer(reader)
28
 
    
29
 
    def _add_phrase(self, terms):
30
 
        phrase = PhraseQuery()
31
 
        for term in terms:
32
 
            phrase.add(term)
33
 
        self.query.add(phrase, False, False)
34
 
        
35
 
    def splitToWords(self, term, reader, terms):
36
 
        text = term.text()
37
 
        field = term.field()
38
 
        for l in xrange(2, len(text)+1):
39
 
            prefix = text[:l]
40
 
            ts = reader.terms(Term(field, prefix))
41
 
            if ((ts.term.text()==prefix and
42
 
                ts.term.field()==field)):
43
 
                t = terms[:]
44
 
                t.append(ts.term)
45
 
                self.splitToWords(Term(field, text[l:]), reader, t)
46
 
        else:
47
 
            ts = reader.terms(term)
48
 
 
49
 
        # check for end words
50
 
        if len(text):
51
 
            return
52
 
            max_length = len(text) + 3
53
 
            while ts.term.text().startswith(text):
54
 
                if (len(ts.term.text()) < max_length and
55
 
                    ts.term.field()==field):
56
 
                    self._add_phrase(terms+[ts.term])
57
 
                try:
58
 
                    ts.next()
59
 
                except StopIteration:
60
 
                    break
61
 
        else:
62
 
            self._add_phrase(terms)