forked from mobeets/imperatives
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imperatives_random.py
executable file
·140 lines (118 loc) · 3.5 KB
/
imperatives_random.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
#!/usr/bin/env python
# encoding=utf-8
import re
import random
import argparse
# http://www.clips.ua.ac.be/pages/pattern-en
from pattern.en import wordnet, conjugate, pluralize, singularize, quantify
from pattern.en.wordlist import BASIC
singled_if_word = lambda x: singularize(x) if wordnet.synsets(singularize(x)) else None
singled_if_word_2 = lambda x: x[:-1] if wordnet.synsets(x[:-1]) else None
SOURCE = 2
if SOURCE == 0:
NOUNS = wordnet.NOUNS.keys()
VERBS = wordnet.VERBS.keys()
ADJS = wordnet.ADJECTIVES.keys()
elif SOURCE == 1:
basic_words = lambda pos: [w for w in BASIC if wordnet.synsets(w, pos=pos)]
NOUNS = basic_words('NN')
VERBS = basic_words('VB')
ADJS = basic_words('JJ')
else:
# http://dictionary-thesaurus.com/wordlists.html
load = lambda filename: [w.lower().strip() for w in open(filename).read().split('\n') if w.lower().strip() and wordnet.synsets(w.lower().strip())]
NOUNS = load('nouns.txt')
NOUNS = [singled_if_word(x) if x.endswith('s') else x for x in NOUNS if not x.endswith('s') or singled_if_word(x)]
VERBS = load('verbs.txt')
ADJS = load('adjectives.txt')
A1 = ['start', 'stop']
A2 = ['always', 'never']
A3 = ['occasionally', 'constantly']
C = ['the', 'the', 'all those', 'so many']
C2 = ['the', 'the', 'all those']
coin_flip = lambda p: random.random() < p
def get_related_noun_or_not(noun, d=True):
w = wordnet.synsets(noun)
if w:
w = w[0]
w1 = w.hyponyms()
w2 = w.hypernyms()
if w1 + w2:
nw = random.choice(w1 + w2)
if nw and nw.senses:
return nw.senses[0]
elif wordnet.synsets(singularize(noun)) and d:
return get_related_noun_or_not(singularize(noun, False))
return noun
def random_imperative(noun=None):
if noun:
n = get_related_noun_or_not(noun)
else:
n = random.choice(NOUNS)
v = random.choice(VERBS)
c = ''
if coin_flip(0.5):
adj = random.choice(ADJS)
else:
adj = ''
if coin_flip(0.7):
n = pluralize(n)
c = random.choice(C2)
else:
i = random.randint(1, 5)
n = quantify(adj + ' ' + n, amount=i)
adj = ''
if coin_flip(0.25):
a = ''
v = conjugate(v)
elif coin_flip(0.33):
v = conjugate(v, 'part') # present participle
a = random.choice(A1)
c = random.choice(C)
elif coin_flip(0.5):
v = conjugate(v)
a = random.choice(A2)
else:
v = conjugate(v)
a = random.choice(A3)
phrase = '{0} {1} {2} {3} {4}'.format(a, v, c, adj, n)
phrase = phrase[1:] if phrase.startswith(' ') else phrase
return re.sub(' +', ' ', phrase)
def add_qualifier(phrase):
n = random.choice(NOUNS)
v = random.choice(VERBS)
if coin_flip(0.5):
a = 'cannot'
else:
a = 'can'
if coin_flip(0.5):
b = 'of'
else:
b = 'for the'
n = pluralize(n)
v = conjugate(v)
qual = '{0} you {1} {2}'.format(n, a, v)
return '{0} {1} {2}'.format(phrase, b, qual)
def protect_against_plurals(word):
wd = word
if word.endswith('s'):
wd = singled_if_word(word) if singled_if_word(word) else word
if wd == word:
wd = singled_if_word_2(word) if singled_if_word_2(word) else word
return wd
def main(N=50, subject=None):
if subject:
subject = protect_against_plurals(subject)
for _ in xrange(N):
if coin_flip(0.5):
imp = random_imperative(subject)
else:
imp = add_qualifier(random_imperative(subject))
print imp.capitalize() + '.'
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--N", default=50, type=int, help="The number of imperatives to generate.")
parser.add_argument("--S", default=None, type=str, help="The related subject of the imperatives.")
args = parser.parse_args()
if __name__ == '__main__':
main(args.N, args.S)