Hacky fix for inconsistently named camera.
[python_utils.git] / unscrambler.py
index 1b242309b649eaa036277fdb22fc6f9c7705f0c8..d70db9936201041a7888e3aee4de9c95f7c61951 100644 (file)
@@ -1,5 +1,7 @@
 #!/usr/bin/env python3
 
 #!/usr/bin/env python3
 
+# © Copyright 2021-2022, Scott Gasch
+
 """A fast word unscrambler library."""
 
 import logging
 """A fast word unscrambler library."""
 
 import logging
@@ -100,10 +102,16 @@ class Unscrambler(object):
     lookup methods support a "fuzzy match" argument that can be set to
     request similar words that do not match exactly in addition to any
     exact matches.
     lookup methods support a "fuzzy match" argument that can be set to
     request similar words that do not match exactly in addition to any
     exact matches.
-
     """
 
     def __init__(self, indexfile: Optional[str] = None):
     """
 
     def __init__(self, indexfile: Optional[str] = None):
+        """
+        Constructs an unscrambler.
+
+        Args:
+            indexfile: overrides the default indexfile location if provided
+        """
+
         # Cached index per instance.
         self.sigs = []
         self.words = []
         # Cached index per instance.
         self.sigs = []
         self.words = []
@@ -120,9 +128,10 @@ class Unscrambler(object):
 
     @staticmethod
     def get_indexfile(indexfile: Optional[str]) -> str:
 
     @staticmethod
     def get_indexfile(indexfile: Optional[str]) -> str:
+        """Returns the current indexfile location."""
         if indexfile is None:
             if 'unscrambler_default_indexfile' in config.config:
         if indexfile is None:
             if 'unscrambler_default_indexfile' in config.config:
-                indexfile = config.config['unscramble_indexfile']
+                indexfile = config.config['unscrambler_default_indexfile']
             else:
                 indexfile = "/usr/share/dict/sparse_index"
         else:
             else:
                 indexfile = "/usr/share/dict/sparse_index"
         else:
@@ -175,6 +184,12 @@ class Unscrambler(object):
         the word and their frequencies.  We try to cluster "similar"
         words close to each other in the signature space.
 
         the word and their frequencies.  We try to cluster "similar"
         words close to each other in the signature space.
 
+        Args:
+            word: the word to compute a signature for
+
+        Returns:
+            The word's signature.
+
         >>> train = Unscrambler.compute_word_sig('train')
         >>> train
         23178969883741
         >>> train = Unscrambler.compute_word_sig('train')
         >>> train
         23178969883741
@@ -199,9 +214,14 @@ class Unscrambler(object):
         dictfile: str = '/usr/share/dict/words',
         indexfile: str = '/usr/share/dict/sparse_index',
     ) -> None:
         dictfile: str = '/usr/share/dict/words',
         indexfile: str = '/usr/share/dict/sparse_index',
     ) -> None:
-        """Before calling this method, change letter_sigs from the default above
-        unless you want to populate the same exact files.
+        """
+        Repopulates the indexfile.
+
+        .. warning::
 
 
+            Before calling this method, change letter_sigs from the
+            default above unless you want to populate the same exact
+            files.
         """
         words_by_sigs: Dict[int, str] = {}
         seen = set()
         """
         words_by_sigs: Dict[int, str] = {}
         seen = set()
@@ -227,10 +247,20 @@ class Unscrambler(object):
         """Looks up a potentially scrambled word optionally including near
         "fuzzy" matches.
 
         """Looks up a potentially scrambled word optionally including near
         "fuzzy" matches.
 
+        Args:
+            word: the word to lookup
+            window_size: the number of nearby fuzzy matches to return
+
+        Returns:
+            A dict of word -> bool containing unscrambled words with (close
+            to or precisely) the same letters as the input word.  The bool
+            values in this dict indicate whether the key word is an exact
+            or near match.  The count of entries in this dict is controlled
+            by the window_size param.
+
         >>> u = Unscrambler()
         >>> u.lookup('eanycleocipd', window_size=0)
         {'encyclopedia': True}
         >>> u = Unscrambler()
         >>> u.lookup('eanycleocipd', window_size=0)
         {'encyclopedia': True}
-
         """
         sig = Unscrambler.compute_word_sig(word)
         return self.lookup_by_sig(sig, window_size=window_size)
         """
         sig = Unscrambler.compute_word_sig(word)
         return self.lookup_by_sig(sig, window_size=window_size)
@@ -240,6 +270,18 @@ class Unscrambler(object):
         a previous call to Unscrambler.compute_word_sig.  Optionally returns
         near "fuzzy" matches.
 
         a previous call to Unscrambler.compute_word_sig.  Optionally returns
         near "fuzzy" matches.
 
+        Args:
+            sig: the signature of the word to lookup (see :meth:`compute_word_sig`
+                to generate these signatures).
+            window_size: the number of nearby fuzzy matches to return
+
+        Returns:
+            A dict of word -> bool containing unscrambled words with (close
+            to or precisely) the same letters as the input word.  The bool
+            values in this dict indicate whether the key word is an exact
+            or near match.  The count of entries in this dict is controlled
+            by the window_size param.
+
         >>> sig = Unscrambler.compute_word_sig('sunepsapetuargiarin')
         >>> sig
         18491949645300288339
         >>> sig = Unscrambler.compute_word_sig('sunepsapetuargiarin')
         >>> sig
         18491949645300288339
@@ -247,7 +289,6 @@ class Unscrambler(object):
         >>> u = Unscrambler()
         >>> u.lookup_by_sig(sig)
         {'pupigerous': False, 'pupigenous': False, 'unpurposing': False, 'superpurgation': False, 'unsupporting': False, 'superseptuaginarian': True, 'purpurogallin': False, 'scuppaug': False, 'purpurigenous': False, 'purpurogenous': False, 'proppage': False}
         >>> u = Unscrambler()
         >>> u.lookup_by_sig(sig)
         {'pupigerous': False, 'pupigenous': False, 'unpurposing': False, 'superpurgation': False, 'unsupporting': False, 'superseptuaginarian': True, 'purpurogallin': False, 'scuppaug': False, 'purpurigenous': False, 'purpurogenous': False, 'proppage': False}
-
         """
         ret = {}
         (_, location) = list_utils.binary_search(self.sigs, sig)
         """
         ret = {}
         (_, location) = list_utils.binary_search(self.sigs, sig)