Exemplo n.º 1
0
Arquivo: main.go Projeto: philipz/ntm
func showPrediction(pred [][]float64, gen *poem.Generator, oripoem [][]string) {
	ps := make([]string, len(pred))

	// Prepare a slice representation of the poem constraints.
	i := len(pred)/2 + 1
	poema := make([]string, len(pred))
	for _, line := range oripoem {
		for _, c := range line {
			if c != "" {
				poema[i] = c
			}
			i++
		}
		poema[i] = poem.CharLinefeed
		i++
	}

	// Determine the final characters from the predicted probability densities, with the following requirements:
	//   * The choosen character is the same as the input constraints.
	//   * The choosen characters are unique among themselves.
	res := make([][]poem.Char, len(pred))
	for i, p := range pred {
		if i < len(pred)/2+1 {
			ps[i] = ""
		} else if poema[i] != "" {
			ps[i] = poema[i]
		} else {
			sorted := gen.SortOutput(p)
			for _, c := range sorted {
				if c.S == poem.CharUnknown || c.S == poem.CharLinefeed {
					continue
				}
				var dup bool = false
				for _, psc := range ps {
					if psc == c.S {
						dup = true
						break
					}
				}
				if !dup {
					ps[i] = c.S
					break
				}
			}
		}

		res[i] = gen.SortOutput(p)[0:5]
	}

	// Print the probability densities.
	for i, chars := range res {
		log.Printf("%s -> %v", ps[i], chars)
		if i == len(res)/2 {
			log.Printf("-------------")
		}
	}

	// Print the final generated poem.
	s := "\n"
	for _, c := range ps[len(ps)/2+1:] {
		if c == poem.CharLinefeed {
			s += "\n"
		} else {
			s += c
		}
	}
	log.Printf(s)
}