2020-05-20 15:10:24 +02:00
|
|
|
package txt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
2020-05-22 16:29:12 +02:00
|
|
|
"strings"
|
2020-05-20 15:10:24 +02:00
|
|
|
)
|
|
|
|
|
2021-11-09 11:42:10 +01:00
|
|
|
var UnknownStateCode = "zz"
|
2021-02-07 17:42:52 +01:00
|
|
|
var UnknownCountryCode = "zz"
|
2020-06-13 07:40:35 +02:00
|
|
|
var CountryWordsRegexp = regexp.MustCompile("[\\p{L}]{2,}")
|
2020-07-11 16:46:29 +02:00
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
// CountryCode tries to find a matching country code for a given string e.g. from a file oder directory name.
|
2021-02-07 16:55:15 +01:00
|
|
|
func CountryCode(s string) (code string) {
|
2021-02-07 17:42:52 +01:00
|
|
|
code = UnknownCountryCode
|
2021-02-07 16:55:15 +01:00
|
|
|
|
2021-02-07 17:42:52 +01:00
|
|
|
if s == "" || s == UnknownCountryCode {
|
2021-02-07 16:55:15 +01:00
|
|
|
return code
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 07:40:35 +02:00
|
|
|
words := CountryWordsRegexp.FindAllString(s, -1)
|
2020-05-28 15:12:18 +02:00
|
|
|
|
2020-06-13 07:40:35 +02:00
|
|
|
for i, w := range words {
|
|
|
|
if i < len(words)-1 {
|
|
|
|
search := strings.ToLower(w + " " + words[i+1])
|
|
|
|
|
2021-02-07 16:55:15 +01:00
|
|
|
if match, ok := Countries[search]; ok {
|
|
|
|
return match
|
2020-06-13 07:40:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
search := strings.ToLower(w)
|
2020-06-12 18:01:22 +02:00
|
|
|
|
2021-02-07 16:55:15 +01:00
|
|
|
if match, ok := Countries[search]; ok {
|
|
|
|
code = match
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-07 16:55:15 +01:00
|
|
|
return code
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|