Bug? default return_all @ AC.match()
At https://github.com/nppoly/cyac/blob/master/lib/cyac/ac.pyx#L261, The description is "return_all: by default, only return the longest. ..."
But when I run following code, I cannot understand the output.
ac = cyac.AC.build([u"py", u"python"])
text = 'python'
for idx,s,e in ac.match(text):
print(s, e, ac[idx])
-- output --
0 2 py <== ??
0 6 python
Is it a bug?
it's introduced in https://github.com/nppoly/cyac/pull/3. I will try to fix it.
@ddolgi In worst case, If dictionary is [chr_1...chr_n, chr_2...chr_n, ....,chr_n, chr_1....chr_(n-1), ....chr_(n-1),....], size is O(n2). and given text is chr_1...chr_n. we have to maintain an array of all possible matched substring, size of this array is O(n2). it's not very efficient. so current implementation actually returns longest substring in substrings with same suffix. any good idea?
by design, I want to make cyac use O(1) space cost, and O(n) time cost.
@ddolgi https://github.com/nppoly/cyac/pull/17 please try this branch
@ddolgi #17 please try this branch
I think this implementation is still not correct, maybe we should only support return all substring.
Hi @ddolgi , I changed description to avoid ambiguity.
@chenkovsky Thank you very much. Now I know that finding global longest match is not easy. I think greedy longest match could be fast, but I'm not sure this will be useful.
@chenkovsky Thank you very much.
Now I know that finding global longest match is not easy.
I think greedy longest match could be fast, but I'm not sure this will be useful.
hi @ddolgi what is your scenario? is Viterbi algorithm suitable for your scenario?
My 'greedy' means: When 'py,pyt,thon' in a trie, returning 'pyt'(leftmost longest one) on 'python' input.
@ddolgi
ac = cyac.AC.build([u"py", u"pyt", u"thon"])
list(ac.match_longest("python")) # this is not based on ac algorithm, it's based on trie.
# [(1, 0, 3)]
or
trie = cyac.Trie()
trie.insert(u"py")
trie.insert(u"pyt")
trie.insert(u"thon")
list(trie.match_longest("python"))