def file( path, start_line, end_line, n_lines, random_state, force_perfect, instant_death, output_file, target_wpm, include_whitespace, ): # noqa: D400 """Type text from a file""" # noqa: D400 import numpy as np from mltype.interactive import main_basic # validation mode_exact = start_line is not None and end_line is not None mode_random = n_lines is not None if not (mode_exact ^ mode_random): raise ValueError("One can either provide start and end " "line or the n_lines to be randomly selected.") all_lines = [line for line in path.readlines()] if not include_whitespace: all_lines = [f"{line.strip()} " for line in all_lines] n_all_lines = len(all_lines) if mode_exact: if random_state is not None: raise ValueError( "One can only use random state in combination with n-lines") if not 0 <= start_line < end_line < len(all_lines): raise ValueError( f"Selected lines fall outside of the range (0, {n_all_lines})" " or are in a wrong order.") if mode_random: if random_state is not None: np.random.seed(random_state) start_line = np.random.randint(n_all_lines - n_lines) end_line = start_line + n_lines selected_lines = all_lines[start_line:end_line] main_basic( "".join(selected_lines), force_perfect=force_perfect, output_file=output_file, instant_death=instant_death, target_wpm=target_wpm, )
def test_strip(monkeypatch): fake_curses = Mock() tt = TypedText("Hello") fake_curses.wrapper.return_value = tt monkeypatch.setattr("mltype.interactive.curses", fake_curses) fake_text = MagicMock(spec=str) main_basic(fake_text, None, None, None, None) fake_text.rstrip.assert_called_once()
def sample( model_name, n_chars, force_perfect, instant_death, random_state, output_file, target_wpm, verbose, starting_text, top_k, ): # noqa: D400 """Sample text from a language""" sys.modules["mlflow"] = None from mltype.interactive import main_basic from mltype.ml import load_model, sample_text from mltype.utils import get_cache_dir, get_config_file cp = get_config_file() try: predefined_path = cp["general"]["models_dir"] languages_dir = get_cache_dir(predefined_path) except KeyError: languages_dir = get_cache_dir() / "languages" model_path = languages_dir / model_name network, vocabulary = load_model(model_path) text = sample_text( n_chars, network, vocabulary, initial_text=starting_text, random_state=random_state, top_k=top_k, verbose=verbose, ) main_basic( text, force_perfect=force_perfect, output_file=output_file, instant_death=instant_death, target_wpm=target_wpm, )
def raw(text, force_perfect, output_file, instant_death, target_wpm, raw_string): # noqa: D400 """Provide text manually""" import codecs from mltype.interactive import main_basic if not raw_string: text = codecs.decode(text, "unicode_escape") main_basic( text, force_perfect=force_perfect, output_file=output_file, instant_death=instant_death, target_wpm=target_wpm, )
def random(characters, force_perfect, instant_death, n_chars, output_file, target_wpm): # noqa: D400 """Sample characters randomly from a vocabulary""" import numpy as np from mltype.interactive import main_basic c = Counter(characters) vocabulary = list(c.keys()) counts = np.array([c[x] for x in vocabulary]) p = counts / counts.sum() text = "".join(np.random.choice(vocabulary, size=n_chars, p=p)) main_basic( text, force_perfect=force_perfect, output_file=output_file, instant_death=instant_death, target_wpm=target_wpm, )