Skip to content
Snippets Groups Projects
Verified Commit ff6403e5 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Improve search functionality

parent 5ab46240
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,8 @@ import ( ...@@ -11,6 +11,8 @@ import (
type AutocompleteStation struct { type AutocompleteStation struct {
Id int64 `json:"stop_id"` Id int64 `json:"stop_id"`
Name string `json:"stop_name"` Name string `json:"stop_name"`
CanonicalizedName string `json:"-"`
FindableName string `json:"-"`
*Position *Position
Distance float64 `json:"stop_distance,omitempty"` Distance float64 `json:"stop_distance,omitempty"`
} }
...@@ -25,18 +27,34 @@ func loadAutocompleteStations() []AutocompleteStation { ...@@ -25,18 +27,34 @@ func loadAutocompleteStations() []AutocompleteStation {
if err = json.NewDecoder(file).Decode(&autocompleteStations); err != nil { if err = json.NewDecoder(file).Decode(&autocompleteStations); err != nil {
log.Fatal(err) log.Fatal(err)
} }
for i := range autocompleteStations {
autocompleteStations[i].CanonicalizedName = canonicalizeName(autocompleteStations[i].Name)
autocompleteStations[i].FindableName = findableName(autocompleteStations[i].Name)
}
if err = file.Close(); err != nil { if err = file.Close(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
return autocompleteStations return autocompleteStations
} }
func canonicalizeName(stationName string) string { func findableName(stationName string) string {
additionalRegex := regexp.MustCompile("\\([^(]*\\)") additionalRegex := regexp.MustCompile("\\([^(]*\\)")
spaceRegex := regexp.MustCompile(" +") spaceRegex := regexp.MustCompile(" +")
stationName = canonicalizeName(stationName)
stationName = additionalRegex.ReplaceAllString(stationName, "") stationName = additionalRegex.ReplaceAllString(stationName, "")
stationName = spaceRegex.ReplaceAllString(stationName, " ") stationName = spaceRegex.ReplaceAllString(stationName, " ")
stationName = strings.TrimSpace(stationName) stationName = strings.TrimSpace(stationName)
stationName = strings.TrimSuffix(stationName, " Hbf") stationName = strings.TrimSuffix(stationName, " hbf")
return stationName
}
func canonicalizeName(stationName string) string {
stationName = strings.ToLower(stationName)
stationName = strings.TrimSpace(stationName)
stationName = strings.ReplaceAll(stationName, "ü", "ue")
stationName = strings.ReplaceAll(stationName, "ä", "ae")
stationName = strings.ReplaceAll(stationName, "ö", "oe")
stationName = strings.ReplaceAll(stationName, "ß", "ss")
return stationName return stationName
} }
...@@ -79,20 +79,22 @@ func main() { ...@@ -79,20 +79,22 @@ func main() {
} }
http.HandleFunc("/autocomplete/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/autocomplete/", func(w http.ResponseWriter, r *http.Request) {
if stationName := strings.TrimSpace(r.FormValue("name")); stationName != "" { if query := canonicalizeName(r.FormValue("name")); query != "" {
var perfectMatch []AutocompleteStation var perfectMatch []AutocompleteStation
var prefix []AutocompleteStation var prefix []AutocompleteStation
var contains []AutocompleteStation var contains []AutocompleteStation
findableQuery := findableName(query)
for _, station := range autocompleteStations { for _, station := range autocompleteStations {
findableName := canonicalizeName(station.Name) if strings.EqualFold(station.CanonicalizedName, query) ||
if strings.EqualFold(station.Name, stationName) { strings.EqualFold(station.FindableName, query) {
perfectMatch = append(perfectMatch, station)
} else if strings.EqualFold(findableName, stationName) {
perfectMatch = append(perfectMatch, station) perfectMatch = append(perfectMatch, station)
} else if strings.HasPrefix(station.Name, stationName) { } else if strings.HasPrefix(station.CanonicalizedName, query) ||
strings.HasPrefix(station.FindableName, findableQuery) {
prefix = append(prefix, station) prefix = append(prefix, station)
} else if strings.Contains(station.Name, stationName) { } else if strings.Contains(station.CanonicalizedName, query) ||
strings.Contains(station.FindableName, findableQuery) {
contains = append(contains, station) contains = append(contains, station)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment