cyac icon indicating copy to clipboard operation
cyac copied to clipboard

Bug? default return_all @ AC.match()

Open ddolgi opened this issue 4 years ago • 10 comments

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?

ddolgi avatar Nov 11 '22 01:11 ddolgi

it's introduced in https://github.com/nppoly/cyac/pull/3. I will try to fix it.

chenkovsky avatar Nov 11 '22 09:11 chenkovsky

@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?

chenkovsky avatar Nov 11 '22 12:11 chenkovsky

by design, I want to make cyac use O(1) space cost, and O(n) time cost.

chenkovsky avatar Nov 11 '22 12:11 chenkovsky

@ddolgi https://github.com/nppoly/cyac/pull/17 please try this branch

chenkovsky avatar Nov 12 '22 05:11 chenkovsky

@ddolgi #17 please try this branch

I think this implementation is still not correct, maybe we should only support return all substring.

chenkovsky avatar Nov 13 '22 01:11 chenkovsky

Hi @ddolgi , I changed description to avoid ambiguity.

chenkovsky avatar Nov 22 '22 02:11 chenkovsky

@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.

ddolgi avatar Nov 23 '22 04:11 ddolgi

@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?

chenkovsky avatar Nov 23 '22 13:11 chenkovsky

My 'greedy' means: When 'py,pyt,thon' in a trie, returning 'pyt'(leftmost longest one) on 'python' input.

ddolgi avatar Dec 06 '22 05:12 ddolgi

@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"))

chenkovsky avatar Dec 07 '22 01:12 chenkovsky