forked from stellarskylark/neoglot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neoglot.py
executable file
·84 lines (68 loc) · 2.19 KB
/
neoglot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
"""
The primary executable file for Neoglot. Uses ``click`` to
provide a command-line interface.
The function run() is not picked up by Sphinx because of ``click``,
but it is the actual command which the CLI runs.
"""
import click
import random
import sys
from parse import Parse
CATEGORIES = {}
SYLLABLES = {}
@click.command()
@click.argument("file")
@click.option('--count', default=20,
help='number of words to generate')
@click.option('--minsylls', default=1,
help='minimum number of syllables per word')
@click.option('--maxsylls', default=3,
help='maximum number of syllables per word')
def run(file, count, minsylls, maxsylls):
"""
Actually runs the generator. Note that all parameters are
supplied by the CLI through click.
"""
# Perform error checking
if minsylls > maxsylls:
click.echo("ERROR: minsylls cannot be greater than maxsylls")
sys.exit(2)
elif minsylls < 1:
click.echo("ERROR: minsylls must be greater than 1")
sys.exit(2)
elif count < 1:
click.echo("ERROR: count must be greater than 1")
sys.exit(2)
global CATEGORIES, SYLLABLES
parser = Parse(file)
CATEGORIES = parser.categories
SYLLABLES = parser.syllables
print_words(count, minsylls, maxsylls)
def print_words(count, minsylls, maxsylls):
"""Prints a number of valid words."""
for _ in range(0, count):
click.echo(gen_word(minsylls, maxsylls))
def gen_word(minsylls, maxsylls):
"""Generates a word with the specified number of syllables."""
word = ""
for _ in range(0, random.randint(minsylls, maxsylls)):
word += gen_syll()
return word
def gen_syll():
"""Generates a syllable with a random syllable structure."""
syll = ""
syllstruct = []
sylltype = random.choice(list(SYLLABLES.keys()))
for element in SYLLABLES[sylltype]:
syllstruct.append(random.choice(element))
for cat in syllstruct:
if cat == '':
continue
if cat in CATEGORIES.keys():
syll += random.choice(CATEGORIES[cat])
else:
syll += cat
return syll
if __name__ == '__main__':
run()