Esempio n. 1
0
func Evaluate(evalData chan *model.Datum, nb model.Interface) {
	var correct, wrong uint
	evalStartTime := time.Now()
	for d := range evalData {
		estimator, _ := nb.Classify(d.Features)
		class, _ := distribution.ArgMax(estimator)
		if class == d.Class {
			correct += 1
		} else {
			fmt.Println("For:", d.Features, "Was:", d.Class, "Got:", class)
			wrong += 1
		}
	}

	elapsed := time.Since(evalStartTime).Seconds()
	fmt.Println("Took", elapsed, "for", correct+wrong, "queries.", elapsed/float64(correct+wrong), "sec/query")
	accuracy := float64(correct) / (float64(correct) + float64(wrong))
	fmt.Println(accuracy*100., "Got", correct, "correct and", wrong, "wrong.")
}
// ServeHTTP classifies
func (h ClassifyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	query := req.FormValue("q")
	corpus := req.FormValue("corpus")
	n, err := strconv.Atoi(req.FormValue("n"))
	if err != nil {
		n = 2
	}
	fmt.Println(req)
	m := model.NewNGramModel(h.pool.Get(corpus), n)

	features := strings.Split(query, " ")
	estimator, explain := m.Classify(features)
	prediction, _ := distribution.ArgMax(estimator)
	output := map[string]interface{}{
		"prediction": prediction,
		"estimator":  distribution.JSON(estimator),
		"explain":    explain,
	}
	jsonBytes, _ := json.MarshalIndent(output, "", "\t")
	w.Write(jsonBytes)
	return
}