-
Notifications
You must be signed in to change notification settings - Fork 0
/
shitpost.py
executable file
·81 lines (68 loc) · 2.23 KB
/
shitpost.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
#!/usr/bin/env python
from __future__ import print_function
import sys
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.history import InMemoryHistory
from prompt_toolkit.shortcuts import prompt
from generate import model
trim_word = lambda x: ' '.join(x.split(' ')[:2])
class MyCompleter(Completer):
def get_completions(self, document, complete_event=None):
choices = set()
text = document.text_before_cursor
if len(text.rstrip(' ')):
last2 = text.strip(' ').split(' ')[-1]
spacing = len(text) - len(text.rstrip(' '))
for _ in range(0, 10):
try:
#print('last2', last2)
res = model.make_sentence_with_start(last2, strict=False, tries=50)
except KeyError:
continue
if res:
res = trim_word(res)
if res not in choices:
choices.add(res)
yield Completion(res, -(spacing + len(last2)))
else:
for _ in range(0, 10):
res = model.make_sentence()
if res:
res = trim_word(res)
if res not in choices:
choices.add(res)
yield Completion(res, 0)
def main():
options = {
'history': InMemoryHistory(),
'completer': MyCompleter(),
'message': u'shitpost> ',
}
isatty = sys.stdin.isatty()
if isatty:
def get_command():
return prompt(**options)
else:
get_command = sys.stdin.readline
while True:
try:
command = get_command()
except KeyboardInterrupt:
continue
except EOFError:
break
if not command:
if isatty:
continue
else:
break
if command[0] == '!':
command = command[1:].split(' ')
if command[0].isdigit():
N = int(command[0])
res = model.make_short_sentence(N)
print(res)
else:
print("Result =", command)
if __name__ == "__main__":
main()