Class: AirportScraper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAirportScraper

Returns a new instance of AirportScraper.



6
7
8
9
# File 'lib/airport_scraper.rb', line 6

def initialize
  load_airports
  create_regexes
end

Instance Attribute Details

#airport_codesObject (readonly)

Returns the value of attribute airport_codes.



4
5
6
# File 'lib/airport_scraper.rb', line 4

def airport_codes
  @airport_codes
end

#airportsObject (readonly)

Returns the value of attribute airports.



4
5
6
# File 'lib/airport_scraper.rb', line 4

def airports
  @airports
end

Instance Method Details

#airport(code) ⇒ Object



95
96
97
# File 'lib/airport_scraper.rb', line 95

def airport(code)
  @airports[code]
end

#create_regexesObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/airport_scraper.rb', line 71

def create_regexes
  @code_match_regex = /\b([A-Z]{3})\b/

  flight_regex = /(flight|flying|plane|jet|turboprop)(\s(back|again|over))?/i
  
  @trans_regex = /(\sto\s)|(\s?->\s?)|(\s?>\s?)|(\s?✈\s?)/
  @via_regex = /,?\s?(via|by way of|on route to)\s/

  @preposition_regex = /\sfrom\s|\sto\s|#{@via_regex}|#{@trans_regex}|\sin\s|\sat\s|@\s|\sout of\s/i

  airport_regex = /(.+)/
  prep_airport_regex = /(.+)(?=#{@preposition_regex})/
  
  @match_regexes = [
    [/(#{@code_match_regex}(#{@trans_regex}(#{@code_match_regex}))+\b)/, @trans_regex],# (#{@via_regex}#{@code_match_regex}\b)?)/i,
    /((at|@|in) #{airport_regex} airport)/i,
    /((boarding|departing) (to|from|in) #{airport_regex})/i,
    /(touched down in #{airport_regex})\b/i,
    /((to land)|(land(ed|ing|s)) (in|at) #{airport_regex})\b/i,
    [/(#{flight_regex}#{@preposition_regex}(#{prep_airport_regex}#{@preposition_regex})*#{airport_regex}+)/i, @trans_regex],
    #/(#{flight_regex}( (from|in|at|out of) #{airport_regex})? (to|into|towards) #{airport_regex}(#{@via_regex}#{airport_regex})?)/i,
  ]
end

#extract_airports(text) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/airport_scraper.rb', line 119

def extract_airports(text)
  airports = [] 
  
  #puts @airport_regex.inspect

  @match_regexes.each do |regex_pair|
    
    case regex_pair
    when Array
      regex = regex_pair[0]
      split_regex = regex_pair[1]
    else
      regex = regex_pair
    end
    
    text.scan(regex) do |matches|
      if split_regex
        matches = matches.compact.map {|m| m.split(split_regex)}.flatten.uniq
      end
      
      #puts "Matches: #{matches.compact.inspect}"
      # puts "Text: #{text}"
      # puts "Regex: #{regex.inspect}"
      matches.compact.each do |match|
        next if match.nil? || match.length < 2

        if match =~ /^#{@code_match_regex}/
          #puts "MATCH: #{match}"
          airport = @airports[$1]
          airports << airport unless airport.nil?
        else
          possible_airports = @matcher_prefixes[prefix_from_match(match)]
          unless possible_airports.nil?
            possible_airports.each do |a|
              next if a['regex'].nil?
              if match =~ /#{a['regex']}\b/
                airports << a
                break
              end
            end
          end
        end
      end
      
      # break
    end
  end
  
  airports.uniq
end

#flight_termsObject



99
100
101
# File 'lib/airport_scraper.rb', line 99

def flight_terms
  %w(touched landed landing land lands plane jet turboprop flying flight boarding departing)
end

#is_flight(text) ⇒ Object



115
116
117
# File 'lib/airport_scraper.rb', line 115

def is_flight(text)
  true
end

#load_airportsObject



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
# File 'lib/airport_scraper.rb', line 19

def load_airports
  @airports = {}
  
  %w(ca_airports us_airports intl_airports).each do |file|
    @airports.merge!(YAML.load_file(File.join(File.dirname(__FILE__), "#{file}.yml")))
  end
  
  @matcher_prefixes = {}
  
  @airports.each do |key, value|
    value['code'] = key
    value['major'] ||= false
    value['match_priority'] ||= value['major'] ? 10 : 0
    value['name'] ||= value['city']
    value['matchers'] = case value['matchers']
      when nil
        []
      when Array
        value['matchers']
      else
        value['matchers'].to_a
    end
    
    value['regex'] = regex_from_matchers(value['matchers'])

    unless value['matchers'].nil?
      prefixes = value['matchers'].map {|x| prefix_from_match(x)}.uniq
      prefixes.each do |p| 
        @matcher_prefixes[p] ||= []
        @matcher_prefixes[p] << value
      end
    end
  end
  
  @airport_codes = @airports.keys

  @matcher_prefixes.values.each do |airports|
    airports.sort! {|a, b| b['match_priority'] <=> a['match_priority'] }
  end
  
#    raise @matcher_prefixes.inspect
end

#possible_flight?(text) ⇒ Boolean

Returns:



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/airport_scraper.rb', line 103

def possible_flight?(text)
  @match_regexes.any? do |regex_pair|
    if regex_pair.is_a? (Array)
      regex = regex_pair[0]
    else
      regex = regex_pair
    end
    
    regex =~ text
  end
end

#prefix_from_match(str) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/airport_scraper.rb', line 62

def prefix_from_match(str)
  case str 
  when /\w\w\b/
    str[0,2].downcase
  else
    str[0,3].downcase
  end
end

#regex_from_matchers(matchers) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/airport_scraper.rb', line 11

def regex_from_matchers(matchers)
  if matchers.nil? || matchers.empty?
    nil
  else
    /^(#{matchers.map {|x| x.gsub(".", "\\.")}.join('|')})\b/i
  end
end