summaryrefslogtreecommitdiff
path: root/line-conf-avg/line-conf-avg.go
blob: 18421d3b737fd3ebf38d1291ebb860c0198f4e4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main

import (
	"bufio"
	"flag"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: line-conf-avg prob1 [prob2] [...]\n")
		flag.PrintDefaults()
	}
	flag.Parse()
	if flag.NArg() < 1 {
		flag.Usage()
		os.Exit(1)
	}

	for _, f := range flag.Args() {
		file, err := os.Open(f)
		if err != nil {
			log.Fatal(err)
		}
		defer file.Close()

		reader := bufio.NewReader(file)

		totalconf := float64(0)
		num := 0

		err = nil
		for err == nil {
			var line string
                        line, err = reader.ReadString('\n')
			fields := strings.Fields(line)

			if len(fields) == 2 {
				conf, converr := strconv.ParseFloat(fields[1], 64)
				if converr != nil {
					fmt.Fprintf(os.Stderr, "Error: can't convert '%s' to float (full line: %s)\n", fields[1], line)
					continue
				}
				totalconf += conf
				num += 1
			}
		}
		avg := totalconf / float64(num)

		fmt.Printf("%s: %.2f%%\n", f, avg)
	}
}