Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add has_keys_with_prefix method (python, tests and docs)

  • Loading branch information...
commit ea9189eafefc652ea36bbfb98490ae5607c3cf3f 1 parent 8200dbd
@hickford hickford authored
View
32 .gitignore
@@ -0,0 +1,32 @@
+MANIFEST
+src/*.html
+
+*.py[cod]
+
+# C extensions
+*.so
+
+# Packages
+*.egg
+*.egg-info
+dist
+build
+eggs
+parts
+bin
+var
+sdist
+develop-eggs
+.installed.cfg
+lib
+lib64
+__pycache__
+
+# Installer logs
+pip-log.txt
+
+# Unit test / coverage reports
+.coverage
+.tox
+nosetests.xml
+
View
3  AUTHORS.rst
@@ -4,7 +4,8 @@ Authors & Contributors
* Mikhail Korobov <kmike84@gmail.com>;
* Dan Blanchard;
* Jakub Wilk;
-* Alex Moiseenko.
+* Alex Moiseenko;
+* `Matt Hickford <https://github.com/matt-hickford>`_.
This module uses `dawgdic`_ C++ library by
Susumu Yata & contributors.
View
5 docs/index.rst
@@ -79,6 +79,11 @@ prefix in a ``CompletionDAWG``::
>>> completion_dawg.keys(u'foo')
>>> [u'foo', u'foobar']
+to test whether some key begins with a given prefix::
+
+ >>> completion_dawg.has_keys_with_prefix(u'foo')
+ >>> True
+
and to find all prefixes of a given key::
>>> base_dawg.prefixes(u'foobarz')
View
12 src/dawg.pyx
@@ -325,6 +325,18 @@ cdef class CompletionDAWG(DAWG):
key = (<char*>completer.key()).decode('utf8')
yield key
+ def has_keys_with_prefix(self, unicode prefix):
+ cdef bytes b_prefix = prefix.encode('utf8')
+ cdef BaseType index = self.dct.root()
+
+ if not self.dct.Follow(b_prefix, &index):
+ return False
+
+ cdef Completer completer
+ init_completer(completer, self.dct, self.guide)
+ completer.Start(index, b_prefix)
+
+ return completer.Next()
cpdef bytes tobytes(self) except +:
"""
View
9 tests/test_dawg.py
@@ -181,6 +181,15 @@ def test_completion(self):
assert d.keys('b') == ['bar']
assert d.keys('foo') == ['foo', 'foobar']
+ def test_has_keys_with_prefix(self):
+ assert self.empty_dawg().has_keys_with_prefix('') == False
+
+ d = self.dawg()
+ assert d.has_keys_with_prefix('') == True
+ assert d.has_keys_with_prefix('b') == True
+ assert d.has_keys_with_prefix('fo') == True
+ assert d.has_keys_with_prefix('bo') == False
+
def test_completion_dawg_saveload(self):
buf = BytesIO()
self.dawg().write(buf)
Please sign in to comment.
Something went wrong with that request. Please try again.