67 lines
1.3 KiB
Go
Raw Normal View History

2019-06-03 09:32:43 +00:00
package search
import (
"sort"
"strings"
"github.com/animenotifier/notify.moe/arn"
"github.com/animenotifier/notify.moe/arn/stringutils"
)
// AMVs searches all anime music videos.
func AMVs(originalTerm string, maxLength int) []*arn.AMV {
term := strings.ToLower(stringutils.RemoveSpecialCharacters(originalTerm))
results := make([]*Result, 0, maxLength)
for amv := range arn.StreamAMVs() {
if amv.ID == originalTerm {
return []*arn.AMV{amv}
}
if amv.IsDraft {
continue
}
text := strings.ToLower(amv.Title.Canonical)
similarity := stringutils.AdvancedStringSimilarity(term, text)
2019-08-31 07:52:42 +00:00
if similarity >= MinStringSimilarity {
2019-06-03 09:32:43 +00:00
results = append(results, &Result{
obj: amv,
similarity: similarity,
})
continue
}
text = strings.ToLower(amv.Title.Native)
similarity = stringutils.AdvancedStringSimilarity(term, text)
2019-08-31 07:52:42 +00:00
if similarity >= MinStringSimilarity {
2019-06-03 09:32:43 +00:00
results = append(results, &Result{
obj: amv,
similarity: similarity,
})
continue
}
}
// Sort
sort.Slice(results, func(i, j int) bool {
return results[i].similarity > results[j].similarity
})
// Limit
if len(results) >= maxLength {
results = results[:maxLength]
}
// Final list
final := make([]*arn.AMV, len(results))
for i, result := range results {
final[i] = result.obj.(*arn.AMV)
}
return final
}