Class: LemmaAnalyzer

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

Class Method Summary collapse

Class Method Details

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

def self.isolate_lemmas_differences(current_content, parent_content)
    return {
    changed_lemmas: [],
    removed_lemmas: [],
    added_lemmas: []
    } if current_content.nil? && parent_content.nil?

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

    changed_labels = []
    removed_labels = []
    added_labels = []


    # if parentid is 0, then add all labels as added and return it
    if parent_content.nil?
        current_labels.each do |lang, label|
            added_labels << { lang: lang }
        end
        return {
            changed_lemmas: changed_labels,
            removed_lemmas: removed_labels,
            added_lemmas: added_labels
        }
    else

    
        # Iterate over each language in the current labels
        (current_labels || {}).each do |lang, current_label|
            parent_label = parent_labels[lang]
        
            if parent_label.nil?
                added_labels << { lang: lang }
            elsif current_label != parent_label
                changed_labels << { lang: lang }
            end
            end
        
            # Iterate over each language in the parent labels to find removed labels
            (parent_labels || {}).each do |lang, parent_label|
            if current_labels[lang].nil?
                removed_labels << { lang: lang }
            end
        end
    end

    {
    changed_lemmas: changed_labels,
    removed_lemmas: removed_labels,
    added_lemmas: added_labels
    }
end