Class: AliasAnalyzer

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

Class Method Summary collapse

Class Method Details

.isolate_aliases_differences(current_content, parent_content) ⇒ Object



2
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
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wikidata/diff/alias_analyzer.rb', line 2

def self.isolate_aliases_differences(current_content, parent_content)
    return {
    changed_aliases: [],
    removed_aliases: [],
    added_aliases: []
    } if current_content.nil? && parent_content.nil?

    changed_aliases = []
    removed_aliases = []
    added_aliases = []

    if current_content
        current_aliases = current_content['aliases']
        if current_aliases.nil? || current_aliases.is_a?(Array)
            current_aliases = {}
        end
    else
        current_aliases = {}
    end

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


    if parent_content.nil?
        (current_aliases || {}).each do |lang, current_aliases_arr|
            current_aliases_arr = [current_aliases_arr] unless current_aliases_arr.is_a?(Array)
        
            current_aliases_arr.each_with_index do |current_alias, index|
                added_aliases << { lang: lang, index: index }
            end
        end
    else
        # Iterate over each language in the current aliases
        (current_aliases || {}).each do |lang, current_aliases_arr|
            parent_aliases_arr = parent_aliases[lang]
        
            # Check if the language exists in the parent aliases
            if parent_aliases_arr
                # Ensure that current_aliases_arr is always an array
                current_aliases_arr = [current_aliases_arr] unless current_aliases_arr.is_a?(Array)
        
                current_aliases_arr.each_with_index do |current_alias, index|
                parent_alias = parent_aliases_arr[index]
                if parent_alias.nil?
                    added_aliases << { lang: lang, index: index }
                elsif current_alias != parent_alias
                    changed_aliases << { lang: lang, index: index }
                end
                end
            else
                # Ensure that current_aliases_arr is always an array
                current_aliases_arr = [current_aliases_arr] unless current_aliases_arr.is_a?(Array)
        
                current_aliases_arr.each_with_index do |current_alias, index|
                added_aliases << { lang: lang, index: index }
                end
            end
        end
    
        # Iterate over each language in the parent aliases to find removed aliases
        (parent_aliases || {}).each do |lang, parent_aliases_arr|
        # Ensure that parent_aliases_arr is always an array
            parent_aliases_arr = [parent_aliases_arr] unless parent_aliases_arr.is_a?(Array)
        
            current_aliases_arr = current_aliases[lang]
        
            if current_aliases_arr.nil?
                parent_aliases_arr.each_index do |index|
                removed_aliases << { lang: lang, index: index }
                end
            end
        end
    end
    {
        changed_aliases: changed_aliases,
        removed_aliases: removed_aliases,
        added_aliases: added_aliases    
    }
end