Files
nlnieuws/cmd/trends/trends.go
Peter Kleiweg a9f9e17acf gone gone
2026-06-06 21:05:00 +02:00

109 lines
1.7 KiB
Go

package main
import (
e "codeberg.org/pebbe/errors"
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
)
type Item struct {
word string
diff float64
gone bool
}
var (
x = e.ExitErr
)
func main() {
refs := make(map[string]int)
refmax := 0
fp, err := os.Open(os.Args[1])
x(err)
scanner := bufio.NewScanner(fp)
for scanner.Scan() {
aa := strings.Split(scanner.Text(), "\t")
n, err := strconv.Atoi(aa[0])
x(err)
refs[aa[1]] = n
if n > refmax {
refmax = n
}
}
x(scanner.Err())
fp.Close()
refmax++
lines := make([]string, 0)
fp, err = os.Open(os.Args[2])
x(err)
scanner = bufio.NewScanner(fp)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
x(scanner.Err())
fp.Close()
curmax, err := strconv.Atoi(strings.Split(lines[len(lines)-1], "\t")[0])
x(err)
curmax++
items := make([]Item, 0)
seen := make(map[string]bool)
for _, line := range lines {
aa := strings.Split(line, "\t")
seen[aa[1]] = true
n, err := strconv.Atoi(aa[0])
x(err)
m, ok := refs[aa[1]]
if !ok {
//continue
m = refmax
}
diff := float64(m)/float64(refmax) - float64(n)/float64(curmax)
if diff > 0.05 || diff < -0.05 {
items = append(items, Item{
word: aa[1],
diff: diff,
})
}
}
for key, value := range refs {
if !seen[key] {
diff := float64(value)/float64(refmax) - 1.0
if diff > 0.05 || diff < -0.05 {
items = append(items, Item{
word: key,
diff: diff,
gone: true,
})
}
}
}
sort.Slice(items, func(a, b int) bool {
if items[a].diff == items[b].diff {
return items[a].word < items[b].word
}
return items[a].diff > items[b].diff
})
for _, item := range items {
var s string
if item.gone {
s = "X"
}
fmt.Printf("%f\t%s\t%s\n", item.diff, s, item.word)
}
}