Class: DescriptionAnalyzer

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

Class Method Summary collapse

Class Method Details

.isolate_descriptions_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
# File 'lib/wikidata/diff/description_analyzer.rb', line 2

def self.isolate_descriptions_differences(current_content, parent_content)
    return {
    changed_descriptions: [],
    removed_descriptions: [],
    added_descriptions: []
    } if current_content.nil? && parent_content.nil?

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

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

    changed_descriptions = []    # Initialize as an array
    removed_descriptions = []    # Initialize as an array
    added_descriptions = []      # Initialize as an array


    # if parentid is 0, add all current description as added and return it
    if parent_content.nil?
        if !current_descriptions.empty?
            current_descriptions.each do |lang, description|
                added_descriptions << { lang: lang }
            end
        end
        return {
            changed_descriptions: changed_descriptions,
            removed_descriptions: removed_descriptions,
            added_descriptions: added_descriptions
        }
    else
        # Iterate over each language in the current descriptions
        (current_descriptions).each do |lang, current_description|
            # checking if the parent descriptions is empty
            if parent_descriptions.empty?
                added_descriptions << { lang: lang }
            elsif parent_descriptions[lang].nil?
                added_descriptions << { lang: lang }
            elsif current_description != parent_descriptions[lang]
                changed_descriptions << { lang: lang }
            end
        end
        
            # Iterate over each language in the parent descriptions to find removed descriptions
        (parent_descriptions).each do |lang, parent_description|
            if current_descriptions.empty?
                removed_descriptions << { lang: lang }
            end
        end
    end
    {
        changed_descriptions: changed_descriptions,
        removed_descriptions: removed_descriptions,
        added_descriptions: added_descriptions
    }
end