Backend: Improve labels and title capitalization

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-06-01 13:22:19 +02:00
parent 973911fabf
commit 716012aa8b
4 changed files with 64 additions and 20 deletions

View file

@ -4017,9 +4017,9 @@ var rules = LabelRules{
Categories: []string{"animal"},
},
"packet": {
Label: "package",
Threshold: 0.000000,
Priority: -1,
Label: "",
Threshold: 1.000000,
Priority: -2,
Categories: []string{},
},
"paddle": {

View file

@ -4020,8 +4020,7 @@ hourglass:
threshold: 0.6
packet:
label: package
priority: -1
see: ignore
swing:
label: moment

View file

@ -8,7 +8,7 @@ import (
"github.com/photoprism/photoprism/pkg/fs"
)
var FileTitleRegexp = regexp.MustCompile("[\\p{L}\\-]{2,}")
var FileTitleRegexp = regexp.MustCompile("[\\p{L}\\-,':]{2,}")
var SpecialWords = map[string]string{
"nyc": "NYC",
@ -43,7 +43,40 @@ var SpecialWords = map[string]string{
"iphone": "iPhone",
"imac": "iMac",
"ipad": "iPad",
"ipod": "iPod",
"macbook": "MacBook",
"airplay": "AirPlay",
"airpods": "AirPods",
"youtube": "YouTube",
"photoprism": "PhotoPrism",
"macgyver": "MacGyver",
"o'brien": "O'Brien",
"mcgregor": "McGregor",
"mcdonald": "McDonald",
"mcdonalds": "McDonald's",
"mcdonald's": "McDonald's",
"macalister": "MacAlister",
"mcalister": "McAlister",
"mcallister": "McAllister",
"macauley": "MacAuley",
"mccauley": "McCauley",
"mcawley": "McAwley",
"macauliffe": "MacAuliffe",
"macbride": "MacBride",
"mcbride": "McBride",
"maccabe": "MacCabe",
"mccabe": "McCabe",
"maccann": "MacCann",
"mccann": "McCann",
"maccarthy": "MacCarthy",
"mccarthy": "McCarthy",
"maccormack": "MacCormack",
"mccormick": "McCormick",
"maccullagh": "MacCullagh",
"macnully": "MacNully",
"mackenna": "MacKenna",
"macnamara": "MacNamara",
"mcnamara": "McNamara",
"gelaende": "Gelände",
"schwaebisch": "Schwäbisch",
"schwaebische": "Schwäbische",
@ -70,15 +103,19 @@ var SpecialWords = map[string]string{
var SmallWords = map[string]bool{
"a": true,
"an": true,
"as": true,
"at": true,
"by": true,
"in": true,
"of": true,
"on": true,
"or": true,
"up": true,
"to": true,
"by": true,
"and": true,
"but": true,
"for": true,
"nor": true,
"the": true,
"from": true,
"with": true,
@ -131,9 +168,11 @@ func Title(s string) string {
}
for i, w := range words {
if match, ok := SpecialWords[strings.ToLower(w)]; ok {
words[i] = match
} else if i > 0 && SmallWords[strings.ToLower(w)] {
search := strings.ToLower(strings.Trim(w, ":.,;!?"))
if match, ok := SpecialWords[search]; ok {
words[i] = strings.Replace(strings.ToLower(w), search, match, 1)
} else if i > 0 && SmallWords[search] {
words[i] = strings.ToLower(w)
} else {
prev := ' '
@ -184,7 +223,7 @@ func TitleFromFileName(s string) string {
found++
if found >= 10 {
if found > 10 {
break
}
}

View file

@ -47,13 +47,13 @@ func TestUcFirst(t *testing.T) {
func TestTitle(t *testing.T) {
t.Run("BrowseYourLife", func(t *testing.T) {
assert.Equal(t, "Browse Your Life In Pictures", Title("Browse your life in pictures"))
assert.Equal(t, "Browse Your Life in Pictures", Title("Browse your life in pictures"))
})
t.Run("PhotoLover", func(t *testing.T) {
assert.Equal(t, "Photo-Lover", Title("photo-lover"))
})
t.Run("NaomiWatts", func(t *testing.T) {
assert.Equal(t, "Naomi Watts / Ewan Mcgregor / The Impossible / TIFF", Title(" /Naomi watts / Ewan Mcgregor / the Impossible / TIFF "))
assert.Equal(t, "Naomi Watts / Ewan McGregor / The Impossible / TIFF", Title(" /Naomi watts / Ewan Mcgregor / the Impossible / TIFF "))
})
t.Run("Penguin", func(t *testing.T) {
assert.Equal(t, "A Boulders Penguin Colony / Simon's Town / 2013", Title("A Boulders Penguin Colony /// Simon's Town / 2013 "))
@ -67,20 +67,26 @@ func TestTitle(t *testing.T) {
t.Run("testAddLabel", func(t *testing.T) {
assert.Equal(t, "TestAddLabel", Title("testAddLabel"))
})
t.Run("photoprism", func(t *testing.T) {
assert.Equal(t, "PhotoPrism", Title("photoprism"))
})
t.Run("youtube", func(t *testing.T) {
assert.Equal(t, "YouTube", Title("youtube"))
})
}
func TestTitleFromFileName(t *testing.T) {
t.Run("Browse your life in pictures", func(t *testing.T) {
assert.Equal(t, "Browse Your Life In Pictures", TitleFromFileName("Browse your life in pictures"))
t.Run("photoprism", func(t *testing.T) {
assert.Equal(t, "PhotoPrism: Browse Your Life in Pictures", TitleFromFileName("photoprism: Browse your life in pictures"))
})
t.Run("photo-lover", func(t *testing.T) {
t.Run("dash", func(t *testing.T) {
assert.Equal(t, "Photo Lover", TitleFromFileName("photo-lover"))
})
t.Run("BRIDGE in nyc", func(t *testing.T) {
assert.Equal(t, "Bridge In NYC", TitleFromFileName("BRIDGE in nyc"))
t.Run("nyc", func(t *testing.T) {
assert.Equal(t, "Bridge in, or by, NYC", TitleFromFileName("BRIDGE in, or by, nyc"))
})
t.Run("phil unveils iphone, ipad, imac or macbook 11 pro and max", func(t *testing.T) {
assert.Equal(t, "Phil Unveils iPhone iPad iMac or MacBook Pro and Max", TitleFromFileName("phil unveils iphone, ipad, imac or macbook 11 pro and max"))
t.Run("apple", func(t *testing.T) {
assert.Equal(t, "Phil Unveils iPhone, iPad, iPod, 'airpods', Airpod, AirPlay, iMac or MacBook", TitleFromFileName("phil unveils iphone, ipad, ipod, 'airpods', airpod, airplay, imac or macbook 11 pro and max"))
})
t.Run("IMG_4568", func(t *testing.T) {
assert.Equal(t, "", TitleFromFileName("IMG_4568"))