Example #1
0
func main() {
	profile := flag.Bool("p", false, "write profiles to ./")
	train := flag.String("t", "", "train using the data in this file")
	evaluate := flag.String("e", "", "evaluate using the data in this file")
	ngram := flag.Int("n", 2, "ngram length")
	corpus := flag.String("c", "review_polarity", "corpus name")

	flag.Parse()

	trainData := make(chan *model.Datum, 100)
	evalData := make(chan *model.Datum, 100)
	quit := make(chan bool)

	pool := store.NewPool(store.NewRedisStore)
	nb := model.NewNGramModel(pool.Get(*corpus), *ngram)

	quitServer := make(chan bool)

	go ServeDebug()
	go Serve(pool, quitServer)

	if *train != "" {
		fmt.Println("Training on", *train)
		f, _ := os.Open(*train)
		go ReadData(bufio.NewReader(f), trainData, quit)
		go func() {
			for d := range trainData {
				nb.Train(d)
			}
		}()
		<-quit
	}

	if *evaluate != "" {
		fmt.Println("Evaluating on", *evaluate)
		f, _ := os.Open(*evaluate)
		go ReadData(bufio.NewReader(f), evalData, quit)
		go Evaluate(evalData, nb)
	}

	if *profile {
		DumpProfiles()
	}

	<-quit
	<-quitServer
}
func (h TrainHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	query := req.FormValue("q")
	class := req.FormValue("class")
	corpus := req.FormValue("corpus")

	n, err := strconv.Atoi(req.FormValue("n"))
	if err != nil {
		n = 2
	}

	count, err := strconv.Atoi(req.FormValue("count"))
	if err != nil {
		count = 1
	}

	m := model.NewNGramModel(h.pool.Get(corpus), n)

	features := strings.Split(query, ",")
	d := &model.Datum{class, features, int64(count)}
	m.Train(d)
	return
}
// 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
}