Class: GroupByMatchType::Matcher
- Inherits:
-
Object
- Object
- GroupByMatchType::Matcher
- Defined in:
- lib/group_by_match_type/matcher.rb
Instance Method Summary collapse
-
#initialize(input_file, match_type) ⇒ Matcher
constructor
A new instance of Matcher.
- #process(output_file = nil) ⇒ Object
Constructor Details
#initialize(input_file, match_type) ⇒ Matcher
Returns a new instance of Matcher.
8 9 10 11 12 |
# File 'lib/group_by_match_type/matcher.rb', line 8 def initialize(input_file, match_type) @input_file = input_file @match_type = match_type @union_find = UnionFind.new end |
Instance Method Details
#process(output_file = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/group_by_match_type/matcher.rb', line 14 def process(output_file = nil) rows = CSV.read(@input_file, headers: true) match_keys = build_match_keys(rows) # First pass: create groupings match_keys.each_with_index do |keys, index| @union_find.find_or_create(keys + [index.to_s]) # Ensure uniqueness end # Second pass: assign group_ids group_id_map = {} rows.each_with_index do |_, index| root = @union_find.find(index.to_s) group_id_map[root] ||= group_id_map.size + 1 end # Determine output file path output_file ||= @input_file.sub(/\.csv$/, '_grouped.csv') CSV.open(output_file, 'w') do |csv| csv << ['group_id'] + rows.headers rows.each_with_index do |row, index| root = @union_find.find(index.to_s) group_id = group_id_map[root] csv << [group_id] + row.fields end end output_file end |