|
|
@ -1,19 +1,18 @@ |
|
|
#!/usr/bin/python3 |
|
|
#!/usr/bin/python3 |
|
|
from string import ascii_lowercase |
|
|
from string import ascii_lowercase |
|
|
import argparse |
|
|
import argparse |
|
|
import itertools |
|
|
|
|
|
import os |
|
|
import os |
|
|
import re |
|
|
import re |
|
|
|
|
|
|
|
|
import cmd2 |
|
|
import cmd2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from dictionary import DEFAULT, ALTERNATE |
|
|
|
|
|
|
|
|
from candidate_cache import CandidateCache |
|
|
|
|
|
from dictionary import DICTS |
|
|
from word_remove_dialog import RemoveWordsActivity |
|
|
from word_remove_dialog import RemoveWordsActivity |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOWERCASE = set(ascii_lowercase) |
|
|
LOWERCASE = set(ascii_lowercase) |
|
|
DICTS = {"default": DEFAULT, "alt": ALTERNATE} |
|
|
|
|
|
|
|
|
|
|
|
# argparsers |
|
|
# argparsers |
|
|
dictionary_manage_parser = argparse.ArgumentParser() |
|
|
dictionary_manage_parser = argparse.ArgumentParser() |
|
|
commands = dictionary_manage_parser.add_mutually_exclusive_group(required=True) |
|
|
commands = dictionary_manage_parser.add_mutually_exclusive_group(required=True) |
|
|
@ -47,16 +46,6 @@ commands.add_argument( |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def candidates(letters, min=2): |
|
|
|
|
|
possibilities = [] |
|
|
|
|
|
for length in range(min, len(letters) + 1): |
|
|
|
|
|
for comb in itertools.combinations(letters, length): |
|
|
|
|
|
for perm in itertools.permutations(comb): |
|
|
|
|
|
word = "".join(perm) |
|
|
|
|
|
possibilities.append(word) |
|
|
|
|
|
return possibilities |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MainLoop(cmd2.Cmd): |
|
|
class MainLoop(cmd2.Cmd): |
|
|
"""Loop for wordscape commands |
|
|
"""Loop for wordscape commands |
|
|
|
|
|
|
|
|
@ -69,7 +58,7 @@ class MainLoop(cmd2.Cmd): |
|
|
self.excludes = set() |
|
|
self.excludes = set() |
|
|
self.hidden = set() |
|
|
self.hidden = set() |
|
|
self.init_letters(input("Enter letters: ")) |
|
|
self.init_letters(input("Enter letters: ")) |
|
|
self._candidates = None |
|
|
|
|
|
|
|
|
self.cache = CandidateCache() |
|
|
super().__init__() |
|
|
super().__init__() |
|
|
|
|
|
|
|
|
def init_letters(self, letters): |
|
|
def init_letters(self, letters): |
|
|
@ -78,17 +67,14 @@ class MainLoop(cmd2.Cmd): |
|
|
letters = letters.lower() |
|
|
letters = letters.lower() |
|
|
self.letters = [l for l in letters if l in LOWERCASE] |
|
|
self.letters = [l for l in letters if l in LOWERCASE] |
|
|
self.prompt = MainLoop.prompt.format(", ".join(self.letters)) |
|
|
self.prompt = MainLoop.prompt.format(", ".join(self.letters)) |
|
|
self._candidates = None |
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def candidates(self): |
|
|
def candidates(self): |
|
|
if self._candidates is None: |
|
|
|
|
|
self._candidates = candidates(self.letters) |
|
|
|
|
|
return self._candidates |
|
|
|
|
|
|
|
|
return self.cache.get(self.dict, self.letters) |
|
|
|
|
|
|
|
|
def filter(self, regex): |
|
|
def filter(self, regex): |
|
|
matching_pattern = filter(regex.match, self.candidates) |
|
|
|
|
|
return DICTS[self.dict].filter(matching_pattern) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return set(filter(regex.match, self.candidates)) |
|
|
|
|
|
|
|
|
@cmd2.with_argparser(dictionary_manage_parser) |
|
|
@cmd2.with_argparser(dictionary_manage_parser) |
|
|
def do_dict(self, args): |
|
|
def do_dict(self, args): |
|
|
|