Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Used temporary branch to add print_conversations file to main search and rescue branch #5

Merged
merged 7 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ ENV/
# Rope project settings
.ropeproject

.history/
.history/
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"sim_code": "33"
}
}
35 changes: 26 additions & 9 deletions print_conversations.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
import glob
import os
chowington marked this conversation as resolved.
Show resolved Hide resolved
import sys
import json


def get_unique_conversations(simulation_name):
step_folder = f"environment/frontend_server/storage/{simulation_name}/movement"
step_folder = "environment/frontend_server/storage"

# Use glob to find all files that start with the simulation_name
search_pattern = os.path.join(step_folder, f"{simulation_name}*/movement/*")
filepaths = glob.glob(search_pattern)

observed_conversations = set()
file_contents = []

# Iterate over all files in the simulation folder
for filename in os.listdir(step_folder):
filepath = os.path.join(step_folder, filename)

# Iterate over all matching files
for filepath in filepaths:
with open(filepath, "r") as file:
data = json.load(file)
personas = data["persona"]
personas = data.get("persona", {})

# Loop over all personas except one, since conversations are always
# between two people
for name in list(personas.keys())[:-1]:
persona = personas[name]
chat = persona["chat"]
chat = persona.get("chat")

if chat:
chat_string = str(chat)

# Add unique conversations
if chat_string not in observed_conversations:
observed_conversations.add(chat_string)
file_contents.append(data)
break

return file_contents

def write_conversations_to_file(conversations, simulation_name):
output_directory = "logs/conversations"
if not os.path.exists(output_directory):
os.makedirs(output_directory)
output_filename = f"{simulation_name}_highlights.json"
full_path = os.path.join(output_directory, output_filename);
with open(full_path, "w") as file:
for conversation in conversations:
json.dump(conversation, file, indent=4)

if __name__ == "__main__":
if len(sys.argv) < 2:
Expand All @@ -41,4 +53,9 @@ def get_unique_conversations(simulation_name):

simulation_name = sys.argv[1]
unique_conversations = get_unique_conversations(simulation_name)
print(json.dumps(unique_conversations, indent=2))

if unique_conversations:
write_conversations_to_file(unique_conversations, simulation_name)
print(f"Unique conversations written to {simulation_name}_highlights.txt")
else:
print("No unique conversations found.")