gone, trends
This commit is contained in:
88
cmd/trends/trends.go
Normal file
88
cmd/trends/trends.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
e "codeberg.org/pebbe/errors"
|
||||
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
word string
|
||||
diff float64
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
for _, line := range lines {
|
||||
aa := strings.Split(line, "\t")
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
fmt.Printf("%f\t%s\n", item.diff, item.word)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user