Class: ClaimAnalyzer

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

Class Method Summary collapse

Class Method Details

.isolate_claims_differences(current_content, parent_content) ⇒ Object



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wikidata/diff/claim_analyzer.rb', line 5

def self.isolate_claims_differences(current_content, parent_content)
    # Initialize empty arrays to store the added, removed, and changed claims
    added_claims = []
    removed_claims = []
    changed_claims = []
    added_references = []
    removed_references = []
    changed_references = []
    added_qualifiers = []
    removed_qualifiers = []
    changed_qualifiers = []

    if current_content.nil?
        current_content_claims = {}
    else
        current_content_claims = current_content["claims"]
        if !current_content_claims.is_a?(Hash)
            current_content_claims = {}
        end
    end
    

    if parent_content.nil?
        parent_content_claims = {}
    else
        parent_content_claims = parent_content["claims"]
        if !parent_content_claims.is_a?(Hash)
            parent_content_claims = {}
        end
    end
    
    # if parentid is 0, add all current claims as added claims and return it
    if parent_content.nil?
        current_content_claims.each do |claim_key, current_claims|
            current_claims.each_with_index do |current_claim, index|
                added_claims << { key: claim_key, index: index }
                # check if there's any references or qualifiers in this claim
                added_references = ReferenceAnalyzer.reference_updates(current_claim, added_references, claim_key, index)
                added_qualifiers = QualifierAnalyzer.qualifier_updates(current_claim, added_qualifiers, claim_key, index)
            end
        end
    else
        # Iterate over each claim key in the current content
        current_content_claims.each do |claim_key, current_claims|
        # Check if the claim key exists in the parent content
        if parent_content_claims.key?(claim_key)
            parent_claims = parent_content_claims[claim_key]
            # Iterate over each claim in the current and parent content
            current_claims.each_with_index do |current_claim, index|
                parent_claim = parent_claims[index]
                if parent_claim.nil?
                    # Claim was added
                    added_claims << { key: claim_key, index: index }
                    # check if there's any references or qualifiers in this claim
                    added_references = ReferenceAnalyzer.reference_updates(current_claim, added_references, claim_key, index)
                    added_qualifiers = QualifierAnalyzer.qualifier_updates(current_claim, added_qualifiers, claim_key, index)

                elsif current_claim != parent_claim
                    # Claim was changed
                    changed_claims << { key: claim_key, index: index }
                    # check if there's any references or qualifiers in this claim
                    changed_references_hash = ReferenceAnalyzer.handle_changed_references(current_claim, parent_claim, changed_references, added_references, removed_references, claim_key, index)

                    added_references = changed_references_hash[:added_references]
                    removed_references = changed_references_hash[:removed_references]
                    changed_references = changed_references_hash[:changed_references]

                    changed_qualifiers_hash = QualifierAnalyzer.handle_changed_qualifiers(current_claim, parent_claim, changed_qualifiers, added_qualifiers, removed_qualifiers, claim_key, index)

                    added_qualifiers = changed_qualifiers_hash[:added_qualifiers]
                    removed_qualifiers = changed_qualifiers_hash[:removed_qualifiers]
                    changed_qualifiers = changed_qualifiers_hash[:changed_qualifiers]
                end
            end
            # Check for removed claims
            parent_claims.each_with_index do |parent_claim, index|
                current_claim = current_claims[index]
                if current_claim.nil?
                    # Claim was removed
                    removed_claims << { key: claim_key, index: index }

                    # check if there's any references or qualifiers in this claim
                    removed_references = ReferenceAnalyzer.reference_updates(parent_claim, removed_references, claim_key, index)
                    removed_qualifiers = QualifierAnalyzer.qualifier_updates(parent_claim, removed_qualifiers, claim_key, index)
                end
            end
        else
            # All claims in current content with this key were added
            current_claims.each_index do |index|
                added_claims << { key: claim_key, index: index }
                # check if there's any references or qualifiers in this claim
                added_references = ReferenceAnalyzer.reference_updates(current_claims[index], added_references, claim_key, index)
                added_qualifiers = QualifierAnalyzer.qualifier_updates(current_claims[index], added_qualifiers, claim_key, index)
            end
        end
        end

        parent_content_claims.each do |claim_key, parent_claims|
            # current content[claims] can be nil
            parent_claims.each_index do |index|
                if current_content_claims.nil? || !current_content_claims.key?(claim_key)
                removed_claims << { key: claim_key, index: index }
                # check if there's any references or qualifiers in this claim
                removed_references = ReferenceAnalyzer.reference_updates(parent_claims[index], removed_references, claim_key, index)
                removed_qualifiers = QualifierAnalyzer.qualifier_updates(parent_claims[index], removed_qualifiers, claim_key, index)
                end
            end
        end
    end

    {
        added_claims: added_claims,
        removed_claims: removed_claims,
        changed_claims: changed_claims,
        added_references: added_references,
        removed_references: removed_references,
        changed_references: changed_references,
        added_qualifiers: added_qualifiers,
        removed_qualifiers: removed_qualifiers,
        changed_qualifiers: changed_qualifiers
    }
end