group_by_match_type 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e02ae439383bee301cbfe85d02b557dc55883b5259bd11b701030f2d5b7a36d
|
4
|
+
data.tar.gz: b2cc916743083fb97352d194fe64713c4738bf4603be7f4ca5ff2ef786998ce0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59ab1c266cffceb1598be59aed1dcdc532a27a252f544d80028791947fb4f94ca24cd0c77b1b56b582ebe62b014205736f7f6afd169974d1cbb11ae066747da5
|
7
|
+
data.tar.gz: 5a8676a87c866a8c740e71253833024f3d048306601385e48f982e7a193fdb1ff0542d2377695dbd0f9afd49c25c225f5250d6cc715ba56db8221c450ee88dac
|
data/exe/group_by_match_type
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'optparse'
|
2
4
|
require_relative 'matcher'
|
3
5
|
|
4
6
|
module GroupByMatchType
|
5
7
|
class CLI
|
6
|
-
VALID_TYPES = %w[same_email same_phone same_email_or_phone]
|
8
|
+
VALID_TYPES = %w[same_email same_phone same_email_or_phone].freeze
|
7
9
|
|
8
10
|
def self.start(args)
|
9
11
|
if args.empty? || args.include?('-h') || args.include?('--help')
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'csv'
|
2
4
|
require_relative 'union_find'
|
3
5
|
|
@@ -28,11 +30,11 @@ module GroupByMatchType
|
|
28
30
|
# Determine output file path
|
29
31
|
output_file ||= @input_file.sub(/\.csv$/, '_grouped.csv')
|
30
32
|
CSV.open(output_file, 'w') do |csv|
|
31
|
-
csv <<
|
33
|
+
csv << ['group_id'] + rows.headers
|
32
34
|
rows.each_with_index do |row, index|
|
33
35
|
root = @union_find.find(index.to_s)
|
34
36
|
group_id = group_id_map[root]
|
35
|
-
csv << row.fields
|
37
|
+
csv << [group_id] + row.fields
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
@@ -1,26 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module GroupByMatchType
|
2
4
|
class UnionFind
|
3
5
|
def initialize
|
4
6
|
@parent = {}
|
5
7
|
end
|
6
8
|
|
7
|
-
def find(
|
8
|
-
@parent[
|
9
|
-
@parent[
|
9
|
+
def find(item)
|
10
|
+
@parent[item] = find(@parent[item]) if @parent[item] && @parent[item] != item
|
11
|
+
@parent[item] ||= item
|
10
12
|
end
|
11
13
|
|
12
|
-
def union(
|
13
|
-
|
14
|
-
|
15
|
-
@parent[
|
14
|
+
def union(source_item, target_item)
|
15
|
+
source_root = find(source_item)
|
16
|
+
target_root = find(target_item)
|
17
|
+
@parent[source_root] = target_root unless source_root == target_root
|
16
18
|
end
|
17
19
|
|
18
20
|
def find_or_create(items)
|
19
21
|
return nil if items.empty?
|
22
|
+
|
20
23
|
items.map!(&:to_s)
|
21
24
|
roots = items.map { |item| find(item) }
|
22
25
|
main_root = roots.first
|
23
|
-
roots[1..].each { |
|
26
|
+
roots[1..].each { |root| union(main_root, root) }
|
24
27
|
main_root
|
25
28
|
end
|
26
29
|
end
|