Ejemplo n.º 1
0
func load(filename string) {
	f, err := os.Open(filename)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	g, err := gzip.NewReader(f)
	if err != nil {
		log.Fatal(err)
	}
	defer g.Close()

	Chain = markov.NewSTChain()
	err = gob.NewDecoder(g).Decode(&Chain)
	if err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 2
0
func main() {
	var err error
	flag.Parse()

	startup()
	defer cleanup()

	r := rand.New(rand.NewSource(*seed))

	in := os.Stdin
	if *input != "" {
		in, err = os.Open(*input)
		if err != nil {
			fmt.Fprintf(os.Stderr, "error while opening -i file: %s\n", err)
			return
		}
		defer in.Close()
	}

	out := os.Stdout
	if *output != "" {
		out, err = os.Create(*output)
		if err != nil {
			fmt.Fprintf(os.Stderr, "error while opening -o file: %s\n", err)
			return
		}
		defer out.Close()
	}

	b := bufio.NewReaderSize(in, 0x10000)

	chain := markov.NewSTChain()

	if *p {
		g, err := gzip.NewReader(b)
		if err != nil {
			fmt.Fprintf(os.Stderr, "error while reading precompiled file: %s\n", err)
			return
		}

		err = gob.NewDecoder(g).Decode(&chain)
		g.Close()

		if err != nil {
			fmt.Fprintf(os.Stderr, "error while reading precompiled file: %s\n", err)
			return
		}
	} else {
		for {
			line, err := b.ReadString('\n')
			if err == io.EOF {
				break
			} else if err != nil {
				fmt.Fprintf(os.Stderr, "error while reading input: %s\n", err)
				return
			}

			chain.Build(strings.Fields(line))
		}
	}

	if *c {
		g, _ := gzip.NewWriterLevel(out, gzip.BestCompression)
		err = gob.NewEncoder(g).Encode(chain)
		g.Close()

		if err != nil {
			fmt.Fprintf(os.Stderr, "error while writing compiled file: %s\n", err)
			return
		}
	} else {
		for i := 0; i < *n; i++ {
			fmt.Fprintln(out, chain.Generate(100, r))
		}
	}
}