-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_seq.pyx
More file actions
294 lines (245 loc) · 9.69 KB
/
Copy path_seq.pyx
File metadata and controls
294 lines (245 loc) · 9.69 KB
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
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# cyseq is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/cyseq for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
from collections import namedtuple as _namedtuple
cimport cython
from libcpp.algorithm cimport sort as cpp_sort
from libcpp.unordered_map cimport unordered_map
from libcpp.vector cimport vector
"""
Token sequences alignment and diffing based on the longest common substrings of
"high tokens". This essentially a non-optimal and reasonably fast single local
sequence alignment between two sequences of integers/token ids.
Based on and heavily modified from Python's difflib.py from the 3.X tip:
https://hg.python.org/cpython/raw-file/0a69b1e8b7fe/Lib/difflib.py
and CyDifflib from the 1.2.X tip:
https://github.com/rapidfuzz/CyDifflib/blob/ef0d1cb49abbdd551e9a27065032fc5317c731fd/src/cydifflib/_initialize.pyx
license: PSF and MIT. See seq.pyx.ABOUT file for details.
"""
Match = _namedtuple('Match', 'a b size')
ctypedef struct MatchingBlockQueueElem:
Py_ssize_t alo
Py_ssize_t ahi
Py_ssize_t blo
Py_ssize_t bhi
ctypedef struct CMatch:
Py_ssize_t a
Py_ssize_t b
Py_ssize_t size
cdef int CMatch_sorter(const CMatch& lhs, const CMatch& rhs):
if lhs.a != rhs.a:
return lhs.a < rhs.a
if lhs.b != rhs.b:
return lhs.b < rhs.b
return lhs.size < rhs.size
cdef CMatch _find_longest_match(
a,
b,
Py_ssize_t alo,
Py_ssize_t ahi,
Py_ssize_t blo,
Py_ssize_t bhi,
b2j,
Py_ssize_t len_good,
matchables
) except *:
"""
Find longest matching block of a and b in a[alo:ahi] and b[blo:bhi].
`b2j` is a mapping of b high token ids -> list of position in b
`len_good` is such that token ids smaller than `_good_good` are treated as
good, non-junk tokens. `matchables` is a set of matchable positions.
Positions absent from this set are ignored.
Return (i,j,k) Match tuple where:
"i" in the start in "a"
"j" in the start in "b"
"k" in the size of the match
and such that a[i:i+k] is equal to b[j:j+k], where
alo <= i <= i+k <= ahi
blo <= j <= j+k <= bhi
and for all (i',j',k') matchable token positions meeting those conditions,
k >= k'
i <= i'
and if i == i', j <= j'
In other words, of all maximal matching blocks, return one that starts
earliest in a, and of all those maximal matching blocks that start earliest
in a, return the one that starts earliest in b.
First the longest matching block (aka contiguous substring) is determined
where no junk element appears in the block. Then that block is extended as
far as possible by matching other tokens including junk on both sides. So
the resulting block never matches on junk.
If no blocks match, return CMatch(alo, blo, 0).
"""
cdef Py_ssize_t besti, bestj, bestsize
cdef Py_ssize_t i, j, k
cdef unordered_map[Py_ssize_t, Py_ssize_t] j2len
cdef unordered_map[Py_ssize_t, Py_ssize_t] newj2len
besti, bestj, bestsize = alo, blo, 0
# find longest junk-free match
# during an iteration of the loop, j2len[j] = length of longest
# junk-free match ending with a[i-1] and b[j]
nothing = []
for i in range(alo, ahi):
# we cannot do LCS on junk or non matchable
cura = a[i]
if cura < len_good and i in matchables:
# look at all instances of a[i] in b; note that because
# b2j has no junk keys, the loop is skipped if a[i] is junk
for j in b2j.get(cura, nothing):
# a[i] matches b[j]
if j < blo:
continue
if j >= bhi:
break
k = newj2len[j] = j2len[j - 1] + 1
if k > bestsize:
besti = i - k + 1
bestj = j - k + 1
bestsize = k
j2len.swap(newj2len)
newj2len.clear()
return extend_match(besti, bestj, bestsize, a, b, alo, ahi, blo, bhi, matchables)
cdef CMatch extend_match(
Py_ssize_t besti,
Py_ssize_t bestj,
Py_ssize_t bestsize,
a,
b,
Py_ssize_t alo,
Py_ssize_t ahi,
Py_ssize_t blo,
Py_ssize_t bhi,
matchables
):
"""
Extend a match identifier by (besti, bestj, bestsize) with any matching
tokens on each end. Return a new CMatch.
"""
if bestsize:
while (besti > alo and bestj > blo
and a[besti - 1] == b[bestj - 1]
and (besti - 1) in matchables):
besti -= 1
bestj -= 1
bestsize += 1
while (besti + bestsize < ahi and bestj + bestsize < bhi
and a[besti + bestsize] == b[bestj + bestsize]
and (besti + bestsize) in matchables):
bestsize += 1
return CMatch(besti, bestj, bestsize)
def find_longest_match(
a,
b,
Py_ssize_t alo,
Py_ssize_t ahi,
Py_ssize_t blo,
Py_ssize_t bhi,
b2j,
Py_ssize_t len_good,
matchables
):
"""
Find longest matching block of a and b in a[alo:ahi] and b[blo:bhi].
`b2j` is a mapping of b high token ids -> list of position in b
`len_good` is such that token ids smaller than `_good_good` are treated as
good, non-junk tokens. `matchables` is a set of matchable positions.
Positions absent from this set are ignored.
Return (i,j,k) Match tuple where:
"i" in the start in "a"
"j" in the start in "b"
"k" in the size of the match
and such that a[i:i+k] is equal to b[j:j+k], where
alo <= i <= i+k <= ahi
blo <= j <= j+k <= bhi
and for all (i',j',k') matchable token positions meeting those conditions,
k >= k'
i <= i'
and if i == i', j <= j'
In other words, of all maximal matching blocks, return one that starts
earliest in a, and of all those maximal matching blocks that start earliest
in a, return the one that starts earliest in b.
First the longest matching block (aka contiguous substring) is determined
where no junk element appears in the block. Then that block is extended as
far as possible by matching other tokens including junk on both sides. So
the resulting block never matches on junk.
If no blocks match, return Match(alo, blo, 0).
"""
x = _find_longest_match(a, b, alo, ahi, blo, bhi, b2j, len_good, matchables)
return Match(x.a, x.b, x.size)
def match_blocks(
a,
b,
Py_ssize_t a_start,
Py_ssize_t a_end,
b2j,
Py_ssize_t len_good,
matchables,
*args,
**kwargs
):
"""
Return a list of matching block Match triples describing matching
subsequences of `a` in `b` starting from the `a_start` position in `a` up to
the `a_end` position in `a`.
`b2j` is a mapping of b "high" token ids -> list of positions in b, e.g. a
posting list.
`len_good` is such that token ids smaller than `len_good` are treated as
important, non-junk tokens.
`matchables` is a set of matchable positions. Positions absent from this set
are ignored.
Each triple is of the form (i, j, n), and means that a[i:i+n] == b[j:j+n].
The triples are monotonically increasing in i and in j. It is also
guaranteed that adjacent triples never describe adjacent equal blocks.
Instead adjacent blocks are merged and collapsed in a single block.
"""
cdef Py_ssize_t i, j, k, i1, j1, k1, i2, j2, k2
cdef Py_ssize_t alo, ahi, blo, bhi
cdef vector[MatchingBlockQueueElem] queue
cdef vector[CMatch] matching_blocks
# This non-recursive algorithm is using a list as a queue of blocks. We
# still need to look at and append partial results to matching_blocks in a
# loop. The matches are sorted at the end.
queue.push_back(MatchingBlockQueueElem(a_start, a_end, 0, len(b)))
while not queue.empty():
elem = queue.back()
alo, ahi, blo, bhi = elem.alo, elem.ahi, elem.blo, elem.bhi
queue.pop_back()
x = _find_longest_match(a, b, alo, ahi, blo, bhi, b2j, len_good, matchables)
i, j, k = x.a, x.b, x.size
# a[alo:i] vs b[blo:j] unknown
# a[i:i+k] same as b[j:j+k]
# a[i+k:ahi] vs b[j+k:bhi] unknown
if k: # if k is 0, there was no matching block
matching_blocks.push_back(x)
if alo < i and blo < j:
# there is unprocessed things remaining to the left
queue.push_back(MatchingBlockQueueElem(alo, i, blo, j))
if i + k < ahi and j + k < bhi:
# there is unprocessed things remaining to the right
queue.push_back(MatchingBlockQueueElem(i+k, ahi, j+k, bhi))
cpp_sort(matching_blocks.begin(), matching_blocks.end(), &CMatch_sorter)
# collapse adjacent blocks
i1 = j1 = k1 = 0
non_adjacent = []
for match in matching_blocks:
i2, j2, k2 = match.a, match.b, match.size
# Is this block adjacent to i1, j1, k1?
if i1 + k1 == i2 and j1 + k1 == j2:
# Yes, so collapse them -- this just increases the length of
# the first block by the length of the second, and the first
# block so lengthened remains the block to compare against.
k1 += k2
else:
# Not adjacent. Remember the first block (k1==0 means it's
# the dummy we started with), and make the second block the
# new block to compare against.
if k1:
non_adjacent.append(Match(i1, j1, k1))
i1, j1, k1 = i2, j2, k2
if k1:
non_adjacent.append(Match(i1, j1, k1))
return non_adjacent