Skip to content

Instantly share code, notes, and snippets.

@tmountain
Created November 27, 2017 15:06
Show Gist options
  • Save tmountain/8d6a5f324d3f7621be7d18489622c659 to your computer and use it in GitHub Desktop.
Save tmountain/8d6a5f324d3f7621be7d18489622c659 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type Score struct {
DomainScore int
PageScore int
TokenScore int
}
func addScore(score *Score, id string, val int) (*Score, bool) {
switch id {
case "tokens":
score.TokenScore += val
case "pages":
score.PageScore += val
case "score":
score.DomainScore += val
default:
return score, false
}
return score, true
}
func main() {
// Wrapping the unbuffered `os.Stdin` with a buffered
// scanner gives us a convenient `Scan` method that
// advances the scanner to the next token; which is
// the next line in the default scanner.
scanner := bufio.NewScanner(os.Stdin)
results := make(map[string]*Score)
for scanner.Scan() {
// `Text` returns the current token, here the next line,
// from the input.
var err error
var scoreInt int
text := strings.Split(strings.TrimSpace(scanner.Text()), "\t")
token, scoreStr := text[0], text[1]
token = strings.Replace(token, `"`, "", -1)
parts := strings.Split(token, ":")
domain := parts[0]
id := parts[1]
if scoreInt, err = strconv.Atoi(scoreStr); err != nil {
continue
}
if score, ok := results[domain]; ok {
addScore(score, id, scoreInt)
} else {
score = &Score{}
addScore(score, id, scoreInt)
results[domain] = score
}
}
fmt.Printf("domain\tscore\tpages\ttokens\n")
for k, v := range results {
fmt.Printf("%s\t%v\t%v\t%v\n", k, v.DomainScore, v.PageScore, v.TokenScore)
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment