1. Mikhail Korobov
  2. DAWG

Commits

Mirth Hickford  committed e2da93e

add has_keys_with_prefix method (python, tests and docs)

  • Participants
  • Parent commits e10ad7c
  • Branches default

Comments (0)

Files changed (5)

File .gitignore

View file
+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
+

File AUTHORS.rst

View file
 * 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.

File docs/index.rst

View file
     >>> 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')

File src/dawg.pyx

View file
             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 +:
         """

File tests/test_dawg.py

View file
         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)