# frozen_string_literal: true require "json" require "fileutils" output_dir = Rails.root.join("huggingface-dataset", "data") FileUtils.mkdir_p(output_dir) puts "Exporting Cosmic Structures, Star Systems, and Celestial Bodies from Astronomer to #{output_dir}..." # 1. Cosmic Structures structures = CosmicStructure.order(:id).to_a systems_count_by_structure = StarSystem.group(:cosmic_structure_id).count bodies_count_by_structure = CelestialBody.joins(:star_system).group("star_systems.cosmic_structure_id").count structure_rows = structures.map do |s| { cosmic_structure_id: s.id, name: s.name, description: s.description, star_systems_count: systems_count_by_structure[s.id] || 0, celestial_bodies_count: bodies_count_by_structure[s.id] || 0, created_at: s.created_at.iso8601, updated_at: s.updated_at.iso8601 } end File.open(output_dir.join("cosmic_structures.jsonl"), "w") do |f| structure_rows.each { |row| f.puts(JSON.generate(row)) } end puts "Exported #{structure_rows.size} cosmic structures to cosmic_structures.jsonl" # 2. Star Systems systems = StarSystem.includes(:cosmic_structure).order(:id).to_a bodies_count_by_system = CelestialBody.group(:star_system_id).count system_rows = systems.map do |s| { star_system_id: s.id, cosmic_structure_id: s.cosmic_structure_id, cosmic_structure_name: s.cosmic_structure.name, name: s.name, description: s.description, themes: s.themes, celestial_bodies_count: bodies_count_by_system[s.id] || 0, embedding_model: s.embedding_model, embedding: s.embedding, created_at: s.created_at.iso8601, updated_at: s.updated_at.iso8601 } end File.open(output_dir.join("star_systems.jsonl"), "w") do |f| system_rows.each { |row| f.puts(JSON.generate(row)) } end puts "Exported #{system_rows.size} star systems to star_systems.jsonl" # 3. Celestial Bodies bodies = CelestialBody.includes(star_system: :cosmic_structure).order(:id).to_a body_rows = bodies.map do |b| { celestial_body_id: b.id, star_system_id: b.star_system_id, star_system_name: b.star_system.name, cosmic_structure_id: b.star_system.cosmic_structure_id, cosmic_structure_name: b.star_system.cosmic_structure.name, name: b.name, description: b.description, themes: b.themes, embedding_model: b.embedding_model, embedding: b.embedding, created_at: b.created_at.iso8601, updated_at: b.updated_at.iso8601 } end File.open(output_dir.join("celestial_bodies.jsonl"), "w") do |f| body_rows.each { |row| f.puts(JSON.generate(row)) } end puts "Exported #{body_rows.size} celestial bodies to celestial_bodies.jsonl" # 4. Train (Unified Denormalized Cosmic Structures -> Star Systems -> Celestial Bodies) train_rows = bodies.map do |b| system = b.star_system structure = system.cosmic_structure { cosmic_structure_id: structure.id, cosmic_structure_name: structure.name, cosmic_structure_description: structure.description, star_system_id: system.id, star_system_name: system.name, star_system_description: system.description, star_system_themes: system.themes, star_system_embedding: system.embedding, celestial_body_id: b.id, celestial_body_name: b.name, celestial_body_description: b.description, celestial_body_themes: b.themes, celestial_body_embedding: b.embedding, embedding_model: b.embedding_model || system.embedding_model || "qwen3-embedding:0.6b" } end File.open(output_dir.join("train.jsonl"), "w") do |f| train_rows.each { |row| f.puts(JSON.generate(row)) } end puts "Exported #{train_rows.size} unified records to train.jsonl" puts "JSONL export complete!"