Skip to content

Commit f421ba9

Browse files
Duc Anh NguyenDuc Anh Nguyen
Duc Anh Nguyen
authored and
Duc Anh Nguyen
committedJan 28, 2017
location guessing system
1 parent c5919b3 commit f421ba9

File tree

6 files changed

+124
-17
lines changed

6 files changed

+124
-17
lines changed
 

‎app/extensions/abbreviation_extension.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def encode_region_24HoursForecast(location)
3838
end
3939

4040
def decode_region_24HoursForecast(location)
41-
location[2,5].capitalize
41+
location[2, 5].capitalize
4242
end
4343

4444
def get_forecast_meaning(abbreviation)

‎app/extensions/wit_extension.rb

+27-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ def initialize
3333
entities = request['entities']
3434

3535
location = first_entity_value(entities, 'location') || context['location']
36-
intent = first_entity_value(entities, 'intent') || context['intent']
36+
# intent = first_entity_value(entities, 'intent') || context['intent']
37+
38+
known_location = KnownLocation.get_known_location(location, 'location')
39+
if known_location
40+
location = known_location
41+
else
42+
location = KnownLocation.guess_known_location(location, 'location')
43+
context['guess_location'] = location
44+
end
3745

3846
if location
3947
forecast = search_forecast(location)
@@ -57,7 +65,15 @@ def initialize
5765
entities = request['entities']
5866

5967
location = first_entity_value(entities, 'location') || context['location']
60-
intent = first_entity_value(entities, 'intent') || context['intent']
68+
# intent = first_entity_value(entities, 'intent') || context['intent']
69+
70+
known_location = KnownLocation.get_known_location(location, 'region')
71+
if known_location
72+
location = known_location
73+
else
74+
location = KnownLocation.guess_known_location(location, 'region')
75+
context['guess_location'] = location
76+
end
6177

6278
if location
6379
context['24HoursForecast'] = search_24HoursForecast(location)
@@ -76,7 +92,15 @@ def initialize
7692
entities = request['entities']
7793

7894
location = first_entity_value(entities, 'location') || context['location']
79-
intent = first_entity_value(entities, 'intent') || context['intent']
95+
# intent = first_entity_value(entities, 'intent') || context['intent']
96+
97+
known_location = KnownLocation.get_known_location(location, 'region')
98+
if known_location
99+
location = known_location
100+
else
101+
location = KnownLocation.guess_known_location(location, 'region')
102+
context['guess_location'] = location
103+
end
80104

81105
if location
82106
hour_psi = search_hour_psi(location)

‎app/models/known_location.rb

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,50 @@
1+
# frozen_string_literal: true
2+
require 'fuzzystringmatch'
3+
14
class KnownLocation
25
include Mongoid::Document
6+
before_save :name_downcase
7+
38
field :name, type: String
49
field :type, type: String
510
field :lat, type: Float
611
field :lon, type: Float
712

813
validates_uniqueness_of :name
914

10-
def self.known_region?(location)
15+
def name_downcase
16+
self.name = name.downcase
17+
end
18+
19+
def self.get_known_location(location, type)
20+
return nil if location.blank?
21+
22+
regex_str = Regexp.new(location.downcase)
23+
known_location = KnownLocation.where(name: regex_str, type: type).first
24+
if known_location.present?
25+
return known_location.name.capitalize
26+
else
27+
return nil
28+
end
1129
end
1230

13-
def self.known_location?(location)
31+
def self.guess_known_location(location, type)
32+
return nil if location.blank?
33+
34+
jarow = FuzzyStringMatch::JaroWinkler.create(:native)
35+
36+
location = location.downcase
37+
known_locations = KnownLocation.where(type: type).only(:name).pluck(:name)
38+
known_locations_with_distance = {}
39+
known_locations.each do |known_location|
40+
known_locations_with_distance[known_location] = jarow.getDistance(known_location, location)
41+
end
42+
43+
known_location = known_locations_with_distance.max_by { |_name, distance| distance }
44+
if known_location[1] < 0.7
45+
return nil
46+
else
47+
return known_location[0].capitalize
48+
end
1449
end
1550
end

‎app/models/message.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ class Message
1111
belongs_to :conversation
1212

1313
validates_inclusion_of :kind, in: %w(outgoing incoming), allow_nil: false
14-
validates :conversation, :presence => true
14+
validates :conversation, presence: true
1515

1616
def digest
17-
if self.quick_replies.present?
17+
if quick_replies.present?
1818
quick_replies = self.quick_replies.map { |qr| { title: qr.title, content_type: qr.content_type, payload: qr.payload } }
1919
end
2020

21-
if /FACEBOOK_TEMPLATE_LIST/ === self.body
21+
if /FACEBOOK_TEMPLATE_LIST/ === body
2222
body = self.body
2323
body.slice!('FACEBOOK_TEMPLATE_LIST:')
2424
reponse_message = JSON.parse(body)
@@ -29,7 +29,7 @@ def digest
2929
}
3030
end
3131

32-
deliver(reponse_message, self.conversation.uid)
32+
deliver(reponse_message, conversation.uid)
3333
end
3434

3535
private

‎db/seeds.rb

+54-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,55 @@
11
# frozen_string_literal: true
2-
# This file should contain all the record creation needed to seed the database with its default values.
3-
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
4-
#
5-
# Examples:
6-
#
7-
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
8-
# Character.create(name: 'Luke', movie: movies.first)
2+
KnownLocation.create(lat: 1.375, lon: 103.839, name: 'ang mo kio', type: 'location')
3+
KnownLocation.create(lat: 1.321, lon: 103.924, name: 'bedok', type: 'location')
4+
KnownLocation.create(lat: 1.350772, lon: 103.839, name: 'bishan', type: 'location')
5+
KnownLocation.create(lat: 1.304, lon: 103.701, name: 'boon lay', type: 'location')
6+
KnownLocation.create(lat: 1.353, lon: 103.754, name: 'bukit batok', type: 'location')
7+
KnownLocation.create(lat: 1.277, lon: 103.819, name: 'bukit merah', type: 'location')
8+
KnownLocation.create(lat: 1.362, lon: 103.77195, name: 'bukit panjang', type: 'location')
9+
KnownLocation.create(lat: 1.325, lon: 103.791, name: 'bukit timah', type: 'location')
10+
KnownLocation.create(lat: 1.38, lon: 103.805, name: 'central water catchment', type: 'location')
11+
KnownLocation.create(lat: 1.357, lon: 103.987, name: 'changi', type: 'location')
12+
KnownLocation.create(lat: 1.377, lon: 103.745, name: 'choa chu kang', type: 'location')
13+
KnownLocation.create(lat: 1.315, lon: 103.76, name: 'clementi', type: 'location')
14+
KnownLocation.create(lat: 1.292, lon: 103.844, name: 'city', type: 'location')
15+
KnownLocation.create(lat: 1.318, lon: 103.884, name: 'geylang', type: 'location')
16+
KnownLocation.create(lat: 1.361218, lon: 103.886, name: 'hougang', type: 'location')
17+
KnownLocation.create(lat: 1.347, lon: 103.67, name: 'jalan bahar', type: 'location')
18+
KnownLocation.create(lat: 1.326, lon: 103.737, name: 'jurong east', type: 'location')
19+
KnownLocation.create(lat: 1.266, lon: 103.699, name: 'jurong island', type: 'location')
20+
KnownLocation.create(lat: 1.34039, lon: 103.705, name: 'jurong west', type: 'location')
21+
KnownLocation.create(lat: 1.312, lon: 103.862, name: 'kallang', type: 'location')
22+
KnownLocation.create(lat: 1.423, lon: 103.717332, name: 'lim chu kang', type: 'location')
23+
KnownLocation.create(lat: 1.419, lon: 103.812, name: 'mandai', type: 'location')
24+
KnownLocation.create(lat: 1.297, lon: 103.891, name: 'marine parade', type: 'location')
25+
KnownLocation.create(lat: 1.327, lon: 103.826, name: 'novena', type: 'location')
26+
KnownLocation.create(lat: 1.37, lon: 103.948, name: 'pasir ris', type: 'location')
27+
KnownLocation.create(lat: 1.358, lon: 103.914, name: 'paya lebar', type: 'location')
28+
KnownLocation.create(lat: 1.315, lon: 103.675, name: 'pioneer', type: 'location')
29+
KnownLocation.create(lat: 1.403, lon: 104.053, name: 'pulau tekong', type: 'location')
30+
KnownLocation.create(lat: 1.404, lon: 103.96, name: 'pulau ubin', type: 'location')
31+
KnownLocation.create(lat: 1.401, lon: 103.904, name: 'punggol', type: 'location')
32+
KnownLocation.create(lat: 1.291, lon: 103.78576, name: 'queenstown', type: 'location')
33+
KnownLocation.create(lat: 1.404, lon: 103.869, name: 'seletar', type: 'location')
34+
KnownLocation.create(lat: 1.445, lon: 103.818495, name: 'sembawang', type: 'location')
35+
KnownLocation.create(lat: 1.384, lon: 103.891443, name: 'sengkang', type: 'location')
36+
KnownLocation.create(lat: 1.243, lon: 103.832, name: 'sentosa', type: 'location')
37+
KnownLocation.create(lat: 1.357, lon: 103.865, name: 'serangoon', type: 'location')
38+
KnownLocation.create(lat: 1.208, lon: 103.842, name: 'southern islands', type: 'location')
39+
KnownLocation.create(lat: 1.413, lon: 103.756, name: 'sungei kadut', type: 'location')
40+
KnownLocation.create(lat: 1.345, lon: 103.944, name: 'tampines', type: 'location')
41+
KnownLocation.create(lat: 1.308, lon: 103.813, name: 'tanglin', type: 'location')
42+
KnownLocation.create(lat: 1.374, lon: 103.715, name: 'tengah', type: 'location')
43+
KnownLocation.create(lat: 1.334304, lon: 103.856327, name: 'toa payoh', type: 'location')
44+
KnownLocation.create(lat: 1.294947, lon: 103.635, name: 'tuas', type: 'location')
45+
KnownLocation.create(lat: 1.205926, lon: 103.746, name: 'western islands', type: 'location')
46+
KnownLocation.create(lat: 1.405, lon: 103.689, name: 'western water catchment', type: 'location')
47+
KnownLocation.create(lat: 1.432, lon: 103.786528, name: 'woodlands', type: 'location')
48+
KnownLocation.create(lat: 1.418, lon: 103.839, name: 'yishun', type: 'location')
49+
KnownLocation.create(lat: nil, lon: nil, name: 'north', type: 'region')
50+
KnownLocation.create(lat: nil, lon: nil, name: 'south', type: 'region')
51+
KnownLocation.create(lat: nil, lon: nil, name: 'east', type: 'region')
52+
KnownLocation.create(lat: nil, lon: nil, name: 'west', type: 'region')
53+
KnownLocation.create(lat: nil, lon: nil, name: 'center', type: 'region')
54+
KnownLocation.create(lat: nil, lon: nil, name: 'centre', type: 'region')
55+
KnownLocation.create(lat: nil, lon: nil, name: 'central', type: 'region')

‎test/models/known_location_test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
require 'test_helper'
23

34
class KnownLocationTest < ActiveSupport::TestCase

0 commit comments

Comments
 (0)
Please sign in to comment.