adding text samples to list command

This commit is contained in:
evilchili 2023-11-25 17:18:18 -05:00
parent e72b79d28e
commit 27e64af308
2 changed files with 4 additions and 72 deletions

View File

@ -1,6 +1,6 @@
import logging
import os
import random
import textwrap
from enum import Enum
from types import ModuleType
@ -9,7 +9,6 @@ import language
import typer
from rich.logging import RichHandler
from rich.console import Console
from rich.markdown import Markdown
app = typer.Typer()
@ -58,23 +57,7 @@ def main(
@app.command()
def text(count: int = typer.Option(50, help="The number of words to generate.")):
phrases = []
phrase = []
for word in app_state["language"].Language.word(count):
phrase.append(str(word))
if len(phrase) >= random.randint(1, 12):
phrases.append(' '.join(phrase))
phrase = []
if phrase:
phrases.append(' '.join(phrase))
paragraph = phrases[0].capitalize()
for phrase in phrases[1:]:
if random.choice([0, 0, 1]):
paragraph = paragraph + random.choice('?!.') + ' ' + phrase.capitalize()
else:
paragraph = paragraph + ', ' + phrase
paragraph = paragraph + random.choice('?!.')
paragraph = app_state["language"].Language.text(count)
console = Console(width=80)
console.print(paragraph)
@ -92,9 +75,9 @@ def names(
@app.command()
def list():
console = Console(width=80)
for lang, module in supported_languages.items():
console.print(Markdown(module.__doc__))
text = textwrap.shorten(module.Language.text(count=20), width=70, placeholder='')
print(f"{lang.title():15s}: {text}")
if __name__ == "__main__":

View File

@ -131,57 +131,6 @@ class Language:
)
>>> Common.word()
reibing
How Words Are Constructed:
The main interface for callers is word(), which returns a
randomly-generated word in the language according to the following
algorithm:
1. Choose a random syllable from the syllable set
2. For each grapheme in the syllable
3. Choose a random grapheme template
4. Choose a random sequence from the language for that grapheme
5. Validate the word against the language rules
6. Repeat 1-5 until a valid word is generated
7. Add a prefix and suffix, if they are defined
The following graphemes are supported by default:
- vowel
- consonant
- prefix
- suffix
When graphemes are chosen, the following rules are applied:
- Every syllable must have at least one vowel
- A syllable may never have three consecutive consonants
How Words Are Validated:
Once a word has been constructed by populating syllable templates, it is
tested against one or more language rules.
The default rules are defined in language.rules.default_rules; they are:
- the word must contain at least one vowel
- the word must not contain 3 or more contiguous english vowels
- the word must not contain 3 or more contiguous english consonants
- the word must not consist of just one vowel, repeated
Since it is possible to craft Syllables resulting in grapheme
selections that rarely or never yield valid words, or rules that
reject every word, an ImprobableTemplateError will be thrown if
10 successive attempts to create a valid word fail.
Extending Languages:
Graphemes are populated by means of callbacks which select a member
of the associated weighted set at random. Graphemes can be any string,
so long as the Language class has a matching callback.
To add support for a new grapheme type, define a method on your
Language class called get_grapheme_TYPE, where TYPE is the string
used in your Syllable templates. Examine test cases in test_types.py
for examples.
"""
def __init__(