def get_crossers(word): word_coordinates = [(r, c) for (r, c) in word.cell_iterator()] wordtext = word.get_text() crossers = [] for i, crossword in enumerate(word.get_crossing_words()): crosser = dict() # Constraint for this letter in the word crosser['pos'] = i + 1 # Index within this word (1, 2, ..., length) crosser['letter'] = wordtext[i] # Letter of this word crosser['text'] = crossword.get_text().replace( ' ', '.') # Text of crossing word crosser['location'] = crossword.location # Now figure out the position in the crossing word where # it crosses this word index = 0 for (r, c) in crossword.cell_iterator(): index += 1 if (r, c) in word_coordinates: break crosser['index'] = index # Point at which we cross the crossing word # Examine all possible values for the crossing word and keep track # of their letter at the crossing index pattern = "^" + crossword.get_text() + "$" pattern = re.sub('[ ?]', '.', pattern) # Figure out the regular expression for this letter position letter_set = set() nchoices = 0 for word in get_matching_words(pattern): nchoices += 1 letter_set.add(word[index - 1]) letters = ''.join(list(letter_set)) regexp = LetterList.regexp(letters) if not regexp: # Special case - word is used but not in dictionary regexp = wordtext[i] nchoices = 1 crosser['regexp'] = regexp # Regular expression for the crossing point crosser['choices'] = nchoices # Add to the constraint list crossers.append(crosser) return crossers
def test_with_all(self): letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" self.assertEqual(".", LetterList.regexp(letters))
def test_with_empty_pattern(self): letters = "" self.assertEqual("", LetterList.regexp(letters))
def test_with_single_letter(self): letters = "S" self.assertEqual("S", LetterList.regexp(letters))
def test_with_all_but_z(self): letters = "ABCDEFGHIJKLMNOPQRSTUVWXY" self.assertEqual("[^Z]", LetterList.regexp(letters))
def test_with_all_but_j_and_q(self): letters = "ABCDEFGHIKLMNOPRSTUVWXYZ" self.assertEqual("[^JQ]", LetterList.regexp(letters))
def test_with_gaps(self): letters = "BCDKLMWXZ" self.assertEqual("[^AE-JN-VY]", LetterList.regexp(letters))
def test_with_small_straight(self): letters = "ABCD" self.assertEqual("[A-D]", LetterList.regexp(letters))