1. import pandas as pd
  2. import os
  3. def export_string_results(results, output_dir="output", dry_run=True):
  4. """
  5. Exports string results to a CSV file for exploratory analysis.
  6. Args:
  7. results (dict): A dictionary where keys are labels and values are lists of strings.
  8. output_dir (str): The directory to save the CSV file. Defaults to "output".
  9. dry_run (bool): If True, only prints the file path without creating the file. Defaults to True.
  10. """
  11. if not os.path.exists(output_dir):
  12. os.makedirs(output_dir)
  13. filename = os.path.join(output_dir, "string_results.csv")
  14. if dry_run:
  15. print(f"Dry run: Would export to {filename}")
  16. else:
  17. df = pd.DataFrame(results)
  18. df.to_csv(filename, index=False)
  19. print(f"Exported to {filename}")
  20. if __name__ == '__main__':
  21. # Example usage
  22. example_results = {
  23. "label_a": ["apple", "banana", "cherry"],
  24. "label_b": ["red", "green", "blue"],
  25. "label_c": ["one", "two", "three"]
  26. }
  27. export_string_results(example_results, output_dir="my_results", dry_run=True)
  28. export_string_results(example_results, output_dir="my_results", dry_run=False)

Add your comment