Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bahn-proxy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Janne Mareike Koschinski
bahn-proxy
Commits
ff6403e5
Verified
Commit
ff6403e5
authored
6 years ago
by
Janne Mareike Koschinski
Browse files
Options
Downloads
Patches
Plain Diff
Improve search functionality
parent
5ab46240
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
autocomplete.go
+22
-4
22 additions, 4 deletions
autocomplete.go
main.go
+9
-7
9 additions, 7 deletions
main.go
with
31 additions
and
11 deletions
autocomplete.go
+
22
−
4
View file @
ff6403e5
...
...
@@ -11,6 +11,8 @@ import (
type
AutocompleteStation
struct
{
Id
int64
`json:"stop_id"`
Name
string
`json:"stop_name"`
CanonicalizedName
string
`json:"-"`
FindableName
string
`json:"-"`
*
Position
Distance
float64
`json:"stop_distance,omitempty"`
}
...
...
@@ -25,18 +27,34 @@ func loadAutocompleteStations() []AutocompleteStation {
if
err
=
json
.
NewDecoder
(
file
)
.
Decode
(
&
autocompleteStations
);
err
!=
nil
{
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
{
log
.
Fatal
(
err
)
}
return
autocompleteStations
}
func
canonicaliz
eName
(
stationName
string
)
string
{
func
findabl
eName
(
stationName
string
)
string
{
additionalRegex
:=
regexp
.
MustCompile
(
"
\\
([^(]*
\\
)"
)
spaceRegex
:=
regexp
.
MustCompile
(
" +"
)
stationName
=
canonicalizeName
(
stationName
)
stationName
=
additionalRegex
.
ReplaceAllString
(
stationName
,
""
)
stationName
=
spaceRegex
.
ReplaceAllString
(
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
}
This diff is collapsed.
Click to expand it.
main.go
+
9
−
7
View file @
ff6403e5
...
...
@@ -79,20 +79,22 @@ func main() {
}
http
.
HandleFunc
(
"/autocomplete/"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
stationName
:=
strings
.
TrimSpac
e
(
r
.
FormValue
(
"name"
));
stationName
!=
""
{
if
query
:=
canonicalizeNam
e
(
r
.
FormValue
(
"name"
));
query
!=
""
{
var
perfectMatch
[]
AutocompleteStation
var
prefix
[]
AutocompleteStation
var
contains
[]
AutocompleteStation
findableQuery
:=
findableName
(
query
)
for
_
,
station
:=
range
autocompleteStations
{
findableName
:=
canonicalizeName
(
station
.
Name
)
if
strings
.
EqualFold
(
station
.
Name
,
stationName
)
{
perfectMatch
=
append
(
perfectMatch
,
station
)
}
else
if
strings
.
EqualFold
(
findableName
,
stationName
)
{
if
strings
.
EqualFold
(
station
.
CanonicalizedName
,
query
)
||
strings
.
EqualFold
(
station
.
FindableName
,
query
)
{
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
)
}
else
if
strings
.
Contains
(
station
.
Name
,
stationName
)
{
}
else
if
strings
.
Contains
(
station
.
CanonicalizedName
,
query
)
||
strings
.
Contains
(
station
.
FindableName
,
findableQuery
)
{
contains
=
append
(
contains
,
station
)
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment