File: whooshSearchEngine.py

package info (click to toggle)
thawab 4.1-2
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye, sid
  • size: 1,912 kB
  • sloc: python: 4,041; makefile: 134; sh: 33
file content (281 lines) | stat: -rw-r--r-- 11,464 bytes parent folder | download
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# -*- coding: UTF-8 -*-
"""

Copyright © 2008, Muayyad Alsadi <alsadi@ojuba.org>

        Released under terms of Waqf Public License.
        This program is free software; you can redistribute it and/or modify
        it under the terms of the latest version Waqf Public License as
        published by Ojuba.org.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

        The Latest version of the license can be found on
        "http://waqf.ojuba.org/license"

"""
import sys, os, os.path, re
import shutil
from .tags import *
from .meta import prettyId,makeId

from whoosh import query
from whoosh.index import EmptyIndexError, create_in, open_dir, IndexVersionError
from whoosh.highlight import highlight, SentenceFragmenter, BasicFragmentScorer, FIRST, HtmlFormatter
from whoosh.filedb.filestore import FileStorage
from whoosh.fields import Schema, ID, IDLIST, TEXT
from whoosh.formats import Frequency
from whoosh.qparser import QueryParserError
from whoosh.lang.porter import stem
from whoosh.analysis import StandardAnalyzer, StemFilter

try:
    from whoosh.index import _CURRENT_TOC_VERSION as whoosh_ix_ver
except ImportError:
    from whoosh.filedb.fileindex import _INDEX_VERSION as whoosh_ix_ver

from .stemming import stemArabic

def stemfn(word): return stemArabic(stem(word))
# word_re = ur"[\w\u064e\u064b\u064f\u064c\u0650\u064d\u0652\u0651\u0640]"
analyzer = StandardAnalyzer(expression = r"[\w\u064e\u064b\u064f\u064c\u0650\u064d\u0652\u0651\u0640]+(?:\.?[\w\u064e\u064b\u064f\u064c\u0650\u064d\u0652\u0651\u0640]+)*") | StemFilter(stemfn)

from whoosh.qparser import FieldAliasPlugin
from .whooshSymbolicQParser import MultifieldSQParser

class ExcerptFormatter(object):
        def __init__(self, between = "..."):
                self.between = between
                
        def _format_fragment(self, text, fragment):
                output = []
                index = fragment.startchar
                
                for t in fragment.matches:
                        if t.startchar > index:
                                output.append(text[index:t.startchar])
                        
                        ttxt = text[t.startchar:t.endchar]
                        if t.matched:
                            ttxt = "\0" + ttxt.upper() + "\010"
                        output.append(ttxt)
                        index = t.endchar
                
                output.append(text[index:fragment.endchar])
                return "".join(output)

        def __call__(self, text, fragments):
                return self.between.join((self._format_fragment(text, fragment)
                                          for fragment in fragments))


from .baseSearchEngine import BaseSearchEngine
class SearchEngine(BaseSearchEngine):
    def __init__(self, th):
        BaseSearchEngine.__init__(self, th, False)
        self.__ix_writer = None
        ix_dir = os.path.join(th.prefixes[0],'index', "ix_" + str(whoosh_ix_ver))
        if not os.path.isdir(ix_dir):
            os.makedirs(ix_dir)
        # try to load a pre-existing index
        try:
            self.indexer = open_dir(ix_dir)
        except (EmptyIndexError, IndexVersionError):
            # create a new one
            try:
                shutil.rmtree(ix_dir, True)
                os.makedirs(ix_dir)
            except OSError:
                pass
            schema = Schema(
                kitab = ID(stored = True),
                vrr = ID(stored = True, unique = False), # version release
                nodeIdNum = ID(stored = True, unique = False), 
                title = TEXT(stored = True, field_boost = 1.5, analyzer = analyzer),
                content = TEXT(stored = False,analyzer = analyzer),
                #content = TEXT(stored = False,analyzer = analyzer,
                #vector = Frequency(analyzer = analyzer)), # with term vector
                tags=IDLIST(stored = False)
            )
            self.indexer = create_in(ix_dir, schema)
        #self.__ix_qparser = ThMultifieldParser(self.th, ("title","content",), schema=self.indexer.schema)
        self.__ix_qparser = MultifieldSQParser(("title","content",), self.indexer.schema)
        self.__ix_qparser.add_plugin(FieldAliasPlugin({
                "kitab":("كتاب",),
                "title":("عنوان",),
                "tags":("وسوم",)})
        )
        #self.__ix_pre = whoosh.query.Prefix
        self.__ix_searcher =  self.indexer.searcher()

    def __del__(self):
        if self.__ix_writer: self.__ix_writer.commit()

    def getIndexedVersion(self, name):
        """
        return a Version-Release string if in index, otherwise return None
        """
        try:
            d = self.__ix_searcher.document(kitab = str(makeId(name)))
        except TypeError:
            return None
        except KeyError:
            return None
        if d:
            return d['vrr']
        return None

    def queryIndex(self, queryString):
        """return an interatable of fields dict"""
        # FIXME: the return should not be implementation specific
        try:
            r = self.__ix_searcher.search(self.__ix_qparser.parse(queryString), limit = 500)
        except QueryParserError:
            return None
        return r

    def resultExcerpt(self, results, i, ki = None):
        # FIXME: this should not be implementation specific
        if not ki:
            r = results[i]
            name = r['kitab']
            v = r['vrr'].split('-')[0]
            m = self.th.getMeta().getLatestKitabV(name,v)
            ki = self.th.getCachedKitab(m['uri'])
        num = int(results[i]['nodeIdNum'])
        node = ki.getNodeByIdNum(num)
        n = ki.toc.next(node)
        if n:
            ub = n.globalOrder
        else:
            ub = -1
        txt = node.toText(ub)
        s = set()
        #results.query.all_terms(s) # return (field,term) pairs
        # return (field,term) pairs    # self.self.__ix_searcher.reader() 
        results.q.existing_terms(self.indexer.reader(), s, phrases = True)
        terms = list(dict(
                [(i[1],i[0]) for i in [j for j in s if j[0] == 'content' or j[0] == 'title']]).keys())
        #print "txt = [%s]" % len(txt)
        snippet = txt[:min(len(txt),512)] # dummy summary
        snippet = highlight(txt,
                            terms,
                            analyzer,
                            SentenceFragmenter(sentencechars = ".!?؟\n"),
                            HtmlFormatter(between = "\u2026\n"),
                            top = 3,
                            scorer = BasicFragmentScorer,
                            minscore = 1,
                            order = FIRST)
        #snippet = highlight(txt, terms, analyzer,
        #     SentenceFragmenter(sentencechars = ".!?"), ExcerptFormatter(between = u"\u2026\n"), top = 3,
        #     scorer = BasicFragmentScorer, minscore = 1,
        #     order = FIRST)
        return snippet

    def indexingStart(self):
        """
        should be called before any sequence of indexing Ops, reindexAll() calls this method automatically
        """
        if not self.__ix_writer:
            try:
                self.__ix_writer = self.indexer.writer()
            except OSError as e:
                print('*** whooshSearchEnfine.indexingStart: %s', e)
                

    def indexingEnd(self):
        """
        should be called after a sequence of indexing Ops, reindexAll() calls this method automatically
        """
        self.__ix_writer.commit(optimize = True)
        # self.indexer.optimize() # no need for this with optimize in previous line
        self.reload()

    def reload(self):
        """
        called after commiting changes to index (eg. adding or dropping from index)
        """
        self.__ix_searcher = self.__ix_searcher.refresh() # no need to obtain new one with self.indexer.searcher()
        self.__ix_writer = None

    def dropKitabIndex(self, name):
        """
        drop search index for a given Kitab by its uri
        if you call indexingStart() before this
        then you must call indexingEnd() after it
        """
        # FIXME: it seems that this used not work correctly without commit() just after drop, this mean that reindex needs a commit in-between
        ki = self.th.getKitab(name)
        if ki:
            self.th.getMeta().setIndexedFlags(ki.uri, 1)
        print("dropping index for kitab name:", name, end=' ')
        w, c = self.__ix_writer, False
        if not w:
            w, c = self.indexer.writer(), True # creates a writer internally if one is not defined
        # NOTE: because the searcher could be limited do a loop that keeps deleting till the query is empty
        while(w.delete_by_term('kitab', name)):
            print("*", end=' ')
        print()
        if c:
            w.commit()
        if ki:
            self.th.getMeta().setIndexedFlags(ki.uri, 0)

    def dropAll(self):
        # FIXME: it would be more effeciant to delete the directory
        # NOTE: see http://groups.google.com/group/whoosh/browse_thread/thread/35b1700b4e4a3d5d
        self.th.getMeta().setAllIndexedFlags(1)
        self.indexingStart()
        reader = self.indexer.reader() # also self.__ix_searcher.reader()
        for docnum in reader.all_stored_fields():
            self.__ix_writer.delete_document(docnum)
        self.indexingEnd()
        self.th.getMeta().setAllIndexedFlags(0)

    def reindexKitab(self,name):
        """
        you need to call indexingStart() before this and indexingEnd() after it
        """
        # NOTE: this method is overridden here because we need to commit 
        # between dropping and creating a new index.
        # NOTE: can't use updateDocument because each Kitab contains many documents
        self.dropKitabIndex(name)
        self.__ix_writer.commit()
        self.indexKitab(name)

    def addDocumentToIndex(self, name, vrr, nodeIdNum, title, content, tags):
        """
        this method must be overridden in implementation specific way
        """
        if content:
            self.__ix_writer.add_document(kitab = name,
                                          vrr = vrr,
                                          nodeIdNum = str(nodeIdNum),
                                          title = title,
                                          content = content,
                                          tags = tags)

    def keyterms(self, kitab, vrr, nodeIdNum):
        s = self.indexer.searcher()
        dn = s.document_number(kitab = kitab, vrr = vrr, nodeIdNum = str(nodeIdNum))
        if dn  ==  None:
            return None, []
        print(" ## ", dn)
        r = s.key_terms([dn], "content", numterms = 5)
        return dn, r

    def related(self, kitab, vrr, nodeIdNum):
        dn, kt = self.keyterms(kitab, vrr, nodeIdNum)
        if not dn:
            return None
        for t, r in kt:
            print("term = ", t, " @ rank = ",r)
        q = query.Or([query.Term("content", t) for (t, r) in kt])
        results = self.indexer.searcher().search(q, limit = 10)
        for i, fields in enumerate(results):
            if results.docnum(i)  !=  dn:
                print(fields['kitab'],"\t\t",str(fields['nodeIdNum']),"\t\t",fields['title'])