aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoop <Joop Kiefte>2022-04-02 05:23:20 +0100
committerjoop <Joop Kiefte>2022-04-02 05:23:20 +0100
commit83089069fbf2916256fbd299afe040be3cb5b246 (patch)
tree6c3c9fcf63f19d3c8eaf61f9b5cea96c697d6ca7
parentaf3eed491c9291862028c9a88f1e08e535b47f0d (diff)
Add length based matches sorting
-rw-r--r--main.go61
1 files changed, 59 insertions, 2 deletions
diff --git a/main.go b/main.go
index f43f004..5039c0a 100644
--- a/main.go
+++ b/main.go
@@ -19,6 +19,7 @@ import (
"math/rand"
"net/http"
"os"
+ "sort"
"strconv"
"strings"
@@ -236,6 +237,58 @@ func ReadPrayers(lang []Language, codep bool) []Prayer {
return prayers
}
+// ByPrayerLength tries to return prayers closest in length to
+// the reference prayer r (in characters)
+func ByPrayerLength(p []Prayer, r Prayer) []Prayer {
+ out := NewPrayerLengthSort(p, r)
+ sort.Sort(out)
+ return out.Prayers()
+}
+
+type PrayerLengthSort []struct {
+ P Prayer
+ Diff int
+}
+
+func (p PrayerLengthSort) Len() int {
+ return len(p)
+}
+
+func (p PrayerLengthSort) Less(i, j int) bool {
+ return p[i].Diff < p[j].Diff
+}
+
+func (p PrayerLengthSort) Swap(i, j int) {
+ p[i], p[j] = p[j], p[i]
+}
+
+func (p PrayerLengthSort) Sort() {
+ sort.Sort(p)
+}
+
+func (p PrayerLengthSort) Prayers() []Prayer {
+ var out []Prayer
+ for _, prayer := range p {
+ out = append(out, prayer.P)
+ }
+ return out
+}
+
+func NewPrayerLengthSort(p []Prayer, r Prayer) PrayerLengthSort {
+ var out PrayerLengthSort
+ for _, prayer := range p {
+ diff := len(prayer.Text) - len(r.Text)
+ if diff < 0 {
+ diff = -diff
+ }
+ out = append(out, struct {
+ P Prayer
+ Diff int
+ }{prayer, diff})
+ }
+ return out
+}
+
func main() {
err := ReadLangCSV()
if err != nil {
@@ -280,6 +333,7 @@ func main() {
fmt.Println(prayer.Text)
fmt.Println("ID:", prayer.Id)
fmt.Println("Author:", prayer.Author())
+ matchcode:
for code == "" {
// Ask for a keyword
fmt.Print("Input a keyword for this prayer, skip to pick another one or code to enter the code manually: ")
@@ -311,18 +365,21 @@ func main() {
fmt.Println("Which of the following matches?")
for i, match := range Matches {
fmt.Println(i+1, ":", match.Text)
- fmt.Print("Does this match? (y/n) ")
+ fmt.Print("Does this match? (y/n/skip) ")
answer := prompt.MustRead[string]()
if answer == "y" {
fmt.Println(CodeList[match.Id])
code = CodeList[match.Id]
break
}
+ if answer == "skip" {
+ break matchcode
+ }
}
}
// Add the code to CodeList
CodeList[prayer.Id] = code
- fmt.Println("Added code ", " to prayer ", prayer.Id)
+ fmt.Println("Added code ", code, " to prayer ", prayer.Id)
// Ask if the user wants to identify another prayer
// or if they want to quit
// To identify another prayer, continue