diff --git a/src/ahocorapy/keywordtree.py b/src/ahocorapy/keywordtree.py index 383b8db..451c065 100644 --- a/src/ahocorapy/keywordtree.py +++ b/src/ahocorapy/keywordtree.py @@ -5,9 +5,6 @@ Quite optimized, the code may not be as beautiful as you like, since inlining and so on was necessary -This library is optimized for the cPython interpreter. -I will most likely run slower with pypy, etc. - Created on Jan 5, 2016 @author: Frederik Petersen (fp@abusix.com) @@ -150,9 +147,6 @@ def search_lss_for_children(self, zero_state): def search_lss(self, state): if state.longest_strict_suffix is None: parent = state.parent - if parent.longest_strict_suffix is None: - # Has not been done yet. Do early - self.search_lss(parent) traversed = parent.longest_strict_suffix while True: if state.symbol in traversed.transitions and\ @@ -164,11 +158,10 @@ def search_lss(self, state): state.longest_strict_suffix = self._zero_state break else: - if traversed.longest_strict_suffix is None: - self.search_lss(traversed) traversed = traversed.longest_strict_suffix suffix = state.longest_strict_suffix - + if suffix.longest_strict_suffix is None: + self.search_lss(suffix) for symbol, next_state in suffix.transitions.items(): if (symbol not in state.transitions and suffix != self._zero_state): diff --git a/tests/ahocorapy_test.py b/tests/ahocorapy_test.py index 7b4284f..3ffd30f 100644 --- a/tests/ahocorapy_test.py +++ b/tests/ahocorapy_test.py @@ -257,5 +257,20 @@ def test_search_all_issue_1_similar(self): self.assertEqual(('bar', 5), next(results)) + def test_search_all_issue_3_similar(self): + text = '/foo/bar' + words = ['foo/', 'foo', '/foo/', '/bar'] + tree = KeywordTree(case_insensitive=True) + for word in words: + tree.add(word) + tree.finalize() + + results = tree.search_all(text) + + self.assertEqual(('foo', 1), next(results)) + self.assertEqual(('/foo/', 0), next(results)) + self.assertEqual(('foo/', 1), next(results)) + self.assertEqual(('/bar', 4), next(results)) + if __name__ == '__main__': unittest.main()