2021-09-02 14:23:40 +02:00
|
|
|
package txt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UniqueNames removes exact duplicates from a list of strings without changing their order.
|
|
|
|
func UniqueNames(names []string) (result []string) {
|
|
|
|
if len(names) < 1 {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
k := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, n := range names {
|
|
|
|
if _, value := k[n]; !value {
|
|
|
|
k[n] = true
|
|
|
|
result = append(result, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinNames joins a list of names to be used in titles and descriptions.
|
2021-09-18 21:40:57 +02:00
|
|
|
func JoinNames(names []string, shorten bool) (result string) {
|
2021-09-05 22:12:53 +02:00
|
|
|
l := len(names)
|
|
|
|
|
|
|
|
if l == 0 {
|
2021-09-02 14:23:40 +02:00
|
|
|
return ""
|
|
|
|
} else if l == 1 {
|
|
|
|
return names[0]
|
2021-09-05 22:12:53 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 21:40:57 +02:00
|
|
|
var familyName string
|
2021-09-05 22:12:53 +02:00
|
|
|
|
2021-09-18 21:40:57 +02:00
|
|
|
// Common family name?
|
2021-09-05 22:12:53 +02:00
|
|
|
if i := strings.LastIndex(names[0], " "); i > 1 && len(names[0][i:]) > 2 {
|
2021-09-18 21:40:57 +02:00
|
|
|
familyName = names[0][i:]
|
2021-09-05 22:12:53 +02:00
|
|
|
|
|
|
|
for i := 1; i < l; i++ {
|
2021-09-18 21:40:57 +02:00
|
|
|
if !strings.HasSuffix(names[i], familyName) {
|
|
|
|
familyName = ""
|
2021-09-05 22:12:53 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 21:40:57 +02:00
|
|
|
// Shorten names?
|
|
|
|
if shorten {
|
|
|
|
shortNames := make([]string, l)
|
|
|
|
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
shortNames[i] = strings.SplitN(names[i], Space, 2)[0]
|
|
|
|
|
|
|
|
if i > 0 && shortNames[i] == strings.SplitN(names[i-1], Space, 2)[0] {
|
|
|
|
shortNames[i] = names[i]
|
|
|
|
shortNames[i-1] = names[i-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
names = shortNames
|
|
|
|
}
|
|
|
|
|
2021-09-05 22:12:53 +02:00
|
|
|
if l == 2 {
|
|
|
|
result = strings.Join(names, " & ")
|
2021-09-02 14:23:40 +02:00
|
|
|
} else {
|
2021-09-05 22:12:53 +02:00
|
|
|
result = fmt.Sprintf("%s & %s", strings.Join(names[:l-1], ", "), names[l-1])
|
|
|
|
}
|
|
|
|
|
2021-09-18 21:40:57 +02:00
|
|
|
// Keep family name at the end.
|
|
|
|
if familyName != "" {
|
|
|
|
if shorten {
|
|
|
|
result = result + familyName
|
|
|
|
} else {
|
|
|
|
result = strings.Replace(result, familyName, "", l-1)
|
|
|
|
}
|
2021-09-02 14:23:40 +02:00
|
|
|
}
|
2021-09-05 22:12:53 +02:00
|
|
|
|
|
|
|
return result
|
2021-09-02 14:23:40 +02:00
|
|
|
}
|
2021-09-03 16:14:09 +02:00
|
|
|
|
|
|
|
// NameKeywords returns a list of unique, lowercase keywords based on a person's names and aliases.
|
|
|
|
func NameKeywords(names, aliases string) (results []string) {
|
|
|
|
if names == "" && aliases == "" {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
names = strings.ToLower(names)
|
|
|
|
aliases = strings.ToLower(aliases)
|
|
|
|
|
|
|
|
return UniqueNames(append(Words(names), Words(aliases)...))
|
|
|
|
}
|