This file is indexed.

/usr/share/doc/python-kjbuckets/examples/trigram.py is in python-kjbuckets 1:1.0.0-15.1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/python -O

"""Trigram indexing of documents.
  T = TriGram()
 makes a trigram archive.
 Add *HASHABLE* documents to the archive, associated with keywords by
  T.add_doc(document, [keyword1, keyword2, ...])
 Get documents that for each substring are associated with a keyword
  containing that substring with T.getDocs([substring1, substring2,...])
  (returns kjSet) OR
  T.getDocList([keyword1, keyword2,...]) returns list.
 if you can't hash your documents use some aliasing strategy...

 performance should be "good" when the graphs are sparse, but may
 get bad if graphs get extremely dense...
"""

### note: this is in the process of improvement ###
### need to vector docs to numbers to save space on loading ###

from kjbuckets import *
from string import upper, find

# note: documents must be hashable!

class TriGram:

    def __init__(self):
        self._trigramtokeyword = kjGraph()
        self._keywordtodoc = kjGraph()
        self._tricache = kjDict() # memory optimization

    def keys(self):
        return self._keywordtodoc.keys()

    def trigrams(self):
        return self._trigramtokeyword.keys()

    def marshal_to_file(self, file):
        from kjfactor import factor
        from marshal import dump
        dump( (factor(self._trigramtokeyword), factor(self._keywordtodoc)),
              file )

    def unmarshal_from_file(self, file):
        from marshal import load
        from kjfactor import unfactor
        (ttkf, kwdf) = load(file)
        self._trigramtokeyword = apply(unfactor, ttkf)
        self._keywordtodoc = apply(unfactor, kwdf)

    # associate document to all keywords in keyword_list
    def add_doc(self, document, keyword_list):
        for keyword in keyword_list:
            keyword = upper(keyword)
            self._addlink(keyword, document)

    # associate keyword to document, assumes keyword upcased
    def _addlink(self, keyword, document):
        cache = self._tricache
        ntrigrams = len(keyword) - 2
        if ntrigrams<1:
            #raise ValueError, "keyword must be length 3 or greater"
            return # ignore
        for start in range(ntrigrams):
            trigram = keyword[start: start+3]
            try:
                trigram = cache[trigram]
            except:
                cache[trigram]=trigram
            self._trigramtokeyword[trigram] = keyword
            self._keywordtodoc[keyword] = document

    # get set of keywords associated with a substring, assumes substring upcased
    def _getkeywords(self, substring):
        TtoK = self._trigramtokeyword
        ntrigrams = len(substring) - 2
        if ntrigrams<1:
            raise ValueError, \
              "substring must be length 3 or greater:" +`substring`
        keywords = None
        for start in range(ntrigrams):
            trigram = substring[start: start+3]
            thesekeywords = kjSet( TtoK.neighbors(trigram) )
            if keywords == None:
                keywords = thesekeywords
            else:
                keywords = keywords & thesekeywords
            if not keywords: break
        # now check for false hits (trigrams in wrong order...)
        for keyword in keywords.items():
            if find(keyword, substring)==-1:
                del keywords[keyword]
        return keywords

    # get kjSet of documents
    #  which for each substring of substring_list
    #  is associated with a keyword containing that substring.
    # (for boolean queries, left as set for easy combination...)
    def getDocs(self, substring_list):
        DocSet = None
        kwToDoc = self._keywordtodoc
        for substring in substring_list:
            substring = upper(substring)
            keywords = self._getkeywords(substring)
            thesedocs = kjSet((keywords * kwToDoc).values())
            if DocSet == None:
                DocSet = thesedocs
            else:
                DocSet = thesedocs & DocSet
            if not DocSet: break
        return DocSet

    # same as above, but returns list not set.
    def getDocList(self, substring_list):
        return self.getDocs(substring_list).items()

if __name__=="__main__":
    #### example usage and for testing
    bigstring = """
    Python release 1.1.1
    ====================
    ==> This is Python version 1.1.1.
    ==> Python 1.1.1 is a pure bugfix release.  It fixes two core dumps
        related to the changed implementation of (new)getargs, some
        portability bugs, and some very minor things here and there.  If
        you have 1.1, you only need to install 1.1 if bugs in it are
        bugging you.
    ==> If you don't know yet what Python is: it's an interpreted,
        extensible, embeddable, interactive, object-oriented programming
        language.  For a quick summary of what Python can mean for a
        UNIX/C programmer, read Misc/BLURB.LUTZ.
    ==> If you want to start compiling right away (on UNIX): just type
        "./configure" in the current directory and when it finishes, type
        "make".  See the section Build Instructions below for more
        details.
    ==> All documentation is in the subdirectory Doc in the form of LaTeX
        files.  In order of importance for new users: Tutorial (tut),
        Library Reference (lib), Language Reference (ref), Extending
        (ext).  Note that especially the Library Reference is of immense
        value since much of Python's power (including the built-in data
        types and functions!) is described there.  [NB The ext document
        has not been updated to reflect this release yet.] ....
    --Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
    <URL:http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>"""

    import string
    bigsplit = string.split(bigstring)
    print "testing loading"
    TGram = TriGram()
    # associate each string of bigstring to itself, unless too small...
    for str in bigsplit:
        if len(str)>3:
            TGram.add_doc(str, [str])
    print len(TGram._trigramtokeyword), len(TGram._keywordtodoc)
    print "testing marshalling"
    f = open("test.mar", "wb")
    TGram.marshal_to_file(f)
    f.close()
    f = open("test.mar", "rb")
    TGram = TriGram()
    TGram.unmarshal_from_file(f)
    f.close()
    print "testing retrieval"
    print TGram.getDocList(["thon"])
    print TGram.getDocList(["tion"])
    print TGram.getDocList(["dire"])
    print TGram.getDocList(["here"])
    print TGram.getDocList(["ers","sio"])
    print TGram.getDocList(["int","era"])
    print TGram.getDocList(["htt","url","van"])
    print TGram.getDocList(["Nope"])