Next / Previous / Contents / Shipman's homepage

22. Puzzle.__digestWords(): Parse the word list

# - - -   P u z z l e . _ _ d i g e s t W o r d s

    def __digestWords ( self, rawWords ):
        '''Feed the word list to the WordBank instance.

          [ rawWords is a list of strings ->
              if (rawWords is a valid word list) and
              (the counts and lengths of the word list match the
              counts and lengths of slots in self.__slotMap) ->
                self.__wordBank  :=  a WordBank instance representing
                    the words in rawWords
              else -> raise SyntaxError ]

First create the empty WordBank. See Section 60, “class WordBank: The word list and puzzle state”.

        #-- 1 --
        # [ self.__wordBank  :=  a new, empty WordBank instance
        #       using self's geometry ]
        self.__wordBank = WordBank ( self )

The words are scattered across the lines of rawWords and separated by whitespace. The word list is not allowed to include the UNK_CELL character; so far, that is the only way to get a syntax error. See Section 62, “WordBank.addWord(): Add a word to the word list”.

        #-- 2 --
        # [ if rawWords contains the UNK_CELL character ->
        #     raise SyntaxError
        #   else ->
        #     self.__wordBank  +:=  whitespace-separated words
        #         from rawWords ]
        for line in rawWords:
            wordList = line.split()
            for word in wordList:
                if UNK_CELL in word:
                    raise SyntaxError ( "Words may not contain the "
                        "'%d' symbol; that denotes an unsolved "
                        "letter." % UNK_CELL )
                if len(word) < 2:
                    raise SyntaxError ( "One-letter words such as "
                        "'%s' are not allowed." % word )
                self.__wordBank.addWord ( word )

Now that we have the complete word list, we must verify that the number of words of each length exactly matches the number of slots of each length, or the puzzle is not correctly constructed. It is also an error if any word has length 1. See Section 23, “Puzzle.__matchLengths(): Does the word list match the slot set?”.

        #-- 3 --
        # [ if (the counts of slots of each length in self.__slotMap
        #   do not exactly match the counts of words of each length
        #   in self.__wordBank) ->
        #     raise SyntaxError
        #   else -> I ]