Class: Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/gtfs_stops_clustering/utils.rb

Overview

Utils class

Class Method Summary collapse

Class Method Details

.find_cluster_name(labels) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gtfs_stops_clustering/utils.rb', line 5

def self.find_cluster_name(labels)
  words = labels.map { |label| label.strip.split }
  common_title = ""

  # Loop through each word index starting from the first
  (0...words.first.length).each do |i|
    words_at_index = words.map { |word_list| word_list[i] }

    break unless words_at_index.uniq.length == 1

    common_title += " #{words_at_index.first.capitalize}"
  end

  common_title.strip! ? common_title : labels.first
end

.find_cluster_position(cluster) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/gtfs_stops_clustering/utils.rb', line 21

def self.find_cluster_position(cluster)
  total_lat = cluster.map { |e| e.items[0].to_f }.sum
  total_lon = cluster.map { |e| e.items[1].to_f }.sum
  avg_lat = total_lat / cluster.size
  avg_lon = total_lon / cluster.size
  [avg_lat, avg_lon]
end

.find_inmediate_neighbor(neighbor_pos, points) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/gtfs_stops_clustering/utils.rb', line 34

def self.find_inmediate_neighbor(neighbor_pos, points)
  coordinates_split = neighbor_pos.split(",")
  points.find do |elem|
    elem.items[0] == coordinates_split[1] &&
      elem.items[1] == coordinates_split[0]
  end
end

.string_similarity(str1, str2) ⇒ Object



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

def self.string_similarity(str1, str2)
  string_distance = Text::Levenshtein.distance(str1.downcase, str2.downcase)
  1 - string_distance.to_f / [str1.length, str2.length].max
end