package main

import (
	"fmt"
	"log"
	"os"
	"training/terms"
)

func main() {
	filename := os.Args[1]
	in, err := os.Open(filename)
	if err != nil {
		log.Println("Unable to open file. Err:", err)
		os.Exit(1)
	}
	defer in.Close()

	log.Println("processing terms")
	tf, err := terms.Frequency(in)
	if err != nil {
		log.Println("Error indexing: err:", err)
	}
	if err := in.Close(); err != nil {
		log.Println("Error closing file. err:", err)
	}

	// write words to file
	out, err := os.Create("terms.txt")
	if err != nil {
		log.Println("Error creating file. err:", err)
	}
	defer out.Close()
	fmt.Fprintln(out, "Index:")

	for k, v := range tf {
		fmt.Fprintln(out, k, ": ", v)
	}
	if err := out.Close(); err != nil {
		log.Println("Error closing file. err:", err)
	}
}
