Merge branch 'pytorch_clear_actions' & 'suggestion loop' into pytorch

This commit is contained in:
AccidentallyOnPurpose
2019-12-24 10:32:46 +08:00
2 changed files with 26 additions and 19 deletions
+1
View File
@@ -97,4 +97,5 @@ query = 7;42
ai-text = 37
main-prompt = 34
user-text = 36
transformed-user-text = 36
print-story = 37
+25 -19
View File
@@ -25,16 +25,27 @@ with open(Path('interface', 'clover'), 'r', encoding='utf-8') as file:
#ECMA-48 set graphics codes for the curious. Check out "man console_codes"
def colPrint(str, col='0', wrap=True):
def colPrint(str, col='0', wrap=True, end=None):
if wrap and settings.getint('text-wrap-width') > 1:
str = textwrap.fill(str, settings.getint('text-wrap-width'), replace_whitespace=False)
print("\x1B[{}m{}\x1B[{}m".format(col, str, colors["default"]))
print("\x1B[{}m{}\x1B[{}m".format(col, str, colors["default"]), end=end)
def colInput(str, col1=colors["default"], col2=colors["default"]):
val=input("\x1B[{}m{}\x1B[0m\x1B[{}m".format(col1,str,col1))
print('\x1B[0m', end='')
return val
def clear_lines(n):
"""Clear the last line in the terminal."""
screen_code = "\033[1A[\033[2K" # up one line, and clear line
for _ in range(n):
print(screen_code, end="")
def count_printed_lines(text):
"""For a prompt, work out how many console lines it took up with wrapping."""
width = settings.getint("text-wrap-width")
return sum([(len(ss) // width) + 1 for ss in text.split("\n")])
def getNumberInput(n):
val=colInput("Enter a number from above (default 0):", colors["selection-prompt"], colors["selection-value"])
if val=='':
@@ -93,21 +104,6 @@ class AIPlayer:
prompt, generate_num=settings.getint('action-generate-num'), temperature=settings.getint('action-temp'))
return clean_suggested_action(result_raw, min_length=settings.getint('action-min-length'))
def get_actions(self, prompt):
suggested_actions = [
self.get_action(prompt)
for _ in range(settings.getint('action-alternatives'))
]
logger.debug("Suggested actions before filter and dedup %s", suggested_actions)
#remove short ones
suggested_actions = [
s
for s in suggested_actions
if len(s) > settings.getint('action-min-length')
]
#remove dups
suggested_actions = list(set(suggested_actions))
return suggested_actions
def play():
generator = getGenerator()
@@ -171,17 +167,26 @@ def play():
) + "\n>"
suggested_actions = []
colPrint('Suggested actions:', colors['selection-value'])
action_suggestion_lines = 1
for i in range(settings.getint('action-alternatives')):
suggested_action = ai_player.get_action(action_prompt)
suggested_actions.append(suggested_action)
colPrint('{}> {}'.format(i, suggested_action), colors['selection-value'])
suggestion = '{}> {}'.format(i, suggested_action)
colPrint(suggestion, colors['selection-value'])
action_suggestion_lines += count_printed_lines(suggestion)
print()
if settings.getboolean('console-bell'):
print('\x07', end='')
action = colInput("> ", colors["main-prompt"], colors["user-text"])
#TODO:Clear suggestions and user input
# Clear suggestions and user input
if settings.getint('action-alternatives') > 0:
action_suggestion_lines += count_printed_lines('> '+action) + 1
clear_lines(action_suggestion_lines)
# Show user input again
colPrint("\n> " + action.rstrip(), colors["user-text"], end="")
setRegex = re.search('^set ([^ ]+) ([^ ]+)$', action)
if setRegex:
@@ -247,6 +252,7 @@ def play():
action = "\n> " + action + "\n"
colPrint("\n>> " + action.lstrip(), colors["transformed-user-text"])
result = "\n" + story_manager.act(action)
if len(story_manager.story.results) >= 2:
similarity = get_similarity(