Metadata: Improve country code from string estimate
This commit is contained in:
parent
96cb6eeed6
commit
e57e9aff70
2 changed files with 22 additions and 9 deletions
|
@ -6,6 +6,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
var UnknownCountry = "zz"
|
||||
var CountryWordsRegexp = regexp.MustCompile("[\\p{L}]{2,}")
|
||||
|
||||
// Int returns a string as int or 0 if it can not be converted.
|
||||
|
@ -39,9 +40,11 @@ func IsUInt(s string) bool {
|
|||
}
|
||||
|
||||
// CountryCode tries to find a matching country code for a given string e.g. from a file oder directory name.
|
||||
func CountryCode(s string) string {
|
||||
if s == "zz" {
|
||||
return "zz"
|
||||
func CountryCode(s string) (code string) {
|
||||
code = UnknownCountry
|
||||
|
||||
if s == "" || s == UnknownCountry {
|
||||
return code
|
||||
}
|
||||
|
||||
words := CountryWordsRegexp.FindAllString(s, -1)
|
||||
|
@ -50,17 +53,17 @@ func CountryCode(s string) string {
|
|||
if i < len(words)-1 {
|
||||
search := strings.ToLower(w + " " + words[i+1])
|
||||
|
||||
if code, ok := Countries[search]; ok {
|
||||
return code
|
||||
if match, ok := Countries[search]; ok {
|
||||
return match
|
||||
}
|
||||
}
|
||||
|
||||
search := strings.ToLower(w)
|
||||
|
||||
if code, ok := Countries[search]; ok {
|
||||
return code
|
||||
if match, ok := Countries[search]; ok {
|
||||
code = match
|
||||
}
|
||||
}
|
||||
|
||||
return "zz"
|
||||
return code
|
||||
}
|
||||
|
|
|
@ -119,10 +119,20 @@ func TestCountryCode(t *testing.T) {
|
|||
assert.Equal(t, "zz", result)
|
||||
})
|
||||
|
||||
t.Run("2018/Oktober 2018/1.-7. Oktober 2018 Berlin/_MG_9831-112.jpg", func(t *testing.T) {
|
||||
t.Run("full path", func(t *testing.T) {
|
||||
result := CountryCode("2018/Oktober 2018/1.-7. Oktober 2018 Berlin/_MG_9831-112.jpg")
|
||||
assert.Equal(t, "de", result)
|
||||
})
|
||||
|
||||
t.Run("little italy montreal", func(t *testing.T) {
|
||||
result := CountryCode("Little Italy Montreal")
|
||||
assert.Equal(t, "ca", result)
|
||||
})
|
||||
|
||||
t.Run("little montreal italy", func(t *testing.T) {
|
||||
result := CountryCode("Little Montreal Italy")
|
||||
assert.Equal(t, "it", result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIsUInt(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue