weg: cmd/score

This commit is contained in:
Peter Kleiweg
2026-05-24 16:42:12 +02:00
parent fcad105a75
commit bf0407b933
2 changed files with 0 additions and 118 deletions

View File

@@ -21,7 +21,6 @@ all:
make bin/dates2json
make bin/flush
make bin/items2count
make bin/score
make bin/top20
make bin/week2files
@@ -37,9 +36,6 @@ bin/flush: cmd/flush/*.go
bin/items2count: cmd/items2count/*.go
go build -o $@ $^
bin/score: cmd/score/*.go
go build -o $@ $^
bin/top20: cmd/top20/*.go
go build -o $@ $^

View File

@@ -1,114 +0,0 @@
package main
import (
e "codeberg.org/pebbe/errors"
"bufio"
"fmt"
"os"
"regexp"
"sort"
"strconv"
"strings"
)
type Item struct {
text string
lctext string
score int
isnew bool
}
var (
x = e.ExitErr
reYearWeek = regexp.MustCompile(`(.*)([12][0-9][0-9][0-9]-[0-5][0-9])(.*)`)
count = make(map[string]int)
items = make([]Item, 0)
)
func main() {
filename := os.Args[1]
prevname := getPrev(filename)
fp, err := os.Open(prevname)
x(err)
scanner := bufio.NewScanner(fp)
for scanner.Scan() {
aa := strings.SplitN(scanner.Text(), "\t", 2)
n, err := strconv.Atoi(strings.TrimSpace(aa[0]))
x(err)
count[aa[1]] = n
}
x(scanner.Err())
x(fp.Close())
fp, err = os.Open(filename)
x(err)
scanner = bufio.NewScanner(fp)
for scanner.Scan() {
aa := strings.SplitN(scanner.Text(), "\t", 2)
n, err := strconv.Atoi(strings.TrimSpace(aa[0]))
x(err)
n1, ok := count[aa[1]]
items = append(items, Item{
text: aa[1],
lctext: strings.ToLower(aa[1]),
score: n - n1,
isnew: !ok,
})
}
x(scanner.Err())
x(fp.Close())
sort.Slice(items, func(i, j int) bool {
/*
if items[i].isnew && !items[j].isnew {
return true
}
if !items[i].isnew && items[j].isnew {
return false
}
*/
if items[i].score != items[j].score {
return items[i].score > items[j].score
}
return items[i].lctext < items[j].lctext
})
for _, item := range items {
/*
if item.score < 2 {
break
}
*/
p := "."
if item.isnew {
p = "N"
}
fmt.Printf("%s\t%4d\t%s\n", p, item.score, item.text)
}
}
func getPrev(filename string) string {
mm := reYearWeek.FindStringSubmatch(filename)
year, err := strconv.Atoi(mm[2][:4])
x(err)
week, err := strconv.Atoi(mm[2][5:])
x(err)
week--
if week == 0 {
week = 53
year--
}
newname := fmt.Sprintf("%s%d-%02d%s", mm[1], year, week, mm[3])
if week == 53 {
_, err := os.Stat(newname)
if err == nil {
return newname
}
newname = fmt.Sprintf("%s%d-%02d%s", mm[1], year, week-1, mm[3])
}
return newname
}