Module: Forecast::Utils

Defined in:
lib/forecast/utils.rb

Class Method Summary collapse

Class Method Details

.celsius_to_fahrenheit(celsius) ⇒ Object



41
42
43
# File 'lib/forecast/utils.rb', line 41

def celsius_to_fahrenheit(celsius)
  celsius * 1.8 + 32 
end

.celsius_to_kelvin(celsius) ⇒ Object



45
46
47
# File 'lib/forecast/utils.rb', line 45

def celsius_to_kelvin(celsius)
  celsius + 273.15
end

.fahrenheit_to_celsius(fahrenheit) ⇒ Object



29
30
31
# File 'lib/forecast/utils.rb', line 29

def fahrenheit_to_celsius(fahrenheit)
  ((fahrenheit - 32) / 1.8)
end

.fahrenheit_to_kelvin(fahrenheit) ⇒ Object



25
26
27
# File 'lib/forecast/utils.rb', line 25

def fahrenheit_to_kelvin(fahrenheit)
  ((fahrenheit - 32) / 1.8) - 273.15
end

.get_doc(url, params = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/forecast/utils.rb', line 63

def get_doc(url, params = {})
  if params.keys.count > 0
    query_string = URI.encode_www_form(params)
    url = url + "?" + query_string
  end
  xml_data = Net::HTTP.get_response(URI.parse(url)).body
  doc = REXML::Document.new(xml_data)
  return doc
end

.get_json(url, params = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/forecast/utils.rb', line 49

def get_json(url, params = {})
  if params.keys.count > 0
    query_string = URI.encode_www_form(params)
    url = url + "?" + query_string
  end
  resp = Net::HTTP.get_response(URI.parse(url))
  data = resp.body
  result = JSON.parse(data)
  if result && result['cod'] != "404"
    return result
  end
  return nil
end

.kelvin_to_celsius(kelvin) ⇒ Object



37
38
39
# File 'lib/forecast/utils.rb', line 37

def kelvin_to_celsius(kelvin)
  kelvin - 273.15
end

.kelvin_to_fahrenheit(kelvin) ⇒ Object



33
34
35
# File 'lib/forecast/utils.rb', line 33

def kelvin_to_fahrenheit(kelvin)
  return ((kelvin - 273.15) * 1.8 + 32)
end

.levenshtein(first, second) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/forecast/utils.rb', line 104

def levenshtein(first, second)
  matrix = [(0..first.length).to_a]
  (1..second.length).each do |j|
    matrix << [j] + [0] * (first.length)
  end
  (1..second.length).each do |i|
    (1..first.length).each do |j|
      if first[j-1] == second[i-1]
        matrix[i][j] = matrix[i-1][j-1]
      else
        matrix[i][j] = [
          matrix[i-1][j],
          matrix[i][j-1],
          matrix[i-1][j-1],
        ].min + 1
      end
    end
  end
  return matrix.last.last
end

.underscore(string) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/forecast/utils.rb', line 11

def underscore(string)
  if string.is_a?(String)
    return string.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
  elsif string.is_a?(Hash)
    return Hash[string.map { |k, v| [Forecast::Utils.underscore(k.to_s).to_sym, v.is_a?(Hash) ? Forecast::Utils.underscore(v) : v] }]
  else
    string
  end
end

.word_similarity(first, second) ⇒ Object



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
# File 'lib/forecast/utils.rb', line 73

def word_similarity(first, second)
  
  if !first.is_a?(String) || !second.is_a?(String)
    return 0
  end
  similar_words = 0.0
  first_words = first.downcase.split(/\W+/)
  second_words = second.downcase.split(/\W+/)
  
  first_words.each do |first_word|
    second_words.each do |second_word|
      similar = 0.0
      if first_word == second_word
        similar = 1.0
      else
        l1 = levenshtein(first_word, second_word)
        l = 1 - (l1.to_f / ([first_word.length, second_word.length].max))
        l = [0, similar].max
        l = [similar, 1].min
        if l1 > 0.6
          similar = 0.1
        end
      end
      similar_words+= similar
    end
  end
  count = first_words.concat(second_words).uniq.length
  similarity = similar_words / count
  return similarity
end