Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix numbering of words #13

Merged
merged 3 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions blacksquare/crossword.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,17 @@ def _parse_grid(self) -> None:
old_across, old_down = self._across, self._down
padded = np.pad(self._grid, 1, constant_values=Cell(None, (None, None), BLACK))
shifted_down, shifted_right = padded[:-2, 1:-1], padded[1:-1, :-2]

shifted_up, shifted_left = padded[2:, 1:-1], padded[1:-1, 2:]
is_open = ~np.equal(self._grid, BLACK)
starts_down, starts_across = (
np.equal(x, BLACK) for x in (shifted_down, shifted_right)
)
needs_num = (is_open) & (starts_down | starts_across)
too_short_down, too_short_across = (
np.equal(x, BLACK) for x in (shifted_up, shifted_left)
)
starts_down = starts_down & ~too_short_down
starts_across = starts_across & ~too_short_across
needs_num = is_open & (starts_down | starts_across)
self._numbers = np.reshape(np.cumsum(needs_num), self._grid.shape) * needs_num
self._across = (
np.maximum.accumulate(starts_across * self._numbers, axis=1) * is_open
Expand Down Expand Up @@ -476,12 +481,16 @@ def get_word_at_index(

Returns:
Optional[Word]: The word passing through the index in the provided
direction. If the index corresponds to a black square, this method returns
None.
direction. If the index corresponds to a black square, or there is
no word in that direction (an unchecked light) this
method returns None.
"""
if self[index] != BLACK:
number = self._get_direction_mask(direction)[index]
return self[direction, number]
try:
return self[direction, number]
except:
return None

def set_word(
self, word_index: WordIndex, value: str, inplace: bool = True
Expand Down
4 changes: 3 additions & 1 deletion blacksquare/word.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def find_matches(self, word_list: Optional[WordList] = None) -> MatchWordList:
letter_scores_per_index = {}
for idx in open_indices:
cross = self.crosses[idx]
if cross is None:
continue
cross_index = cross.crosses.index(self)
cross_matches = word_list.find_matches(cross)
letter_scores_per_index[idx] = cross_matches.letter_scores_at_index(
Expand All @@ -135,7 +137,7 @@ def score_word_fn(word: str, score: float) -> float:
per_letter_scores = [
letter_scores_per_index[i].get(word[i], 0)
* INVERSE_CHARACTER_FREQUENCIES.get(word[i], 1)
for i in open_indices
for i in open_indices if i in letter_scores_per_index
]
return np.log(np.prod(per_letter_scores) + 1) * score

Expand Down