Class: Topical::ModelSerializer
- Inherits:
-
Object
- Object
- Topical::ModelSerializer
- Defined in:
- lib/topical/model_serializer.rb
Overview
Handles saving and loading of topic models
Class Method Summary collapse
-
.load(path) ⇒ Engine
Load a topic model from JSON file.
-
.save(engine, path) ⇒ Object
Save a topic model to JSON file.
Class Method Details
.load(path) ⇒ Engine
Load a topic model from JSON file
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/topical/model_serializer.rb', line 38 def self.load(path) require 'json' data = JSON.parse(File.read(path), symbolize_names: true) # Make sure k is passed for kmeans and convert string keys to symbols config = data[:config] config[:clustering_method] = config[:clustering_method].to_sym if config[:clustering_method] config[:labeling_method] = config[:labeling_method].to_sym if config[:labeling_method] if config[:clustering_method] == :kmeans && !config[:k] # Extract k from saved topics or use default config[:k] = data[:topics]&.length || 5 end engine = Engine.new(**config) # Reconstruct topics engine.instance_variable_set(:@topics, data[:topics].map { |t| Topic.from_h(t) }) engine end |
.save(engine, path) ⇒ Object
Save a topic model to JSON file
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/topical/model_serializer.rb', line 9 def self.save(engine, path) require 'json' config = { clustering_method: engine.instance_variable_get(:@clustering_method), min_cluster_size: engine.instance_variable_get(:@min_cluster_size), min_samples: engine.instance_variable_get(:@min_samples), reduce_dimensions: engine.instance_variable_get(:@reduce_dimensions), n_components: engine.instance_variable_get(:@n_components), labeling_method: engine.instance_variable_get(:@labeling_method) } # Include k for kmeans = engine.instance_variable_get(:@options) if config[:clustering_method] == :kmeans config[:k] = [:k] || engine.topics.length end data = { topics: engine.topics.map(&:to_h), config: config } File.write(path, JSON.pretty_generate(data)) end |