-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_location.go
129 lines (106 loc) · 3.47 KB
/
find_location.go
1
2
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
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
61
62
63
64
65
66
67
68
69
70
71
72
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package findlocation
import "strings"
type LocationFinder struct {
CountriesAndCodes map[string]string
CitiesWithPopulationsOver15000 map[string]string
AlternativeNamesForCitiesAndCountries map[string]string
UsStates []string
UkCounties []string
CanadianProvincesAndCodes map[string]string
AustralianStatesAndCodes map[string]string
UsStateCodes []string
}
func NewLocationFinder() *LocationFinder {
return &LocationFinder {
AlternativeNamesForCitiesAndCountries: loadTwoColumnFile("data/alternative_names_for_cities_and_countries.tsv"),
AustralianStatesAndCodes: loadTwoColumnFile("data/australian_states_and_codes.tsv"),
CanadianProvincesAndCodes: loadTwoColumnFile("data/canadian_provinces_and_codes.tsv"),
CitiesWithPopulationsOver15000: loadTwoColumnFile("data/cities_with_populations_over_15000.tsv"),
CountriesAndCodes: loadTwoColumnFile("data/countries_and_codes.tsv"),
UkCounties: loadSingleColumnFile("data/uk_counties.txt"),
UsStates: loadSingleColumnFile("data/us_states.txt"),
UsStateCodes: loadSingleColumnFile("data/us_state_codes.txt"),
}
}
func (lf *LocationFinder) FindLocation(search string) string {
splitSearchString := strings.Split(search, " ")
// Search for the presence of a country name
searchResult := searchMultiColumnByKey(lf.CountriesAndCodes, splitSearchString)
if searchResult != "" {
return searchResult
}
// Search through a list of cities with populations over 15,000
searchResult = searchMultiColumnByKey(lf.CitiesWithPopulationsOver15000, splitSearchString)
if searchResult != "" {
return searchResult
}
// Search through a list alternative/non-English names for cities/countries
searchResult = searchMultiColumnByKey(lf.AlternativeNamesForCitiesAndCountries, splitSearchString)
if searchResult != "" {
return searchResult
}
// Search for state/province/county name
searchResult = searchSingleColumn(lf.UsStates, splitSearchString)
if searchResult != "" {
return "US"
}
searchResult = searchSingleColumn(lf.UkCounties, splitSearchString)
if searchResult != "" {
return "GB"
}
searchResult = searchMultiColumnByKey(lf.CanadianProvincesAndCodes, splitSearchString)
if searchResult != "" {
return "CA"
}
searchResult = searchMultiColumnByKey(lf.AustralianStatesAndCodes, splitSearchString)
if searchResult != "" {
return "AU"
}
// Search for abbreviations/codes
searchResult = searchMultiColumnByValue(lf.CountriesAndCodes, splitSearchString)
if searchResult != "" {
return searchResult
}
searchResult = searchSingleColumn(lf.UsStateCodes, splitSearchString)
if searchResult != "" {
return "US"
}
searchResult = searchMultiColumnByValue(lf.CanadianProvincesAndCodes, splitSearchString)
if searchResult != "" {
return "CA"
}
searchResult = searchMultiColumnByValue(lf.AustralianStatesAndCodes, splitSearchString)
if searchResult != "" {
return "AU"
}
return ""
}
func searchMultiColumnByKey(file map[string]string, searchStrings []string) string {
for _, segment := range searchStrings {
result := file[segment]
if result != "" {
return result
}
}
return ""
}
func searchMultiColumnByValue(file map[string]string, searchStrings []string) string {
for _, segment := range searchStrings {
for _, value := range file {
if value == segment {
return value
}
}
}
return ""
}
func searchSingleColumn(file []string, searchStrings []string) string {
for _, segment := range searchStrings {
for _, value := range file {
if value == segment {
return value
}
}
}
return ""
}