-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsym.py
143 lines (134 loc) · 5.27 KB
/
sym.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import click, random, string, sys
with open('dictionary.txt', 'rt') as input_file:
words = input_file.read().splitlines()
a, c, n, p, v = [], [], [], [], []
posList = [a, c, n, p, v] # Meta-list of all part-of-speech lists
textLabels = ['A:', 'C:', 'N:', 'P:', 'V:']
varLabels = ['a', 'c', 'n', 'p', 'v']
alphas = list(string.ascii_lowercase) # List of all lc alpha characters
d1 = dict(zip(textLabels, posList))
d2 = dict(zip(varLabels, posList))
for line in words: # Add lines in dictionary file to pos lists
for x1, y1 in d1.items():
if x1 in line:
y1.append(line.strip())
@click.group()
def cli():
pass
@cli.command()
@click.argument('pattern')
@click.option('--output', '-o', default='output.txt',
help='Specify output file name.')
@click.option('--stanzas', '-s', default=1,
help='Number of stanzas to generate.')
@click.option('--wait', '-w', is_flag=True,
help='Wait for input after each line.')
def poet(pattern, output, stanzas, wait):
"""Utility to cultivate poetic ideas.
Generate word(s) arranged in PATTERN.\n
PATTERN is a string including any combination of:\n
adjective/adverb: a; conjunction/preposition: c; interjection: i; noun: n;
pronoun: p; spoken contraction: s; verb: v; any previous: x\n
Like this: python sym.py poet avnx -s 2 -w -o mypoem.txt"""
inPoem, outPoem = [], []
for stanza in range(0, stanzas):
for letters in pattern:
if any(z in letters for z in d2.keys()):
for x2, y2 in d2.items():
if x2 in letters:
inPoem.append(random.choice(y2))
elif 'x' in letters:
inPoem.append(random.choice(random.choice(posList)))
else:
inPoem.append('INVALID PART OF SPEECH')
inPoem.append('')
if wait:
click.echo()
for item in inPoem:
if item == '':
click.echo(item)
outPoem.append(item)
else:
usrInput = input(item + '\n')
outPoem.append(item)
outPoem.append(usrInput)
if usrInput == '*':
click.echo()
sys.exit()
else:
click.echo()
for item in inPoem:
click.echo(item)
outPoem.append(item)
if output:
if output == 'dictionary.txt':
output = input('Please choose a different output filename: ')
with open(output, 'w') as output_file:
output_file.write('\n'.join(outPoem))
@cli.command()
@click.argument('partofspeech')
@click.argument('letters')
@click.option('--all', '-a', is_flag=True,
help='Return all possible words.')
@click.option('--begin', '-b', is_flag=True,
help='Specify if word(s) must begin with LETTERS')
@click.option('--end', '-e', is_flag=True,
help='Specify if word(s) must end with LETTERS')
@click.option('--output', '-o', default='output.txt',
help='Specify output file name (only for --all).')
def find(partofspeech, letters, all, begin, end, output):
"""Utility to identify potential words.
Generate word(s) from PARTOFSPEECH category with starting LETTERS.\n
PARTOFSPEECH must include exactly one of:\n
adjective/adverb: a; conjunction/preposition: c; interjection: i; noun: n;
pronoun: p; spoken contraction: s; verb: v; any previous: x\n
Like this: python sym.py find n intro -b -a -o mywords.txt"""
selectWords = []
click.echo()
if partofspeech == 'x':
for pos in posList:
for word in pos:
if begin:
if word.startswith(letters):
selectWords.append(word)
elif end:
token = word.split()
if token[0].endswith(letters):
selectWords.append(word)
else:
if letters in word:
selectWords.append(word)
elif partofspeech in varLabels:
if letters.isalpha():
for x2, y2 in d2.items():
if partofspeech == x2:
for word in y2:
if begin:
if word.startswith(letters):
selectWords.append(word)
elif end:
token = word.split()
if token[0].endswith(letters):
selectWords.append(word)
else:
if letters in word:
selectWords.append(word)
if len(selectWords) == 0:
click.echo('No results. Try a less specific query.')
elif all:
trimmedWords = set(selectWords)
finalWords = sorted(trimmedWords)
for item in finalWords:
click.echo(item)
click.echo()
if output:
if output == 'dictionary.txt':
output = input('Please choose a different output filename: ')
with open(output, 'w') as output_file:
output_file.write('\n'.join(finalWords))
else:
finalWords = random.choice(selectWords)
click.echo(finalWords)
click.echo()
if __name__ == '__main__':
cli()