|
|
|
@ -1,11 +1,13 @@ |
|
|
|
#!/usr/bin/python3 |
|
|
|
import argparse |
|
|
|
from string import ascii_lowercase |
|
|
|
import cmd2 |
|
|
|
import re |
|
|
|
import argparse |
|
|
|
import itertools |
|
|
|
import os |
|
|
|
import re |
|
|
|
|
|
|
|
import cmd2 |
|
|
|
|
|
|
|
|
|
|
|
from candidates import candidates, filter_possibilities |
|
|
|
from dictionary import DEFAULT, ALTERNATE |
|
|
|
from word_remove_dialog import RemoveWordsActivity |
|
|
|
|
|
|
|
@ -14,7 +16,7 @@ LOWERCASE = set(ascii_lowercase) |
|
|
|
DICTS = {"default": DEFAULT, "alt": ALTERNATE} |
|
|
|
# argparsers |
|
|
|
dictionary_manage_parser = argparse.ArgumentParser() |
|
|
|
commands = dictionary_manage_parser.add_mutually_exclusive_group() |
|
|
|
commands = dictionary_manage_parser.add_mutually_exclusive_group(required=True) |
|
|
|
commands.add_argument( |
|
|
|
"-l", "--list", action="store_true", help="List available dictionaries" |
|
|
|
) |
|
|
|
@ -27,7 +29,7 @@ guessing = argparse.ArgumentParser() |
|
|
|
guessing.add_argument("pattern", help="Pattern to match") |
|
|
|
|
|
|
|
hide_exclude = argparse.ArgumentParser() |
|
|
|
commands = hide_exclude.add_mutually_exclusive_group() |
|
|
|
commands = hide_exclude.add_mutually_exclusive_group(required=True) |
|
|
|
commands.add_argument( |
|
|
|
"-e", |
|
|
|
"--edit", |
|
|
|
@ -45,6 +47,16 @@ 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): |
|
|
|
"""Loop for wordscape commands |
|
|
|
|
|
|
|
@ -57,6 +69,7 @@ class MainLoop(cmd2.Cmd): |
|
|
|
self.excludes = set() |
|
|
|
self.hidden = set() |
|
|
|
self.init_letters(input("Enter letters: ")) |
|
|
|
self._candidates = None |
|
|
|
super().__init__() |
|
|
|
|
|
|
|
def init_letters(self, letters): |
|
|
|
@ -65,7 +78,17 @@ class MainLoop(cmd2.Cmd): |
|
|
|
letters = letters.lower() |
|
|
|
self.letters = [l for l in letters if l in LOWERCASE] |
|
|
|
self.prompt = MainLoop.prompt.format(", ".join(self.letters)) |
|
|
|
self.candidates = candidates(self.letters, DICTS[self.dict], self.excludes) |
|
|
|
self._candidates = None |
|
|
|
|
|
|
|
@property |
|
|
|
def candidates(self): |
|
|
|
if self._candidates is None: |
|
|
|
self._candidates = candidates(self.letters) |
|
|
|
return self._candidates |
|
|
|
|
|
|
|
def filter(self, regex): |
|
|
|
matching_pattern = filter(regex.match, self.candidates) |
|
|
|
return DICTS[self.dict].filter(matching_pattern) |
|
|
|
|
|
|
|
@cmd2.with_argparser(dictionary_manage_parser) |
|
|
|
def do_dict(self, args): |
|
|
|
@ -92,7 +115,7 @@ class MainLoop(cmd2.Cmd): |
|
|
|
def do_find(self, args): |
|
|
|
"""Find words that match a pattern""" |
|
|
|
pattern = re.compile(args.pattern + "$") |
|
|
|
matching_words = filter_possibilities(self.candidates, pattern) |
|
|
|
matching_words = self.filter(pattern) |
|
|
|
app = RemoveWordsActivity(matching_words - self.hidden, self.excludes) |
|
|
|
if os.name == "nt": |
|
|
|
app.run(fork=False) |
|
|
|
|