Exemplo n.º 1
0
def test_wordmap_answers():
    map = WordMap(words=_test_dictionary)
    assert valid_word_permutations(map, "tac") == [
        PermutationOption(word="tca", minimum_distance=3),
        PermutationOption(word="atc", minimum_distance=3),
        PermutationOption(word="tac", minimum_distance=2),
        PermutationOption(word="act", minimum_distance=2),
        PermutationOption(word="cta", minimum_distance=2),
    ]
    assert valid_word_permutations(map, "notword") == []
Exemplo n.º 2
0
def valid_word_permutations(
    word_map: WordMap, word: Optional[str]
) -> List[PermutationOption]:
    if word is None:
        return []
    options = []
    answers = word_map.correct_answers(word)
    if len(answers) == 0:
        return []
    for s in permutations(word):
        candidate = "".join(s)
        if not word_map.is_word(candidate):
            options.append(
                PermutationOption(
                    word=candidate,
                    minimum_distance=min(
                        map(lambda word: textdistance.hamming(candidate, word), answers)
                    ),
                )
            )
        if len(options) > 1000:
            break
    options.sort(key=lambda opt: -opt.minimum_distance)
    return options
Exemplo n.º 3
0
def trick_anagram_permutations(
    bucket_words: BucketedWords, difficulty: int, word_map: WordMap
) -> Optional[str]:
    for _ in range(0, 10):
        word = anagram_permutations(bucket_words, difficulty, word_map)
        if word is None:
            return None
        letters = _common_letters.copy()
        random.shuffle(letters)
        while len(letters) > 0:
            candidate = word + letters.pop()
            if word_map.is_knapsack_word(candidate):
                continue
            return candidate
    # This is unlikely, but possible. Could refactor to walk through difficulties?
    return None
Exemplo n.º 4
0
    def __init__(self, word_map: WordMap):
        """
            Initializes the difficulty buckets using a function and a wordmap.
            Args:

            bucketfunc: Callable(int, int) -> int
                A function that takes an integer for the length of the string
                 and an integer for the number of answers and returns an integer 0-9
            word_map: WordMap
                A word map object to allocate difficulties to
        """
        self._build_histograms(word_map)
        self.buckets: List[List[str]] = [[] for _ in range(0, 10)]
        for _, (knapsack, answers) in enumerate(word_map.word_map.items()):
            if len(answers) == 0:  # these words have no anagrams
                continue
            s = word_map.letters_from_key(knapsack)
            diff = self.difficulty(len(s), len(answers))
            # print(len(s), len(answers), difficulty)
            self.buckets[diff].append(s)
Exemplo n.º 5
0
class AnagramGame(object):
    def __init__(self, words=None, dict_file="/usr/share/dict/words"):
        self.word_map = WordMap(wordfile=dict_file, words=words)
        self.buckets = BucketedWords(self.word_map)

    def get_puzzle(self, difficulty: int):
        if difficulty < 0 or difficulty > 8:
            raise Exception(
                "get_puzzle must be called with a difficulty between 1-8")

        puzzle = anagram_permutations(self.buckets, difficulty, self.word_map)
        return AnagramPuzzle(word=puzzle,
                             answers=self.word_map.correct_answers(puzzle))

    def get_trick_puzzle(self, difficulty: int):
        if difficulty < 0 or difficulty > 8:
            raise Exception(
                "get_trick_puzzle must be called with a difficulty between 1-8"
            )

        puzzle = trick_anagram_permutations(self.buckets, difficulty,
                                            self.word_map)
        return AnagramTrickPuzzle(word=puzzle)
Exemplo n.º 6
0
def test_simple_wordmap_answers():
    map = WordMap(words=_test_dictionary)
    buckets = BucketedWords(map)
    assert [len(i) for i in buckets.buckets] == [0, 0, 0, 0, 0, 1, 1, 0, 1, 0]
Exemplo n.º 7
0
def test_wordmap_answers():
    map = WordMap()
    buckets = BucketedWords(map)
    print([len(i) for i in buckets.buckets])
    for i in range(0, 8):
        assert len(buckets.buckets[i]) > 100
Exemplo n.º 8
0
def test_wordmap_words():
    map = WordMap(words=_test_dictionary)
    assert map.is_word("cat")
    assert not map.is_word("cta")
Exemplo n.º 9
0
def test_wordmap_answers():
    map = WordMap(words=_test_dictionary)
    assert map.correct_answers("cat") == ["cat"]
    assert map.correct_answers("aligned") == ["aligned", "dealing", "leading"]
    assert map.correct_answers("arrest") == ["arrest", "rarest", "raters", "starer"]
    assert map.correct_answers("srrtae") == ["arrest", "rarest", "raters", "starer"]
Exemplo n.º 10
0
def test_trick_anagram_permutations():
    random.seed(1)
    map = WordMap(words=_test_dictionary)
    buckets = BucketedWords(map)
    word = trick_anagram_permutations(buckets, 6, map)
    assert set(word) == set("tcas")
Exemplo n.º 11
0
def test_anagram_permutations():
    map = WordMap(words=_test_dictionary)
    buckets = BucketedWords(map)
    word = anagram_permutations(buckets, 6, map)
    assert word != "cat"
    assert set(word) == set("cat")
Exemplo n.º 12
0
 def __init__(self, words=None, dict_file="/usr/share/dict/words"):
     self.word_map = WordMap(wordfile=dict_file, words=words)
     self.buckets = BucketedWords(self.word_map)