People: Shorten names in titles #22
This commit is contained in:
parent
03b799a6ca
commit
f7cc61edbc
5 changed files with 82 additions and 21 deletions
|
@ -53,7 +53,7 @@ func (m *Photo) UpdateTitle(labels classify.Labels) error {
|
|||
m.UpdateDescription(people)
|
||||
|
||||
if n := len(people); n > 0 && n < 4 {
|
||||
names = txt.JoinNames(people)
|
||||
names = txt.JoinNames(people, true)
|
||||
}
|
||||
|
||||
if m.LocationLoaded() {
|
||||
|
@ -204,7 +204,7 @@ func (m *Photo) UpdateDescription(people []string) {
|
|||
|
||||
// Add subject names to description when there's more than one person.
|
||||
if len(people) > 3 {
|
||||
m.PhotoDescription = txt.JoinNames(people)
|
||||
m.PhotoDescription = txt.JoinNames(people, false)
|
||||
} else {
|
||||
m.PhotoDescription = ""
|
||||
}
|
||||
|
|
|
@ -162,7 +162,7 @@ func TestPhoto_UpdateTitle(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, "Franzilein & Actress A / 2008", m.PhotoTitle)
|
||||
assert.Equal(t, "Franzilein & Actress / 2008", m.PhotoTitle)
|
||||
})
|
||||
t.Run("no location", func(t *testing.T) {
|
||||
m := PhotoFixtures.Get("Photo01")
|
||||
|
@ -243,7 +243,7 @@ func TestPhoto_UpdateTitle(t *testing.T) {
|
|||
|
||||
assert.Equal(t, SrcAuto, m.TitleSrc)
|
||||
assert.Equal(t, SrcAuto, m.DescriptionSrc)
|
||||
assert.Equal(t, "Corn McCornface & Jens Mander / 2014", m.PhotoTitle)
|
||||
assert.Equal(t, "Corn & Jens / Germany / 2014", m.PhotoTitle)
|
||||
assert.Equal(t, "", m.PhotoDescription)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -139,7 +139,7 @@ func Photos(f form.PhotoSearch) (results PhotoResults, count int, err error) {
|
|||
if f.Query != "" && f.Subject == "" {
|
||||
if subj, names, remaining := SubjectUIDs(f.Query); len(subj) > 0 {
|
||||
f.Subject = strings.Join(subj, txt.And)
|
||||
log.Debugf("people: searching for %s", txt.Quote(txt.JoinNames(names)))
|
||||
log.Debugf("people: searching for %s", txt.Quote(txt.JoinNames(names, false)))
|
||||
f.Query = remaining
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ func UniqueNames(names []string) (result []string) {
|
|||
}
|
||||
|
||||
// JoinNames joins a list of names to be used in titles and descriptions.
|
||||
func JoinNames(names []string) (result string) {
|
||||
func JoinNames(names []string, shorten bool) (result string) {
|
||||
l := len(names)
|
||||
|
||||
if l == 0 {
|
||||
|
@ -33,27 +33,49 @@ func JoinNames(names []string) (result string) {
|
|||
return names[0]
|
||||
}
|
||||
|
||||
var familySuffix string
|
||||
var familyName string
|
||||
|
||||
// Common family name?
|
||||
if i := strings.LastIndex(names[0], " "); i > 1 && len(names[0][i:]) > 2 {
|
||||
familySuffix = names[0][i:]
|
||||
familyName = names[0][i:]
|
||||
|
||||
for i := 1; i < l; i++ {
|
||||
if !strings.HasSuffix(names[i], familySuffix) {
|
||||
familySuffix = ""
|
||||
if !strings.HasSuffix(names[i], familyName) {
|
||||
familyName = ""
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
if l == 2 {
|
||||
result = strings.Join(names, " & ")
|
||||
} else {
|
||||
result = fmt.Sprintf("%s & %s", strings.Join(names[:l-1], ", "), names[l-1])
|
||||
}
|
||||
|
||||
if familySuffix != "" {
|
||||
result = strings.Replace(result, familySuffix, "", l-1)
|
||||
// Keep family name at the end.
|
||||
if familyName != "" {
|
||||
if shorten {
|
||||
result = result + familyName
|
||||
} else {
|
||||
result = strings.Replace(result, familyName, "", l-1)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
|
|
|
@ -22,42 +22,81 @@ func TestUniqueNames(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestJoinNames(t *testing.T) {
|
||||
// Regular.
|
||||
t.Run("NoName", func(t *testing.T) {
|
||||
result := JoinNames([]string{})
|
||||
result := JoinNames([]string{}, false)
|
||||
assert.Equal(t, "", result)
|
||||
})
|
||||
t.Run("OneName", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Jens Mander"})
|
||||
result := JoinNames([]string{"Jens Mander"}, false)
|
||||
assert.Equal(t, "Jens Mander", result)
|
||||
})
|
||||
t.Run("TwoNames", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Jens Mander", "Name 2"})
|
||||
result := JoinNames([]string{"Jens Mander", "Name 2"}, false)
|
||||
assert.Equal(t, "Jens Mander & Name 2", result)
|
||||
})
|
||||
t.Run("ThreeNames", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Jens Mander", "Name 2", "Name 3"})
|
||||
result := JoinNames([]string{"Jens Mander", "Name 2", "Name 3"}, false)
|
||||
assert.Equal(t, "Jens Mander, Name 2 & Name 3", result)
|
||||
})
|
||||
t.Run("ManyNames", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Jens Mander", "Name 2", "Name 3", "Name 4"})
|
||||
result := JoinNames([]string{"Jens Mander", "Name 2", "Name 3", "Name 4"}, false)
|
||||
assert.Equal(t, "Jens Mander, Name 2, Name 3 & Name 4", result)
|
||||
})
|
||||
t.Run("Partners", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Jens Mander", "Jane Mander"})
|
||||
result := JoinNames([]string{"Jens Mander", "Jane Mander"}, false)
|
||||
assert.Equal(t, "Jens & Jane Mander", result)
|
||||
})
|
||||
t.Run("Family", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Anna Mander", "Jens Mander", "Jane Mander"})
|
||||
result := JoinNames([]string{"Anna Mander", "Jens Mander", "Jane Mander"}, false)
|
||||
assert.Equal(t, "Anna, Jens & Jane Mander", result)
|
||||
})
|
||||
t.Run("ShortFamilyName", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Anna M", "Jens M", "Jane M"})
|
||||
result := JoinNames([]string{"Anna M", "Jens M", "Jane M"}, false)
|
||||
assert.Equal(t, "Anna M, Jens M & Jane M", result)
|
||||
})
|
||||
t.Run("NoFamily", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Anna Mander", "Jane Mander", "Bill Gates"})
|
||||
result := JoinNames([]string{"Anna Mander", "Jane Mander", "Bill Gates"}, false)
|
||||
assert.Equal(t, "Anna Mander, Jane Mander & Bill Gates", result)
|
||||
})
|
||||
|
||||
// Short.
|
||||
t.Run("NoName", func(t *testing.T) {
|
||||
result := JoinNames([]string{}, true)
|
||||
assert.Equal(t, "", result)
|
||||
})
|
||||
t.Run("OneName", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Jens Mander"}, true)
|
||||
assert.Equal(t, "Jens Mander", result)
|
||||
})
|
||||
t.Run("TwoNames", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Jens Mander", "Name 2"}, true)
|
||||
assert.Equal(t, "Jens & Name", result)
|
||||
})
|
||||
t.Run("ThreeNames", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Jens Mander", "Name 2", "Name 3"}, true)
|
||||
assert.Equal(t, "Jens, Name 2 & Name 3", result)
|
||||
})
|
||||
t.Run("ManyNames", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Jens Mander", "Name 2", "Name 3", "Name 4"}, true)
|
||||
assert.Equal(t, "Jens, Name 2, Name 3 & Name 4", result)
|
||||
})
|
||||
t.Run("Partners", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Jens Mander", "Jane Mander"}, true)
|
||||
assert.Equal(t, "Jens & Jane Mander", result)
|
||||
})
|
||||
t.Run("Family", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Anna Mander", "Jens Mander", "Jane Mander"}, true)
|
||||
assert.Equal(t, "Anna, Jens & Jane Mander", result)
|
||||
})
|
||||
t.Run("ShortFamilyName", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Anna M", "Jens M", "Jane M"}, true)
|
||||
assert.Equal(t, "Anna, Jens & Jane", result)
|
||||
})
|
||||
t.Run("NoFamily", func(t *testing.T) {
|
||||
result := JoinNames([]string{"Anna Mander", "Jane Mander", "Bill Gates"}, true)
|
||||
assert.Equal(t, "Anna, Jane & Bill", result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNameKeywords(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue