Skip to content

Commit

Permalink
new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaher86 committed Sep 27, 2023
1 parent 0be49b8 commit 0d4420b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/blacksquare/crossword.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,8 +768,8 @@ def _grid_html(self, size_px: Optional[int] = None) -> str:
table.xw{suffix} {{table-layout:fixed; background-color:white;width:{width}px;height:{height}px;}}
td.xw{suffix} {{outline: 2px solid black;outline-offset: -1px;position: relative;font-family: Arial, Helvetica, sans-serif;}}
tr.xw{suffix} {{background-color: white !important;}}
.number{suffix} {{position: absolute;top: 2px;left: 2px;font-size: {num_font}px;font-weight: normal;user-select: none;}}
.value{suffix} {{position: absolute;bottom:0;left: 50%;font-weight: bold;font-size: {val_font}px; transform: translate(-50%, 0%);}}
.number{suffix} {{position: absolute;top: 2px;left: 2px;font-size: {num_font}px;font-weight: normal;user-select: none; color: black;}}
.value{suffix} {{position: absolute;bottom:0;left: 50%;font-weight: bold;font-size: {val_font}px; transform: translate(-50%, 0%); color: black;}}
.black{suffix} {{background-color: black;}}
.gray{suffix} {{background-color: lightgrey;}}
.circle{suffix} {{position: absolute; border-radius: 50%; border: 1px solid black; right: 0px; left: 0px; top: 0px; bottom: 0px;}}
Expand Down
31 changes: 20 additions & 11 deletions src/blacksquare/word_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io
import re
from collections import defaultdict
from functools import lru_cache
from functools import lru_cache, cached_property
from importlib.resources import files
from pathlib import Path
from typing import TYPE_CHECKING, Callable, Dict, List, NamedTuple, Optional, Union
Expand Down Expand Up @@ -118,13 +118,13 @@ def __init__(
self._words, self._scores = norm_words, norm_scores

word_scores_by_length = defaultdict(lambda: ([], []))
for w, s in zip(norm_words, norm_scores):
word_scores_by_length[len(w)][0].append(w)
word_scores_by_length[len(w)][1].append(s)
for word, score in zip(norm_words, norm_scores):
word_scores_by_length[len(word)][0].append(word)
word_scores_by_length[len(word)][1].append(score)

self._word_scores_by_length = {
l: (np.array(words, dtype=str), np.array(scores))
for l, (words, scores) in word_scores_by_length.items()
length: (np.array(words, dtype=str), np.array(scores))
for length, (words, scores) in word_scores_by_length.items()
}

def find_matches(self, word: Word) -> MatchWordList:
Expand Down Expand Up @@ -170,12 +170,16 @@ def find_matches_str(self, query: str) -> MatchWordList:
len(query_array), np.empty((0,), dtype=str), np.empty((0,), dtype=float)
)

@property
def words(self) -> List[str]:
@cached_property
def words(self) -> list[str]:
return list(self._words)

@cached_property
def _words_set(self) -> set[str]:
return set(self._words)

@property
def scores(self) -> List[float]:
@cached_property
def scores(self) -> list[float]:
return list(self._scores)

def get_score(self, word: str) -> Optional[float]:
Expand All @@ -194,7 +198,7 @@ def get_score(self, word: str) -> Optional[float]:
else:
return None

@property
@cached_property
def frame(self) -> pd.DataFrame:
return pd.DataFrame({"word": self._words, "score": self._scores})

Expand All @@ -209,6 +213,9 @@ def score_filter(self, threshold: float) -> WordList:
"""
score_mask = self._scores >= threshold
return WordList(dict(zip(self._words[score_mask], self._scores[score_mask])))

def filter(self, filter_fn: Callable[[ScoredWord], bool]) -> WordList:
return WordList(dict([w for w in self if filter_fn(w)]))

def __len__(self):
return len(self._words)
Expand Down Expand Up @@ -239,6 +246,8 @@ def __add__(self, other):
{w: s for w, s in zip(self.words + other.words, self.scores + other.scores)}
)

def __contains__(self, item):
return item in self._words_set

class MatchWordList(WordList):
"""An object representing a WordList matching an open word. This class should not be
Expand Down

0 comments on commit 0d4420b

Please sign in to comment.