115 lines
2.0 KiB
Go
115 lines
2.0 KiB
Go
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
|
|
}
|