File: __init__.py

package info (click to toggle)
python-acora 2.0-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,440 kB
  • sloc: python: 987; sh: 13; ansic: 12; makefile: 9
file content (314 lines) | stat: -rw-r--r-- 10,484 bytes parent folder | download | duplicates (4)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""\
Acora - a multi-keyword search engine based on Aho-Corasick trees.

Usage::

    >>> from acora import AcoraBuilder

Collect some keywords::

    >>> builder = AcoraBuilder('ab', 'bc', 'de')
    >>> builder.add('a', 'b')

Generate the Acora search engine::

    >>> ac = builder.build()

Search a string for all occurrences::

    >>> ac.findall('abc')
    [('a', 0), ('ab', 0), ('b', 1), ('bc', 1)]
    >>> ac.findall('abde')
    [('a', 0), ('ab', 0), ('b', 1), ('de', 2)]
"""

from __future__ import absolute_import

import sys
IS_PY3 = sys.version_info[0] >= 3

if IS_PY3:
    unicode = str

FILE_BUFFER_SIZE = 32 * 1024


class PyAcora(object):
    """A simple (and very slow) Python implementation of the Acora
    search engine.
    """
    transitions = None

    def __init__(self, machine, transitions=None):
        if transitions is not None:
            # old style format
            start_state = machine
            self.transitions = dict([
                ((state.id, char), (target_state.id, target_state.matches))
                for ((state, char), target_state) in transitions.items()])
        else:
            # new style Machine format
            start_state = machine.start_state
            ignore_case = machine.ignore_case
            self.transitions = transitions = {}

            child_states = machine.child_states
            child_targets = {}
            state_matches = {}
            needs_bytes_conversion = None
            for state in child_states:
                state_id = state.id
                child_targets[state_id], state_matches[state_id] = (
                    _merge_targets(state, ignore_case))
                if needs_bytes_conversion is None and state_matches[state_id]:
                    if IS_PY3:
                        needs_bytes_conversion = any(
                            isinstance(s, bytes) for s in state_matches[state_id])
                    elif any(isinstance(s, unicode) for s in state_matches[state_id]):
                        # in Py2, some keywords might be str even though we're processing unicode
                        needs_bytes_conversion = False

            if needs_bytes_conversion is None and not IS_PY3:
                needs_bytes_conversion = True
            if needs_bytes_conversion:
                if IS_PY3:
                    convert = ord
                else:
                    from codecs import latin_1_encode

                    def convert(s):
                        return latin_1_encode(s)[0]
            else:
                convert = None

            get_child_targets = child_targets.get
            get_matches = state_matches.get

            state_id = start_state.id
            for ch, child in _merge_targets(start_state, ignore_case)[0].items():
                child_id = child.id
                if convert is not None:
                    ch = convert(ch)
                transitions[(state_id, ch)] = (child_id, get_matches(child_id))

            for state in child_states:
                state_id = state.id
                for ch, child in get_child_targets(state_id).items():
                    child_id = child.id
                    if convert is not None:
                        ch = convert(ch)
                    transitions[(state_id, ch)] = (child_id, get_matches(child_id))

        self.start_state = start_state.id

    def finditer(self, s):
        """Iterate over all occurrences of any keyword in the string.

        Returns (keyword, offset) pairs.
        """
        state = self.start_state
        start_state = (state, [])
        next_state = self.transitions.get
        pos = 0
        for char in s:
            pos += 1
            state, matches = next_state((state, char), start_state)
            if matches:
                for match in matches:
                    yield (match, pos-len(match))

    def findall(self, s):
        """Find all occurrences of any keyword in the string.

        Returns a list of (keyword, offset) pairs.
        """
        return list(self.finditer(s))

    def filefind(self, f):
        """Iterate over all occurrences of any keyword in a file.

        Returns (keyword, offset) pairs.
        """
        opened = False
        if not hasattr(f, 'read'):
            f = open(f, 'rb')
            opened = True

        try:
            state = self.start_state
            start_state = (state, ())
            next_state = self.transitions.get
            pos = 0
            while 1:
                data = f.read(FILE_BUFFER_SIZE)
                if not data:
                    break
                for char in data:
                    pos += 1
                    state, matches = next_state((state, char), start_state)
                    if matches:
                        for match in matches:
                            yield (match, pos-len(match))
        finally:
            if opened:
                f.close()

    def filefindall(self, f):
        """Find all occurrences of any keyword in a file.

        Returns a list of (keyword, offset) pairs.
        """
        return list(self.filefind(f))


# import from shared Python/Cython module
from acora._acora import (
    insert_bytes_keyword, insert_unicode_keyword,
    build_trie as _build_trie, build_MachineState as _MachineState, merge_targets as _merge_targets)

# import from Cython module if available
try:
    from acora._cacora import (
        UnicodeAcora, BytesAcora, insert_bytes_keyword, insert_unicode_keyword)
except ImportError:
    # C module not there ...
    UnicodeAcora = BytesAcora = PyAcora


class AcoraBuilder(object):
    """The main builder class for an Acora search engine.

    Add keywords by calling ``.add(*keywords)`` or by passing them
    into the constructor. Then build the search engine by calling
    ``.build()``.

    Builds a case insensitive search engine when passing
    ``ignore_case=True``, and a case sensitive engine otherwise.
    """
    ignore_case = False

    def __init__(self, *keywords, **kwargs):
        if kwargs:
            self.ignore_case = kwargs.pop('ignore_case', False)
            if kwargs:
                raise TypeError(
                    "%s() got unexpected keyword argument %s" % (
                        self.__class__.__name__, next(iter(kwargs))))

        if len(keywords) == 1 and isinstance(keywords[0], (list, tuple)):
            keywords = keywords[0]
        self.for_unicode = None
        self.state_counter = 1
        self.keywords = set()
        self.tree = _MachineState(0)
        if keywords:
            self.update(keywords)

    def __update(self, keywords):
        """Add more keywords to the search engine builder.

        Adding keywords does not impact previously built search
        engines.
        """
        if not keywords:
            return
        self.tree = None
        self.keywords.update(keywords)
        if self.for_unicode is None:
            for keyword in keywords:
                if isinstance(keyword, unicode):
                    self.for_unicode = True
                elif isinstance(keyword, bytes):
                    self.for_unicode = False
                else:
                    raise TypeError(
                        "keywords must be either bytes or unicode, not mixed (got %s)" %
                        type(keyword))
                break
        # validate input string types
        marker = object()
        if self.for_unicode:
            for keyword in keywords:
                if not isinstance(keyword, unicode):
                    break
            else:
                keyword = marker
        else:
            for keyword in keywords:
                if not isinstance(keyword, bytes):
                    break
            else:
                keyword = marker
        if keyword is not marker:
            raise TypeError(
                "keywords must be either bytes or unicode, not mixed (got %s)" %
                type(keyword))

    def add(self, *keywords):
        """Add more keywords to the search engine builder.

        Adding keywords does not impact previously built search
        engines.
        """
        if keywords:
            self.update(keywords)

    def build(self, ignore_case=None, acora=None):
        """Build a search engine from the aggregated keywords.

        Builds a case insensitive search engine when passing
        ``ignore_case=True``, and a case sensitive engine otherwise.
        """
        if acora is None:
            if self.for_unicode:
                acora = UnicodeAcora
            else:
                acora = BytesAcora

        if self.for_unicode == False and ignore_case:
            import sys
            if sys.version_info[0] >= 3:
                raise ValueError(
                    "Case insensitive search is not supported for byte strings in Python 3")

        if ignore_case is not None and ignore_case != self.ignore_case:
            # must rebuild tree
            builder = type(self)(ignore_case=ignore_case)
            builder.update(self.keywords)
            return builder.build(acora=acora)

        return acora(_build_trie(self.tree, ignore_case=self.ignore_case))

    def update(self, keywords):
        for_unicode = self.for_unicode
        ignore_case = self.ignore_case
        insert_keyword = insert_unicode_keyword if for_unicode else insert_bytes_keyword
        for keyword in keywords:
            if for_unicode is None:
                for_unicode = self.for_unicode = isinstance(keyword, unicode)
                insert_keyword = (
                    insert_unicode_keyword if for_unicode else insert_bytes_keyword)
            elif for_unicode != isinstance(keyword, unicode):
                raise TypeError(
                    "keywords must be either bytes or unicode, not mixed (got %s)" %
                    type(keyword))
            self.state_counter = insert_keyword(
                self.tree, keyword, self.state_counter, ignore_case)
        self.keywords.update(keywords)


### convenience functions

def search(s, *keywords):
    """Convenience function to search a string for keywords.
    """
    acora = AcoraBuilder(keywords).build()
    return acora.findall(s)


def search_ignore_case(s, *keywords):
    """Convenience function to search a string for keywords.  Case
    insensitive version.
    """
    acora = AcoraBuilder(keywords, ignore_case=True).build()
    return acora.findall(s)