2020-01-07 17:36:49 +01:00
|
|
|
package txt
|
2019-12-13 03:07:26 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
2020-03-25 12:39:07 +01:00
|
|
|
"sort"
|
2019-12-13 03:07:26 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-04-16 15:57:07 +02:00
|
|
|
var KeywordsRegexp = regexp.MustCompile("[\\p{L}\\-]{3,}")
|
2019-12-13 03:07:26 +01:00
|
|
|
|
2020-04-16 15:57:07 +02:00
|
|
|
// Words returns a slice of words with at least 3 characters from a string, dashes count as character ("ile-de-france").
|
2020-02-02 02:00:47 +01:00
|
|
|
func Words(s string) (results []string) {
|
|
|
|
return KeywordsRegexp.FindAllString(s, -1)
|
|
|
|
}
|
2019-12-13 03:07:26 +01:00
|
|
|
|
2020-04-16 15:57:07 +02:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-04-16 23:30:30 +02:00
|
|
|
// FilenameKeywords returns a slice of keywords without stopwords.
|
|
|
|
func FilenameKeywords(s string) (results []string) {
|
|
|
|
for _, w := range FilenameWords(s) {
|
|
|
|
w = strings.ToLower(w)
|
|
|
|
|
|
|
|
if _, ok := Stopwords[w]; ok == false {
|
|
|
|
results = append(results, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keywords returns a slice of keywords without stopwords but including dashes.
|
2020-02-02 02:00:47 +01:00
|
|
|
func Keywords(s string) (results []string) {
|
|
|
|
for _, w := range Words(s) {
|
2019-12-13 03:07:26 +01:00
|
|
|
w = strings.ToLower(w)
|
|
|
|
|
|
|
|
if _, ok := Stopwords[w]; ok == false {
|
|
|
|
results = append(results, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
2020-03-25 12:39:07 +01:00
|
|
|
|
|
|
|
// UniqueWords sorts and filters a string slice for unique words.
|
|
|
|
func UniqueWords(words []string) (results []string) {
|
|
|
|
last := ""
|
|
|
|
|
2020-04-16 21:49:31 +02:00
|
|
|
SortCaseInsensitive(words)
|
2020-03-25 12:39:07 +01:00
|
|
|
|
|
|
|
for _, w := range words {
|
2020-04-16 21:49:31 +02:00
|
|
|
w = strings.ToLower(w)
|
|
|
|
|
2020-03-25 12:39:07 +01:00
|
|
|
if len(w) < 3 || w == last {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
last = w
|
|
|
|
|
|
|
|
results = append(results, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2020-05-28 21:20:42 +02:00
|
|
|
// RemoveFromWords removes words from a string slice and returns the sorted result.
|
|
|
|
func RemoveFromWords(words []string, remove string) (results []string) {
|
|
|
|
remove = strings.ToLower(remove)
|
|
|
|
last := ""
|
|
|
|
|
|
|
|
SortCaseInsensitive(words)
|
|
|
|
|
|
|
|
for _, w := range words {
|
|
|
|
w = strings.ToLower(w)
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if len(w) < 3 || w == last || strings.Contains(remove, w) {
|
2020-05-28 21:20:42 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
last = w
|
|
|
|
|
|
|
|
results = append(results, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2020-03-25 12:39:07 +01:00
|
|
|
// UniqueKeywords returns a slice of unique and sorted keywords without stopwords.
|
|
|
|
func UniqueKeywords(s string) (results []string) {
|
|
|
|
last := ""
|
|
|
|
|
|
|
|
words := Keywords(s)
|
|
|
|
|
2020-04-16 21:49:31 +02:00
|
|
|
SortCaseInsensitive(words)
|
2020-03-25 12:39:07 +01:00
|
|
|
|
|
|
|
for _, w := range words {
|
|
|
|
w = strings.ToLower(w)
|
|
|
|
|
|
|
|
if len(w) < 3 || w == last {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
last = w
|
|
|
|
|
|
|
|
results = append(results, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
2020-04-16 21:49:31 +02:00
|
|
|
|
|
|
|
// Sorts string slice case insensitive.
|
|
|
|
func SortCaseInsensitive(words []string) {
|
|
|
|
sort.Slice(words, func(i, j int) bool { return strings.ToLower(words[i]) < strings.ToLower(words[j]) })
|
|
|
|
}
|