[英]Finding all common, non-overlapping substrings
給定兩個字符串,我想識別從最長到最短的所有常見子字符串。
我想刪除任何“子”子字符串。 例如,'1234'的任何子串都不會包含在'12345'和'51234'之間的匹配中。
string1 = '51234'
string2 = '12345'
result = ['1234', '5']
我想找到最長的公共子串 ,然后遞歸地找到左/右的最長子串。 但是,我不希望在找到之后刪除一個公共子字符串。 例如,下面的結果在中間共享一個6:
string1 = '12345623456'
string2 = '623456'
result = ['623456', '23456']
最后,我需要針對數千個字符串的固定列表檢查一個字符串。 我不確定是否有一個聰明的步驟可以用來散列這些字符串中的所有子串。
以前的答案:
在這個線程中 ,發現了一個動態編程解決方案需要O(nm)時間,其中n和m是字符串的長度。 我對更有效的方法感興趣,它將使用后綴樹。
背景:
我正在用旋律片段創作歌曲旋律。 有時,組合設法生成在現有的一行中匹配太多音符的旋律。
我可以使用字符串相似性度量,例如編輯距離,但相信與旋律有很小差異的曲調是獨特且有趣的。 不幸的是,這些曲調與連續復制旋律的許多音符的歌曲具有相似的相似度。
讓我們從樹開始吧
from collections import defaultdict
def identity(x):
return x
class TreeReprMixin(object):
def __repr__(self):
base = dict(self)
return repr(base)
class PrefixTree(TreeReprMixin, defaultdict):
'''
A hash-based Prefix or Suffix Tree for testing for
sequence inclusion. This implementation works for any
slice-able sequence of hashable objects, not just strings.
'''
def __init__(self):
defaultdict.__init__(self, PrefixTree)
self.labels = set()
def add(self, sequence, label=None):
layer = self
if label is None:
label = sequence
if label:
layer.labels.add(label)
for i in range(len(sequence)):
layer = layer[sequence[i]]
if label:
layer.labels.add(label)
return self
def add_ngram(self, sequence, label=None):
if label is None:
label = sequence
for i in range(1, len(sequence) + 1):
self.add(sequence[:i], label)
def __contains__(self, sequence):
layer = self
j = 0
for i in sequence:
j += 1
if not dict.__contains__(layer, i):
break
layer = layer[i]
return len(sequence) == j
def depth_in(self, sequence):
layer = self
count = 0
for i in sequence:
if not dict.__contains__(layer, i):
print "Breaking"
break
else:
layer = layer[i]
count += 1
return count
def subsequences_of(self, sequence):
layer = self
for i in sequence:
layer = layer[i]
return layer.labels
def __iter__(self):
return iter(self.labels)
class SuffixTree(PrefixTree):
'''
A hash-based Prefix or Suffix Tree for testing for
sequence inclusion. This implementation works for any
slice-able sequence of hashable objects, not just strings.
'''
def __init__(self):
defaultdict.__init__(self, SuffixTree)
self.labels = set()
def add_ngram(self, sequence, label=None):
if label is None:
label = sequence
for i in range(len(sequence)):
self.add(sequence[i:], label=label)
要填充樹,您將使用.add_ngram
方法。
下一部分有點棘手,因為您正在尋找並發遍歷字符串同時跟蹤樹坐標。 為了解決所有這些問題,我們需要一些在樹上運行的函數和一個查詢字符串
def overlapping_substrings(string, tree, solved=None):
if solved is None:
solved = PrefixTree()
i = 1
last = 0
matching = True
solutions = []
while i < len(string) + 1:
if string[last:i] in tree:
if not matching:
matching = True
else:
i += 1
continue
else:
if matching:
matching = False
solutions.append(string[last:i - 1])
last = i - 1
i -= 1
i += 1
if matching:
solutions.append(string[last:i])
for solution in solutions:
if solution in solved:
continue
else:
solved.add_ngram(solution)
yield solution
def slide_start(string):
for i in range(len(string)):
yield string[i:]
def seek_subtree(tree, sequence):
# Find the node of the search tree which
# is found by this sequence of items
node = tree
for i in sequence:
if i in node:
node = node[i]
else:
raise KeyError(i)
return node
def find_all_common_spans(string, tree):
# We can keep track of solutions to avoid duplicates
# and incomplete prefixes using a Prefix Tree
seen = PrefixTree()
for substring in slide_start(string):
# Drive generator forward
list(overlapping_substrings(substring, tree, seen))
# Some substrings are suffixes of other substrings which you do not
# want
compress = SuffixTree()
for solution in sorted(seen.labels, key=len, reverse=True):
# A substrings may be a suffix of another substrings, but that substrings
# is actually a repeating pattern. If a solution is
# a repeating pattern, `not solution in seek_subtree(tree, solution)` will tell us.
# Otherwise, discard the solution
if solution in compress and not solution in seek_subtree(tree, solution):
continue
else:
compress.add_ngram(solution)
return compress.labels
def search(query, corpus):
tree = SuffixTree()
if isinstance(corpus, SuffixTree):
tree = corpus
else:
for elem in corpus:
tree.add_ngram(elem)
return list(find_all_common_spans(query, tree))
所以現在做你想做的事,做到這一點:
search("12345", ["51234"])
search("623456", ["12345623456"])
如果有什么不清楚的地方,請告訴我,我會盡力澄清。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.