Class: Importance::Header

Inherits:
Object
  • Object
show all
Defined in:
app/models/importance/header.rb

Class Method Summary collapse

Class Method Details

.default_value_for_header(file_header, attribute_mappings) ⇒ Object



36
37
38
# File 'app/models/importance/header.rb', line 36

def self.default_value_for_header(file_header, attribute_mappings)
  attribute_mappings.key(file_header) || ""
end

.match_attributes_to_headers(importer_attributes, file_headers) ⇒ 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
# File 'app/models/importance/header.rb', line 3

def self.match_attributes_to_headers(importer_attributes, file_headers)
  attribute_mappings = {}

  importer_attributes.each do |attribute|
    best_header = nil
    best_similarity = 0

    file_headers.each do |header|
      attribute.labels.each do |label|
        if header == label
          best_header = header
          best_similarity = 1.0
          break # No need to check further if an exact match is found
        end
        distance = DidYouMean::Levenshtein.distance(header.to_s, label.to_s)
        percentage = distance / header.to_s.length.to_f
        similarity = 1 - percentage
        if similarity > best_similarity
          best_similarity = similarity
          best_header = header
        end
      end
    end

    # Only assign if similarity is reasonable (> 0.5) and header isn't already taken
    if best_similarity > 0.5 && !attribute_mappings.values.include?(best_header)
      attribute_mappings[attribute.key] = best_header
    end
  end

  attribute_mappings
end