Backend: Improve country code detection

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-06-12 18:01:22 +02:00
parent 374ea03b10
commit 01717ae6f7
5 changed files with 55 additions and 52 deletions

View file

@ -26,7 +26,7 @@ func TestPhoto_EstimateCountry(t *testing.T) {
})
t.Run("de", func(t *testing.T) {
m := Photo{PhotoName: "flughafen", PhotoPath: "2020/Berlin", OriginalName: "Flughafen BER.jpg"}
m := Photo{PhotoName: "Brauhaus", PhotoPath: "2020/Bayern", OriginalName: "München.jpg"}
assert.Equal(t, UnknownCountry.ID, m.CountryCode())
assert.Equal(t, UnknownCountry.CountryName, m.CountryName())
m.EstimateCountry()
@ -42,6 +42,4 @@ func TestPhoto_EstimateCountry(t *testing.T) {
assert.Equal(t, "ca", m.CountryCode())
assert.Equal(t, "Canada", m.CountryName())
})
//OriginalName: "demo/Toronto/port-lands--gardiner-expressway--toronto_7999515645_o.jpg"
}

View file

@ -186,13 +186,14 @@ func CountryCode(s string) string {
return "zz"
}
r := strings.NewReplacer("--", " / ", "_", " ", "-", " ")
r := strings.NewReplacer("\\", " ", "/", " ", "_", " ", "-", " ")
s = r.Replace(s)
s = strings.ToLower(s)
s = strings.ReplaceAll(s, " ", " ")
words := strings.Fields(s)
for keyword, code := range Countries {
if strings.Contains(s, keyword) {
for _, w := range words {
search := strings.ToLower(strings.Trim(w, ":.,;!?/-_"))
if code, ok := Countries[search]; ok {
return code
}
}

View file

@ -197,14 +197,44 @@ func TestCountryCode(t *testing.T) {
assert.Equal(t, "gb", result)
})
t.Run("reunion island", func(t *testing.T) {
result := CountryCode("Reunion-Island-2019")
assert.Equal(t, "zz", result)
})
t.Run("reunion island france", func(t *testing.T) {
result := CountryCode("Reunion-Island-france-2019")
assert.Equal(t, "fr", result)
})
t.Run("réunion", func(t *testing.T) {
result := CountryCode("My-RéunioN-2019")
assert.Equal(t, "fr", result)
})
t.Run("NYC", func(t *testing.T) {
result := CountryCode("NYC 2019")
assert.Equal(t, "us", result)
})
t.Run("Scuba", func(t *testing.T) {
result := CountryCode("Scuba 2019")
assert.Equal(t, "zz", result)
})
t.Run("Cuba", func(t *testing.T) {
result := CountryCode("Cuba 2019")
assert.Equal(t, "cu", result)
})
t.Run("San Francisco", func(t *testing.T) {
result := CountryCode("San Francisco 2019")
assert.Equal(t, "us", result)
assert.Equal(t, "zz", result)
})
t.Run("U.S.A.", func(t *testing.T) {
result := CountryCode("Born in the U.S.A. is a song written and performed by Bruce Springsteen...")
assert.Equal(t, "us", result)
assert.Equal(t, "zz", result)
})
t.Run("US", func(t *testing.T) {

View file

@ -55,34 +55,28 @@ var Countries = map[string]string{
"brasil": "br",
"russia": "ru",
"ukraine": "ua",
"united kingdom": "gb",
"unitedkingdom": "gb",
"london": "gb",
"scotland": "gb",
"edinburgh": "gb",
"england": "gb",
"u.k.": "gb",
"northern ireland": "gb",
"great britain": "gb",
"buckingham palace": "gb",
"britain": "gb",
"buckingham": "gb",
"isle of man": "im",
"ireland": "ie",
"dublin": "ie",
"deutsche demokratische": "de",
"deutschland": "de",
"germany": "de",
"aachen": "de",
"steglitz": "de",
"friedrichshain": "de",
"berghain": "de",
"raw gelände": "de",
"bundestag": "de",
"grunewald": "de",
"charlottenburg": "de",
"wilmersdorf": "de",
"tempelhof": "de",
"schönefeld": "de",
"airport ber": "de",
"flughafen ber": "de",
"reichstag": "de",
"kreuzberg": "de",
"ostsee": "de",
@ -96,8 +90,8 @@ var Countries = map[string]string{
"bisingen": "de",
"balingen": "de",
"hechingen": "de",
"swabian jura": "de",
"schwäbische alb": "de",
"swabian": "de",
"schwäbische": "de",
"albtrauf": "de",
"münchen": "de",
"württemberg": "de",
@ -164,7 +158,7 @@ var Countries = map[string]string{
"houston": "us",
"las vegas": "us",
"minneapolis": "us",
"kansas city": "us",
"kansas": "us",
"wisconsin": "us",
"michigan": "us",
"illinois": "us",
@ -406,9 +400,7 @@ var Countries = map[string]string{
"ascension": "sh",
"tristan da cunha": "sh",
"south sandwich island": "gs",
"island of bermuda": "bm",
"islands of bermuda": "bm",
"bermuda island": "bm",
"bermuda": "bm",
"british indian ocean": "io",
"british indian territory": "io",
"thailand": "th",
@ -427,19 +419,14 @@ var Countries = map[string]string{
"pitcairn island": "pn",
"tokelau": "tk",
"tonga": "to",
"kingdom of tonga": "to",
"ile de la reunion": "fr",
"reunion island": "fr",
"réunion": "fr",
"piton de la fournaise": "fr",
"piton des neiges": "fr",
"strassburg": "fr",
"strasbourg": "fr",
"straßburg": "fr",
"paris": "fr",
"netherlands": "nl",
"holland": "nl",
"republic of chad": "td",
"chad": "td",
"bosnia": "ba",
"herzegovina": "ba",
"norway": "no",

View file

@ -51,34 +51,28 @@ BR:Brazil
BR:Brasil
RU:Russia
UA:Ukraine
GB:United Kingdom
GB:UnitedKingdom
GB:London
GB:Scotland
GB:Edinburgh
GB:England
GB:U.K.
GB:Northern Ireland
GB:Great Britain
GB:Buckingham Palace
GB:Britain
GB:Buckingham
IM:Isle of Man
IE:Ireland
IE:Dublin
DE:Deutsche Demokratische
DE:Deutschland
DE:Germany
DE:Aachen
DE:Steglitz
DE:Friedrichshain
DE:Berghain
DE:RAW Gelände
DE:Bundestag
DE:Grunewald
DE:Charlottenburg
DE:Wilmersdorf
DE:Tempelhof
DE:Schönefeld
DE:Airport BER
DE:Flughafen BER
DE:Reichstag
DE:Kreuzberg
DE:Ostsee
@ -92,8 +86,8 @@ DE:Stuttgart
DE:Bisingen
DE:Balingen
DE:Hechingen
DE:Swabian Jura
DE:Schwäbische Alb
DE:Swabian
DE:Schwäbische
DE:Albtrauf
DE:München
DE:Württemberg
@ -160,7 +154,7 @@ US:Dallas
US:Houston
US:Las Vegas
US:Minneapolis
US:Kansas City
US:Kansas
US:Wisconsin
US:Michigan
US:Illinois
@ -402,9 +396,7 @@ SH:Saint Helena
SH:Ascension
SH:Tristan da Cunha
GS:South Sandwich Island
BM:Island of Bermuda
BM:Islands of Bermuda
BM:Bermuda Island
BM:Bermuda
IO:British Indian Ocean
IO:British Indian Territory
TH:Thailand
@ -423,19 +415,14 @@ FK:Falkland Island
PN:Pitcairn Island
TK:Tokelau
TO:Tonga
TO:Kingdom of Tonga
FR:ile de la Reunion
FR:Reunion Island
FR:Réunion
FR:Piton de la Fournaise
FR:Piton des Neiges
FR:Strassburg
FR:Strasbourg
FR:Straßburg
FR:Paris
NL:Netherlands
NL:Holland
TD:Republic of Chad
TD:Chad
BA:Bosnia
BA:Herzegovina
NO:Norway