added code for reading and writing tree data

This commit is contained in:
nickwalton
2019-10-25 17:19:53 -06:00
parent 7e55a8ad21
commit c01eb5c91f
9 changed files with 169 additions and 85 deletions
+4
View File
@@ -0,0 +1,4 @@
context,story_block_1,previous_action,story_block_2␍
The world has ended. The cities are in ruin and few people are left alive.,"You go after him. As you try and stand up though you realize you're in extreme pain. You think one of your ribs is broken. You manage to get yourself on your feet, but realize catching him is impossible. ",look for something to wrap your chest with.,
The world has ended. The cities are in ruin and few people are left alive.,You go to the backyard of the house. There's a dirty grill and an overturned slide. There's a door to the house on the back porch.,open the back door into the house.,
The world has ended. The cities are in ruin and few people are left alive.,You get back on the road and continue your journey. After a few hours you realize how starving you are. You' really need to find a food.,look around to see what's around me.,
1 context story_block_1 previous_action story_block_2
2 The world has ended. The cities are in ruin and few people are left alive. You go after him. As you try and stand up though you realize you're in extreme pain. You think one of your ribs is broken. You manage to get yourself on your feet, but realize catching him is impossible. look for something to wrap your chest with.
3 The world has ended. The cities are in ruin and few people are left alive. You go to the backyard of the house. There's a dirty grill and an overturned slide. There's a door to the house on the back porch. open the back door into the house.
4 The world has ended. The cities are in ruin and few people are left alive. You get back on the road and continue your journey. After a few hours you realize how starving you are. You' really need to find a food. look around to see what's around me.
+21
View File
@@ -0,0 +1,21 @@
Prompt,You wake up in an old rundown hospital with no memory of how you got there. You look around and see ,,,
Story Continuation or Continuation Reference,broken equipement lying around the room. The hospital seems to be completely empty.,B16,C10,D22
Action,leave the hospital and see if there is anything outside.,ask the man what he wants and where everybody is.,leave the store and get back in your car.,eat a few granola bars.
Result,Outside the hospital you see a worn down road and empty cars. Everything is strangely quiet and you start getting worried. You feel hungry and wonder when the last time you ate was.,"He looks at you confused. ""What are you stupid? This is the end of the world man!"" he says.",You think about what the man said. How is this possible? Why were you in that hospital and what should you do now?,"You finish off the rest of your granola bars. You're full now, but are worried about what you'll eat when you get hungry again."
Action,try to find a working car.,"say ""What do you mean? I just woke up in a hospital with no memory. What happened?""",drive back to the hospital to see if you can find any clues. ,keep walking towards Clayton.
Result,You find a car with the keys in the ignition and try to start it. Nothing happens. You keep looking and find another car that looks like it might work. You manage to get the engine started. ,"He responds ""Like I said, the end of the world. When the bombs starting falling three years ago we thought we could pull through. But then the skies turned black, crops started dying and everyone went crazy. Most people died in the first couple months."" ",As you pull in to the hospital parking lot the car sputters and dies. It's out of fuel an slows to a halt. ,You walk for two hours and take a break. You've left the town you were in and are now in a more rural area. There's a farmhouse to your right and a forest on your left.
Action,drive around looking for any people or stores that might have supplies.,ask him how he managed to survive.,go back to the room you woke up in.,go in the farmhouse and look for food.
Result,"You drive around the town and see many boarded up buildings, but no people. Eventually you find a store that you think might have supplies.",He tells you that he was away hunting when it all started happening. He managed to live off the land for the first little while. But after the animals started dying he ran out of food and came to the city.,You get to the room. It's the same as how you remember it. Broken equipment eveywhere. But there's also a small notebook that you didn't notice before lying on the floor. ,"You try to open the farmhouse front door, but it's locked. There's a broken window on the right and a shed behind the house."
Action,go inside the store and see if there are any supplies.,ask him if you two can work together. ,read the notebook.,go through the window into the farmhouse.
Result,It looks like the store has been ransacked and all the shelves are empty. You manage to find a package of granola bars and a flashlight behind the counter.,"The man shakes his head. ""Hardly enough food for one person. I've only lived this long by looking after myself."" He turns and starts walking away.",It looks like a journal. As you flip through the pages you see drawings of the death and destruction. The journal talks about someone's horrific experiences during the aftermath of the war. ,"You break the rest of the glass in the window and try to carefully crawl through. You cut yourself on the edges as you're climbing in, but you make it inside. "
Action,eat a granola bar.,run after the man and tell him you'll both do better together.,try to see if anything in it sparks your memory.,search the farmhouse for food.
Result,You feel a little bit better. But you're still hungry.,"He stops and thinks about it for a second, then says, ""Fine. I guess there might be advantages to working together."" ","you read for a while hoping this will give you some clue. None of the events written about spark anything, but you do read someone's name that you feel like you should know. ""Sarina"". It says she heard about a community called ""Bright Meadow"" that was safe and had food and was leaving to find it. ",You search around the farmhouse for food. You find some thick sturdy shirts and some rusty tools but no food. It's getting dark outside. You think it might be a good idea to sleep the night here.
Action,eat another granola bar.,ask him where he thinks we should go next.,see if you can find out where Bright Meadow is from the journal. ,sleep the night in the farmhouse.
Result,You feel better. You should probably save the rest of your food though.,He tells you there is a house on the road that looked undisturbed that might be worth checking out. ,You look through the journal to see if there are any clues about where Bright Meadow is. You find a crude map that shows Bright Meadow to the north of a town called Clayton. ,You wake up in the middle of the night sweating from a nightmare. In it you saw a woman screaming in some dark place. You feel like you need to rescue her and that you're the only one who can help her.
Action,leave the store and get back in the car.,start going towards the house. ,go to Clayton.,go back to sleep till the morning.
Result,As you leave the store you notice movement out behind your car. A man steps out with a hungry look in his eyes. His clothes are in tatters and he is carrying a crowbar.,You are walking towards the house. You reach an intersection. There is a police station on one corner an office building on another. The house is another half mile down the road. ,You don't know where Clayton is. But maybe if you found a map you could figure out where it is.,"You try to go back to sleep, but can't shake the feelings from your dream. You decide you might as well get up and see if you can do anything productive. "
Action,run at the man and attack him.,keep going towards the house. ,look for a map somewhere in the hospital. ,look around the farmhouse for anything else useful.
Result,You run forward to attack the man. His crowbar crashes into your chest and you fall to the ground. He kicks you a few times and then grabs your granola bars and runs off.,You keep walking and eventually reach the house. You see a two story house that looks fairly undisturbed. There's a garage on the right and a path to the backyard on the left. ,You start looking around for a map. You find an administrator's office that you think might have one.,You look around the farmhouse for anything else useful. You don't find anything else useful.
Action,go after him.,go around the house to the backyard.,search the office for a map.,leave the farmhouse and continue walking towards Clayton.
Result,"You go after him. As you try and stand up though you realize you're in extreme pain. You think one of your ribs is broken. You manage to get yourself on your feet, but realize catching him is impossible. ",You go to the backyard of the house. There's a dirty grill and an overturned slide. There's a door to the house on the back porch.,You look through the filing cabinets for a map. You find a bunch of patient records in a filing cabinet. In the desk you find a map that shows other hospitals in the region. It says Clayton is 15 miles to the West of here. ,You get back on the road and continue your journey. After a few hours you realize how starving you are. You' really need to find a food.
Action,look for something to wrap your chest with.,open the back door into the house.,leave the hospital and start heading towards Clayton.,look around to see what's around me.
1 Prompt You wake up in an old rundown hospital with no memory of how you got there. You look around and see
2 Story Continuation or Continuation Reference broken equipement lying around the room. The hospital seems to be completely empty. B16 C10 D22
3 Action leave the hospital and see if there is anything outside. ask the man what he wants and where everybody is. leave the store and get back in your car. eat a few granola bars.
4 Result Outside the hospital you see a worn down road and empty cars. Everything is strangely quiet and you start getting worried. You feel hungry and wonder when the last time you ate was. He looks at you confused. "What are you stupid? This is the end of the world man!" he says. You think about what the man said. How is this possible? Why were you in that hospital and what should you do now? You finish off the rest of your granola bars. You're full now, but are worried about what you'll eat when you get hungry again.
5 Action try to find a working car. say "What do you mean? I just woke up in a hospital with no memory. What happened?" drive back to the hospital to see if you can find any clues. keep walking towards Clayton.
6 Result You find a car with the keys in the ignition and try to start it. Nothing happens. You keep looking and find another car that looks like it might work. You manage to get the engine started. He responds "Like I said, the end of the world. When the bombs starting falling three years ago we thought we could pull through. But then the skies turned black, crops started dying and everyone went crazy. Most people died in the first couple months." As you pull in to the hospital parking lot the car sputters and dies. It's out of fuel an slows to a halt. You walk for two hours and take a break. You've left the town you were in and are now in a more rural area. There's a farmhouse to your right and a forest on your left.
7 Action drive around looking for any people or stores that might have supplies. ask him how he managed to survive. go back to the room you woke up in. go in the farmhouse and look for food.
8 Result You drive around the town and see many boarded up buildings, but no people. Eventually you find a store that you think might have supplies. He tells you that he was away hunting when it all started happening. He managed to live off the land for the first little while. But after the animals started dying he ran out of food and came to the city. You get to the room. It's the same as how you remember it. Broken equipment eveywhere. But there's also a small notebook that you didn't notice before lying on the floor. You try to open the farmhouse front door, but it's locked. There's a broken window on the right and a shed behind the house.
9 Action go inside the store and see if there are any supplies. ask him if you two can work together. read the notebook. go through the window into the farmhouse.
10 Result It looks like the store has been ransacked and all the shelves are empty. You manage to find a package of granola bars and a flashlight behind the counter. The man shakes his head. "Hardly enough food for one person. I've only lived this long by looking after myself." He turns and starts walking away. It looks like a journal. As you flip through the pages you see drawings of the death and destruction. The journal talks about someone's horrific experiences during the aftermath of the war. You break the rest of the glass in the window and try to carefully crawl through. You cut yourself on the edges as you're climbing in, but you make it inside.
11 Action eat a granola bar. run after the man and tell him you'll both do better together. try to see if anything in it sparks your memory. search the farmhouse for food.
12 Result You feel a little bit better. But you're still hungry. He stops and thinks about it for a second, then says, "Fine. I guess there might be advantages to working together." you read for a while hoping this will give you some clue. None of the events written about spark anything, but you do read someone's name that you feel like you should know. "Sarina". It says she heard about a community called "Bright Meadow" that was safe and had food and was leaving to find it. You search around the farmhouse for food. You find some thick sturdy shirts and some rusty tools but no food. It's getting dark outside. You think it might be a good idea to sleep the night here.
13 Action eat another granola bar. ask him where he thinks we should go next. see if you can find out where Bright Meadow is from the journal. sleep the night in the farmhouse.
14 Result You feel better. You should probably save the rest of your food though. He tells you there is a house on the road that looked undisturbed that might be worth checking out. You look through the journal to see if there are any clues about where Bright Meadow is. You find a crude map that shows Bright Meadow to the north of a town called Clayton. You wake up in the middle of the night sweating from a nightmare. In it you saw a woman screaming in some dark place. You feel like you need to rescue her and that you're the only one who can help her.
15 Action leave the store and get back in the car. start going towards the house. go to Clayton. go back to sleep till the morning.
16 Result As you leave the store you notice movement out behind your car. A man steps out with a hungry look in his eyes. His clothes are in tatters and he is carrying a crowbar. You are walking towards the house. You reach an intersection. There is a police station on one corner an office building on another. The house is another half mile down the road. You don't know where Clayton is. But maybe if you found a map you could figure out where it is. You try to go back to sleep, but can't shake the feelings from your dream. You decide you might as well get up and see if you can do anything productive.
17 Action run at the man and attack him. keep going towards the house. look for a map somewhere in the hospital. look around the farmhouse for anything else useful.
18 Result You run forward to attack the man. His crowbar crashes into your chest and you fall to the ground. He kicks you a few times and then grabs your granola bars and runs off. You keep walking and eventually reach the house. You see a two story house that looks fairly undisturbed. There's a garage on the right and a path to the backyard on the left. You start looking around for a map. You find an administrator's office that you think might have one. You look around the farmhouse for anything else useful. You don't find anything else useful.
19 Action go after him. go around the house to the backyard. search the office for a map. leave the farmhouse and continue walking towards Clayton.
20 Result You go after him. As you try and stand up though you realize you're in extreme pain. You think one of your ribs is broken. You manage to get yourself on your feet, but realize catching him is impossible. You go to the backyard of the house. There's a dirty grill and an overturned slide. There's a door to the house on the back porch. You look through the filing cabinets for a map. You find a bunch of patient records in a filing cabinet. In the desk you find a map that shows other hospitals in the region. It says Clayton is 15 miles to the West of here. You get back on the road and continue your journey. After a few hours you realize how starving you are. You' really need to find a food.
21 Action look for something to wrap your chest with. open the back door into the house. leave the hospital and start heading towards Clayton. look around to see what's around me.
+128
View File
@@ -0,0 +1,128 @@
"""
format of tree is
dict {
tree_id: tree_id_text
context: context text?
story_prompt: story_prompt_text
story_continuation: story_continuation_text
action_results: [act_res1, act_res2, act_res3...]
}
where each action_result's format is:
dict{
action: action_text
result: result_text
action_results: [act_res1, act_res2, act_res3...]
}
"""
import csv
import json
def get_numeric_cell(cell_string):
letter = cell_string[0]
number = cell_string[1:]
if number.isnumeric():
col_num = ord(letter.lower()) - 97
row_num = int(number)
return row_num, col_num
else:
return None
def dungeon_data_to_tree(filename):
tree = {}
tree["tree_id"] = filename
current_action_results = []
tree["action_results"] = current_action_results
rows = []
with open(filename) as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for reader_row in reader:
rows.append(reader_row)
tree["story_prompt"] = rows[0][1]
col_ind = 1
n_columns = len(rows[0])
while col_ind < n_columns:
row_and_col = get_numeric_cell(rows[1][col_ind])
# if it's a cell designation got to traverse tree to find starting point
if row_and_col is not None:
cols_to_traverse = [row_and_col[1], col_ind]
current_action_results = tree["action_results"]
while cols_to_traverse[0] is not 1:
new_row_and_col = get_numeric_cell(rows[1][cols_to_traverse[0]])
cols_to_traverse.insert(0, new_row_and_col[1])
for j in range(len(cols_to_traverse) -1):
next_row_and_col = get_numeric_cell(rows[1][cols_to_traverse[j+1]])
traverse_rows = int((next_row_and_col[0] - 2) / 2)
for _ in range(traverse_rows):
current_action_results = current_action_results[-1]["action_results"]
else:
tree["story_continuation"] = rows[1][col_ind]
ind = 2
while(ind < len(rows)):
action_result = {"action": rows[ind][col_ind], "result": None, "action_results": []}
if ind+1 < len(rows):
action_result["result"] = rows[ind+1][col_ind]
current_action_results.append(action_result)
current_action_results = action_result["action_results"]
ind += 2
col_ind += 1
return tree
def build_action_samples_helper(context, story_block, action_results):
samples = []
for action_result in action_results:
if len(action_result["action_results"]) is 0:
row = [context, story_block, action_result["action"], action_result["result"]]
samples.append(row)
else:
sub_result = build_action_samples_helper(context, action_result["result"], action_result["action_results"])
samples += sub_result
return samples
def make_write_actions_batch(tree, filename):
# Traverse to the bottom levels of each tree
with open(filename, mode='w') as file:
writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(["context", "story_block_1", "previous_action", "story_block_2"])
first_story_block = tree["story_prompt"] + tree["story_continuation"]
samples = build_action_samples_helper(tree["context"], first_story_block, tree["action_results"])
for sample in samples:
writer.writerow(sample)
def save_tree(tree, filename):
with open(filename, 'w') as fp:
json.dump(tree, fp)
def load_tree(filename):
with open(filename, 'r') as fp:
tree = json.load(fp)
return tree
tree = dungeon_data_to_tree("./data_nick_1.csv")
tree["context"] = "The world has ended. The cities are in ruin and few people are left alive."
make_write_actions_batch(tree, "batch.csv")
save_tree(tree, "tree_data.json")
data = load_tree("tree_data.json")
print("done")
File diff suppressed because one or more lines are too long
+6
View File
@@ -0,0 +1,6 @@
"""
Story tree representation
"""
+6 -4
View File
@@ -69,10 +69,10 @@ var Typer={
evt = evt || window.event
var charCode = evt.keyCode || evt.which
if(charCode == 8){
if(inputStr.length > 0){
console.log(inputStr)
inputStr = inputStr.substring(0, inputStr.length-1)
console.log(inputStr)
if(Typer.inputStr.length > 0){
console.log(Typer.inputStr)
Typer.inputStr = Typer.inputStr.substring(0, Typer.inputStr.length-1)
console.log(Typer.inputStr)
Typer.removeChar()
}
}
@@ -98,6 +98,8 @@ var Typer={
},
}
function onButtonClick(num){
document.getElementById('buttons').style.visibility='hidden';
-78
View File
@@ -1,78 +0,0 @@
You enter the first door and see an old man in tattered robes. He is covered in dirt and grime. His eyes are sunken into his head as if he's been crying.
"Hello?"
"I'm here to kill you."
The old man looks up from where he was crouching.
"I don't know what you're talking about."
"You have no idea how much you scare me."
"What do you mean?"
"You're not going down there without me or your daughter."
My heart drops.
"Are you crazy?"
"No, I'm not. You need to go back home right now."
"Why would I? You're just scaring me!"
"Don't be silly. Go on ahead then."
"But why should I?"
"Because it's your only way out."
"Why?"
"Because you'll never get out alive. If you don't, I'll come after you. And then we'll both die."
"Why? Why me?"
"Because you've done something terrible. Something so terrible it will haunt you forever. You'll always remember this day because it'll forever change everything."
"How can you say that?"
Horror Text: I enter a dungeon with my trusty sword and shield. I am searching for the evil necromancer who killed my family. I've heard that he resides at the bottom of the dungeon, guarded by legions of the undead. I enter the first door and see an old man in tattered robes. He is covered in dirt and grime. His eyes are sunken into his head as if he's been crying.
"Hello?"
"I'm here to kill you."
The old man looks up from where he was crouching.
"I don't know what you're talking about."
"You have no idea how much you scare me."
"What do you mean?"
"You're not going down there without me or your daughter."
My heart drops.
"Are you crazy?"
"No, I'm not. You need to go back home right now."
"Why would I? You're just scaring me!"
"Don't be silly. Go on ahead then."
"But why should I?"
"Because it's your only way out."
"Why?"
"Because you'll never get out alive. If you don't, I'll come after you. And then we'll both die."
"Why? Why me?"
"Because you've done something terrible. Something so terrible it will haunt you forever. You'll always remember this day because it'll forever change everything."
"How can you say that?"
View File
+3 -3
View File
@@ -12,7 +12,6 @@ import numpy as np
app = Flask(__name__)
app.secret_key = '#d\xe0\xd1\xfb\xee\xa4\xbb\xd0\xf0/e)\xb5g\xdd<`\xc7\xa5\xb0-\xb8d0S'
CRED_FILE = "./AI-Adventure-2bb65e3a4e2f.json"
story = Story("")
# Bread and butter of app, updates story and returns based on choice
@app.route('/generate', methods=['POST'])
@@ -27,14 +26,15 @@ def generate():
# If there is a story in session continue from it.
elif "story" not in session or session["story"] is None:
story_start = session["prompt"] + action
story.story_start = story_start
story = Story(story_start)
session["story"] = story.to_json()
response = "Enter the action then two newlines then the result:\n\n> "
else:
story = Story("")
story = story.initialize_from_json(session["story"])
action, result = action.split("\n")
action_result = action.split("\n")
story.add_to_story(action, result)
session["story"] = story.to_json()