Class: LabelAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/wikidata/diff/label_analyzer.rb

Class Method Summary collapse

Class Method Details

.isolate_labels_differences(current_content, parent_content) ⇒ Object



3
4
5
6
7
8
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/wikidata/diff/label_analyzer.rb', line 3

def self.isolate_labels_differences(current_content, parent_content)
    return {
    changed_labels: [],
    removed_labels: [],
    added_labels: []
    } if current_content.nil? && parent_content.nil?
    
    if current_content
        current_labels = current_content['labels']
        if current_labels.nil? || current_labels.is_a?(Array)
            current_labels = {}
        end
    else
        current_labels = {}
    end

    if parent_content
        parent_labels = parent_content['labels']
        if parent_labels.nil? || parent_labels.is_a?(Array)
            parent_labels = {}
        end
    else
        parent_labels = {}
    end



    changed_labels_labels = []
    removed_labels_labels = []
    added_labels_labels = []

    # if parentid is 0, then add all labels as added_labels and return it
    if parent_content.nil?  
        if !current_labels.empty?
            current_labels.each do |lang, label|
                added_labels_labels << { lang: lang }
            end
        end
        return {
            changed_labels: changed_labels_labels,
            removed_labels: removed_labels_labels,
            added_labels: added_labels_labels
        }
    else
        # Iterate over each language in the current labels
        (current_labels).each do |lang, current_label|
            if parent_labels.empty?
                added_labels_labels << { lang: lang }
            else
                parent_label = parent_labels[lang]
                if parent_label.nil?
                    added_labels_labels << { lang: lang }
                elsif current_label != parent_label
                    changed_labels_labels << { lang: lang }
                end
            end
        end
        
            # Iterate over each language in the parent labels to find removed_labels labels
            (parent_labels).each do |lang, parent_label|
            if current_labels.empty?
                removed_labels_labels << { lang: lang }
            end
        end
    end

    {
    changed_labels: changed_labels_labels,
    removed_labels: removed_labels_labels,
    added_labels: added_labels_labels
    }
end