我贴出了这个问题:
Non overlapping pattern matching with gap constraint in python;两个月前。我只有一个回应。但解决方案相当长,对于模式中的每个单词,都会形成一个嵌套循环。有没有办法递归地形成下面的函数?在i=0
while i < len(pt_dic[pt_split[0]]):
match=False
ii = pt_dic[pt_split[0]][i]
#print "ii=" + str(ii)
# Start loop at next index after ii
j = next(x[0] for x in enumerate(pt_dic[pt_split[1]]) if x[1] > ii)
while j < len(pt_dic[pt_split[1]]) and not match:
jj = pt_dic[pt_split[1]][j]
#print "jj=" + str(jj)
if jj > ii and jj <= ii + 2:
# Start loop at next index after ii
k = next(x[0] for x in enumerate(pt_dic[pt_split[2]]) if x[1] > jj)
while k < len(pt_dic[pt_split[2]]) and not match:
kk = pt_dic[pt_split[2]][k]
#print "kk=" + str(kk)
if kk > jj and kk <= jj + 2:
# Start loop at next index after kk
l = next(x[0] for x in enumerate(pt_dic[pt_split[3]]) if x[1] > kk)
while l < len(pt_dic[pt_split[2]]) and not match:
ll = pt_dic[pt_split[3]][l]
#print "ll=" + str(ll)
if ll > kk and ll <= kk + 2:
print "Match: (" + str(ii) + "," + str(jj) + "," + str(kk) + "," + str(ll) + ")"
# Now that we've found a match, skip indices within that match.
i = next(x[0] for x in enumerate(pt_dic[pt_split[0]]) if x[1] > ll)
i -= 1
match=True
l += 1
k += 1
j += 1
i += 1
编辑:对于那些不了解上下文的人:
我想找出在一个序列中出现的一个模式的非重叠匹配的总数,间隙约束为2。在
例如A B C是一种使用某种算法发现的模式。我必须找到这个模式的总数出现在一个序列中,比如A A B B C D E A B C …,其中max-gap约束是2。在
Max.gap不在整个序列中显示,而是在属于序列中的子字符串的模式的两个单词之间。E、 g.Pat: A B C和{}。在
在本例中,A B D E C ...是一个匹配项,因为a、B和B、C之间允许的最大间隙为两个。接下来我们将找到另一个匹配项A B A B C。有趣的是。有两个匹配项(2个字符b/w A,b和2个字符b/w b,C)。不过,我们只算一个,因为这是一个重叠的比赛。A B X X X C无效。在