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

« back to all changes in this revision

Viewing changes to MoinMoin/support/lupy/index/term.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 2003 Amir
2
 
# Bakhtiar (amir@divmod.org). 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
 
class Term(object):
7
 
    
8
 
    def __init__(self, fld, txt, intern=False):
9
 
        self.set(fld, txt)
10
 
 
11
 
    def __cmp__(self, other):
12
 
        """Compares two terms, returning an integer which is less than zero iff this
13
 
        term belongs after the argument, equal zero iff this term is equal to the
14
 
        argument, and greater than zero iff this term belongs after the argument.
15
 
 
16
 
        The ordering of terms is first by field, then by text."""
17
 
 
18
 
        if self.fld == other.fld:
19
 
            # fields are interned
20
 
            return cmp(self.txt, other.txt)
21
 
        else:
22
 
            return cmp(self.fld, other.fld)
23
 
 
24
 
    def __hash__(self):
25
 
        return self._hash
26
 
    
27
 
    def field(self):
28
 
        return self.fld
29
 
    
30
 
    def readObject(self, inp):
31
 
        inp.defaultReadObject()
32
 
 
33
 
    def set(self, fld, txt):
34
 
        self.fld = fld
35
 
        self.txt = txt
36
 
        self._hash = hash(fld + txt)
37
 
 
38
 
    def text(self):
39
 
        return self.txt
40
 
 
41
 
    def __repr__(self):
42
 
        return 'Term<'+self.fld.encode('utf8')+':'+self.txt.encode('utf8')+'>'
43
 
 
44
 
class TermInfo(object):
45
 
 
46
 
    def __init__(self):
47
 
        self.docFreq = 0
48
 
        self.freqPointer = 0
49
 
        self.proxPointer = 0
50
 
 
51
 
    def set(self, df, fp, pp):
52
 
        self.docFreq = df
53
 
        self.freqPointer = fp
54
 
        self.proxPointer = pp
55
 
 
56
 
    def setTo(self, ti):
57
 
        self.docFreq = ti.docFreq
58
 
        self.freqPointer = ti.freqPointer
59
 
        self.proxPointer = ti.proxPointer
60
 
 
61
 
    def __repr__(self):
62
 
        return '<TermInfo:d:' + str(self.docFreq)+ ' f:' + str(self.freqPointer) +\
63
 
               ' p:' + str(self.proxPointer) + '>'
64
 
 
65
 
 
66
 
class TermInfosWriter(object):
67
 
    INDEX_INTERVAL = 128
68
 
 
69
 
 
70
 
    def __init__(self, d, seg, fis, isIndex = False):
71
 
        
72
 
        self.initialize(d, seg, fis, isIndex)
73
 
        
74
 
        self.size = 0
75
 
        self.lastIndexPointer = 0
76
 
        self.lastTerm = Term('','')
77
 
        self.lastTi = TermInfo()
78
 
        
79
 
        if isIndex is False:
80
 
            self.other = TermInfosWriter(d, seg, fis, True)
81
 
            self.other.other = self
82
 
 
83
 
            
84
 
    def initialize(self, d, seg, fis, isi):
85
 
        self.fieldInfos = fis
86
 
        self.isIndex = isi
87
 
        if isi is True:
88
 
            ext = '.tii'
89
 
        else:
90
 
            ext = '.tis'
91
 
            
92
 
        self.output=d.createFile(seg + ext)
93
 
        # leave space for size
94
 
        self.output.writeInt(0)
95
 
 
96
 
 
97
 
    def stringDifference(self, s1, s2):
98
 
        prefixLength = min(len(s1), len(s2))
99
 
        for i in range(prefixLength):
100
 
            if s1[i] != s2[i]:
101
 
                return i
102
 
        
103
 
        return prefixLength
104
 
 
105
 
 
106
 
    def add(self, term, ti):
107
 
        if not self.isIndex and term <= self.lastTerm:
108
 
            raise Exception, "term out of order: " + str(term) + str(self.lastTerm)
109
 
        if ti.freqPointer < self.lastTi.freqPointer:
110
 
            raise Exception, "freqPointer out of order"
111
 
        if ti.proxPointer < self.lastTi.proxPointer:
112
 
            raise Exception, "proxPointer out of order"
113
 
 
114
 
        if (not self.isIndex and self.size % self.INDEX_INTERVAL == 0):
115
 
            # add an index term
116
 
            self.other.add(self.lastTerm, self.lastTi)
117
 
 
118
 
        # write term
119
 
        self.writeTerm(term)
120
 
        # write doc freq
121
 
        self.output.writeVInt(ti.docFreq)
122
 
        # write pointers
123
 
        self.output.writeVLong(ti.freqPointer - self.lastTi.freqPointer)
124
 
        self.output.writeVLong(ti.proxPointer - self.lastTi.proxPointer)
125
 
 
126
 
        if self.isIndex:
127
 
            self.output.writeVLong(self.other.output.getFilePointer() - self.lastIndexPointer)
128
 
            self.lastIndexPointer = self.other.output.getFilePointer()
129
 
 
130
 
        self.lastTi.setTo(ti)
131
 
        self.size += 1
132
 
 
133
 
 
134
 
    def close(self):
135
 
        self.output.seek(0)
136
 
        self.output.writeInt(self.size)
137
 
        self.output.close()
138
 
 
139
 
        if self.isIndex is not True:
140
 
            self.other.close()
141
 
 
142
 
 
143
 
    def writeTerm(self, term):
144
 
        a, b = self.lastTerm.text(), term.text()
145
 
        start = self.stringDifference(a, b)
146
 
        delta = term.text()[start:]
147
 
        # write shared prefix length
148
 
        self.output.writeVInt(start)
149
 
        # write delta chars
150
 
        self.output.writeString(delta)
151
 
        # write field num
152
 
        i = self.fieldInfos.fieldNumber(term.field())
153
 
        self.output.writeVInt(i)
154
 
        self.lastTerm = term
155
 
 
156
 
 
157