2020-07-06 11:31:03 +02:00
|
|
|
|
package txt
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
|
)
|
|
|
|
|
|
2020-12-30 13:59:27 +01:00
|
|
|
|
var FileTitleRegexp = regexp.MustCompile("[\\p{L}\\-,':&+!?!]{1,}|( [&+] )?")
|
2020-07-06 11:31:03 +02:00
|
|
|
|
|
|
|
|
|
// FileTitle returns the string with the first characters of each word converted to uppercase.
|
|
|
|
|
func FileTitle(s string) string {
|
2020-07-14 11:00:49 +02:00
|
|
|
|
s = fs.BasePrefix(s, true)
|
2020-07-06 11:31:03 +02:00
|
|
|
|
|
2020-12-27 16:37:28 +01:00
|
|
|
|
if len(s) < 3 && IsASCII(s) {
|
2020-07-06 11:31:03 +02:00
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
words := FileTitleRegexp.FindAllString(s, -1)
|
|
|
|
|
var result []string
|
|
|
|
|
|
|
|
|
|
found := 0
|
|
|
|
|
|
|
|
|
|
for _, w := range words {
|
|
|
|
|
w = strings.ToLower(w)
|
|
|
|
|
|
2020-12-27 16:37:28 +01:00
|
|
|
|
if IsASCII(w) && (len(w) < 3 && found == 0 || len(w) == 1) {
|
2020-07-06 11:31:03 +02:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, ok := StopWords[w]; ok && found == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if UnknownWord(w) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = append(result, w)
|
|
|
|
|
|
|
|
|
|
found++
|
|
|
|
|
|
|
|
|
|
if found > 10 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if found == 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
title := strings.Join(result, " ")
|
|
|
|
|
|
|
|
|
|
title = strings.ReplaceAll(title, "--", " / ")
|
|
|
|
|
title = strings.ReplaceAll(title, "-", " ")
|
|
|
|
|
title = strings.ReplaceAll(title, " ", " ")
|
|
|
|
|
|
2021-01-27 15:07:00 +01:00
|
|
|
|
// Remove small words from title ending.
|
|
|
|
|
for w, _ := range SmallWords {
|
|
|
|
|
title = strings.TrimSuffix(title, " "+w)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-27 16:37:28 +01:00
|
|
|
|
if len(title) <= 4 && IsASCII(title) {
|
2020-07-06 11:31:03 +02:00
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Title(title)
|
|
|
|
|
}
|