Class: Broadlistening::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/broadlistening/context.rb

Overview

Manages pipeline execution context - all data flowing through the pipeline.

The Context holds all intermediate results and provides methods for loading from / saving to disk for incremental execution support.

Examples:

Creating a new context

context = Context.new
context.comments = [Comment.new(...), ...]
context.save_step(:extraction, output_dir)

Loading from existing output

context = Context.load_from_dir("/path/to/output")

Constant Summary collapse

OUTPUT_FILES =

Output file mapping for each step

{
  extraction: "extraction.json",
  embedding: "embeddings.json",
  clustering: "clustering.json",
  initial_labelling: "initial_labels.json",
  merge_labelling: "merge_labels.json",
  overview: "overview.json",
  aggregation: "result.json"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/broadlistening/context.rb', line 56

def initialize
  @comments = []
  @arguments = []
  @relations = []
  @cluster_results = {}
  @umap_coords = nil
  @initial_labels = {}
  @labels = {}
  @overview = nil
  @result = nil
  @output_dir = nil
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



21
22
23
# File 'lib/broadlistening/context.rb', line 21

def arguments
  @arguments
end

#cluster_resultsObject

Returns the value of attribute cluster_results.



21
22
23
# File 'lib/broadlistening/context.rb', line 21

def cluster_results
  @cluster_results
end

#commentsObject

Returns the value of attribute comments.



21
22
23
# File 'lib/broadlistening/context.rb', line 21

def comments
  @comments
end

#initial_labelsObject

Returns the value of attribute initial_labels.



21
22
23
# File 'lib/broadlistening/context.rb', line 21

def initial_labels
  @initial_labels
end

#labelsObject

Returns the value of attribute labels.



21
22
23
# File 'lib/broadlistening/context.rb', line 21

def labels
  @labels
end

#output_dirObject

Returns the value of attribute output_dir.



21
22
23
# File 'lib/broadlistening/context.rb', line 21

def output_dir
  @output_dir
end

#overviewObject

Returns the value of attribute overview.



21
22
23
# File 'lib/broadlistening/context.rb', line 21

def overview
  @overview
end

#relationsObject

Returns the value of attribute relations.



21
22
23
# File 'lib/broadlistening/context.rb', line 21

def relations
  @relations
end

#resultObject

Returns the value of attribute result.



21
22
23
# File 'lib/broadlistening/context.rb', line 21

def result
  @result
end

#umap_coordsObject

Returns the value of attribute umap_coords.



21
22
23
# File 'lib/broadlistening/context.rb', line 21

def umap_coords
  @umap_coords
end

Class Method Details

.load_from_dir(output_dir) ⇒ Context

Load existing context from output directory



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/broadlistening/context.rb', line 41

def self.load_from_dir(output_dir)
  context = new
  dir = Pathname.new(output_dir)

  OUTPUT_FILES.each do |step, filename|
    file = dir / filename
    next unless file.exist?

    data = JSON.parse(file.read, symbolize_names: true)
    context.send(:merge_step_data, step, data)
  end

  context
end

Instance Method Details

#save_step(step_name, output_dir) ⇒ Object

Save a step's output to file



73
74
75
76
77
78
79
80
81
# File 'lib/broadlistening/context.rb', line 73

def save_step(step_name, output_dir)
  dir = Pathname.new(output_dir)
  filename = OUTPUT_FILES[step_name]
  return unless filename

  FileUtils.mkdir_p(dir)
  data = extract_step_output(step_name)
  File.write(dir / filename, JSON.pretty_generate(data))
end

#to_hHash

Convert to hash for serialization



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/broadlistening/context.rb', line 86

def to_h
  {
    comments: @comments.map(&:to_h),
    arguments: @arguments.map(&:to_h),
    relations: @relations,
    cluster_results: @cluster_results,
    umap_coords: @umap_coords,
    initial_labels: @initial_labels,
    labels: @labels,
    overview: @overview,
    result: @result
  }
end