Created
July 10, 2019 20:28
-
-
Save thefish/caa5b08b215bf543e4df5ba50c4029c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"github.com/jessevdk/go-flags" | |
"github.com/rs/zerolog/log" | |
"os" | |
"strconv" | |
"strings" | |
) | |
type options struct { | |
Help bool `short:"h" long:"help" description:"Show help message"` | |
Input string `short:"i" long:"input" default:"freq.txt" description:"File with trigrams and count seperated by space, new record on newline"` | |
Output string `short:"o" long:"output" default:"result.txt" description:"Output file with trigrams"` | |
} | |
type Cache struct { | |
Data map[string]int | |
} | |
func (c *Cache) Init() { | |
c.Data = make(map[string]int) | |
} | |
func (c *Cache) Update (trigram, count string) { | |
cnt, err := strconv.Atoi(count) | |
if err != nil { | |
log.Warn().Msgf("Could not parse integer from %v", count) | |
return | |
} | |
if _, ok := c.Data[trigram]; ok { | |
c.Data[trigram] += cnt | |
} else { | |
c.Data[trigram] = cnt | |
} | |
} | |
func main() { | |
var opts options | |
p := flags.NewParser(&opts, flags.Default&^flags.HelpFlag) | |
_, err := p.Parse() | |
if err != nil { | |
log.Fatal().Msgf("fail to parse args: %v", err) | |
} | |
if opts.Help { | |
p.WriteHelp(os.Stdout) | |
os.Exit(0) | |
} | |
c := Cache{} | |
c.Init() | |
input, err := os.Open(opts.Input) | |
if err != nil { | |
log.Fatal().Msgf("could not read file %s", opts.Input) | |
} | |
defer input.Close() | |
out, err := os.Create(opts.Output) | |
if err != nil { | |
log.Fatal().Msgf("could not create output file %v", opts.Output) | |
} | |
defer out.Close() | |
scanner := bufio.NewScanner(input) | |
for scanner.Scan() { | |
line := strings.TrimSpace(scanner.Text()) | |
parsed := strings.Split(line, " ") | |
log.Info().Msgf("parsed: %v", parsed) | |
c.Update(parsed[1], parsed[0]) | |
} | |
w := bufio.NewWriter(out) | |
for trigram, count := range c.Data { | |
w.WriteString(fmt.Sprintf("%s %d\n", trigram, count)) | |
} | |
w.Flush() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment