diff --git a/internal/entity/location.go b/internal/entity/location.go index 95fddc3b7..5182c424b 100644 --- a/internal/entity/location.go +++ b/internal/entity/location.go @@ -87,14 +87,11 @@ func (m *Location) Find(db *gorm.DB, api string) error { // Keywords computes keyword based on a Location func (m *Location) Keywords() (result []string) { - result = append(result, txt.Keywords(m.City())...) - result = append(result, txt.Keywords(m.State())...) - result = append(result, txt.Keywords(m.CountryName())...) + result = append(result, txt.Keywords(txt.ReplaceSpaces(m.City(), "-"))...) + result = append(result, txt.Keywords(txt.ReplaceSpaces(m.State(), "-"))...) + result = append(result, txt.Keywords(txt.ReplaceSpaces(m.CountryName(), "-"))...) result = append(result, txt.Keywords(m.Category())...) - result = append(result, txt.Keywords(m.Name())...) - result = append(result, txt.Keywords(m.Label())...) - result = append(result, txt.Keywords(m.Notes())...) result = txt.UniqueWords(result) diff --git a/internal/photoprism/index_mediafile.go b/internal/photoprism/index_mediafile.go index 784401354..6ba72ccf6 100644 --- a/internal/photoprism/index_mediafile.go +++ b/internal/photoprism/index_mediafile.go @@ -308,7 +308,7 @@ func (ind *Index) MediaFile(m *MediaFile, o IndexOptions, originalName string) ( } w = append(w, locKeywords...) - w = append(w, txt.Keywords(file.OriginalName)...) + w = append(w, txt.FilenameWords(file.OriginalName)...) w = append(w, file.FileMainColor) w = append(w, labels.Keywords()...) diff --git a/internal/remote/discover_test.go b/internal/remote/discover_test.go index 12cfd510e..cfb1bdeda 100644 --- a/internal/remote/discover_test.go +++ b/internal/remote/discover_test.go @@ -14,7 +14,7 @@ func TestDiscover(t *testing.T) { t.Fatal(err) } - assert.Equal(t, "Webdav", r.AccName) + assert.Equal(t, "Webdav-Dummy", r.AccName) assert.Equal(t, "webdav", r.AccType) assert.Equal(t, "http://webdav-dummy/", r.AccURL) assert.Equal(t, "admin", r.AccUser) @@ -28,7 +28,7 @@ func TestDiscover(t *testing.T) { t.Fatal(err) } - assert.Equal(t, "Webdav", r.AccName) + assert.Equal(t, "Webdav-Dummy", r.AccName) assert.Equal(t, "webdav", r.AccType) assert.Equal(t, "http://webdav-dummy/", r.AccURL) assert.Equal(t, "admin", r.AccUser) diff --git a/pkg/fs/base_test.go b/pkg/fs/base_test.go index 1412e439c..1f82f3218 100644 --- a/pkg/fs/base_test.go +++ b/pkg/fs/base_test.go @@ -61,5 +61,4 @@ func TestBaseAbs(t *testing.T) { assert.Equal(t, "/testdata/Test (4)", result) }) - } diff --git a/pkg/txt/words.go b/pkg/txt/words.go index 7092ac38a..a66225ca8 100644 --- a/pkg/txt/words.go +++ b/pkg/txt/words.go @@ -6,13 +6,25 @@ import ( "strings" ) -var KeywordsRegexp = regexp.MustCompile("[\\p{L}]{3,}") +var KeywordsRegexp = regexp.MustCompile("[\\p{L}\\-]{3,}") -// Words returns a slice of words with at least 3 characters from a string. +// Words returns a slice of words with at least 3 characters from a string, dashes count as character ("ile-de-france"). func Words(s string) (results []string) { return KeywordsRegexp.FindAllString(s, -1) } +// ReplaceSpaces replaces all spaces with another string. +func ReplaceSpaces(s string, char string) string { + return strings.Replace(s, " ", char, -1) +} + +var FilenameKeywordsRegexp = regexp.MustCompile("[\\p{L}]{3,}") + +// FilenameWords returns a slice of words with at least 3 characters from a string ("ile", "france"). +func FilenameWords(s string) (results []string) { + return FilenameKeywordsRegexp.FindAllString(s, -1) +} + // Keywords returns a slice of keywords without stopwords. func Keywords(s string) (results []string) { for _, w := range Words(s) { diff --git a/pkg/txt/words_test.go b/pkg/txt/words_test.go index 221572f79..754ba5354 100644 --- a/pkg/txt/words_test.go +++ b/pkg/txt/words_test.go @@ -7,9 +7,9 @@ import ( ) func TestWords(t *testing.T) { - t.Run("I'm a lazy brown fox!", func(t *testing.T) { - result := Words("I'm a lazy BRoWN fox!") - assert.Equal(t, []string{"lazy", "BRoWN", "fox"}, result) + t.Run("I'm a lazy-brown fox!", func(t *testing.T) { + result := Words("I'm a lazy-BRoWN fox!") + assert.Equal(t, []string{"lazy-BRoWN", "fox"}, result) }) t.Run("no result", func(t *testing.T) { result := Words("x") @@ -17,6 +17,17 @@ func TestWords(t *testing.T) { }) } +func TestFilenameWords(t *testing.T) { + t.Run("I'm a lazy-brown fox!", func(t *testing.T) { + result := FilenameWords("I'm a lazy-BRoWN fox!") + assert.Equal(t, []string{"lazy", "BRoWN", "fox"}, result) + }) + t.Run("no result", func(t *testing.T) { + result := FilenameWords("x") + assert.Equal(t, []string(nil), result) + }) +} + func TestKeywords(t *testing.T) { t.Run("I'm a lazy brown fox!", func(t *testing.T) { result := Keywords("I'm a lazy BRoWN img!") @@ -30,8 +41,8 @@ func TestKeywords(t *testing.T) { func TestUniqueWords(t *testing.T) { t.Run("many", func(t *testing.T) { - result := UniqueWords([]string{"lazy", "brown", "apple", "brown"}) - assert.Equal(t, []string{"apple", "brown", "lazy"}, result) + result := UniqueWords([]string{"lazy", "brown", "apple", "brown", "new-york"}) + assert.Equal(t, []string{"apple", "brown", "lazy", "new-york"}, result) }) t.Run("one", func(t *testing.T) { result := UniqueWords([]string{"lazy"}) @@ -41,8 +52,8 @@ func TestUniqueWords(t *testing.T) { func TestUniqueKeywords(t *testing.T) { t.Run("many", func(t *testing.T) { - result := UniqueKeywords("lazy, brown, apple, brown, ...") - assert.Equal(t, []string{"apple", "brown", "lazy"}, result) + result := UniqueKeywords("lazy, brown, apple, new-york, brown, ...") + assert.Equal(t, []string{"apple", "brown", "lazy", "new-york"}, result) }) t.Run("one", func(t *testing.T) { result := UniqueKeywords("")