Example #1
0
func main() {
	flag.Parse()

	if len(flag.Args()) != 2 {
		fmt.Printf("Usage: %s lexicon modelname\n", os.Args[0])
		os.Exit(1)
	}

	f, err := os.Open(flag.Arg(0))
	if err != nil {
		fmt.Printf("Could not open file: %s\n", flag.Arg(0))
		os.Exit(1)
	}

	r := bufio.NewReader(f)
	dict := word_classification.ReadDictionary(r)

	word_classification.FilterDictionary(dict, 6)

	problem, metadata := word_classification.ExtractFeatures(dict)

	param := golinear.DefaultParameters()

	model, err := golinear.TrainModel(param, problem)
	if err != nil {
		panic(err)
	}

	modelName := flag.Arg(1)

	err = model.Save(fmt.Sprintf("%s.model", modelName))
	if err != nil {
		panic(err)
	}

	bMetadata, err := json.Marshal(metadata)
	if err != nil {
		panic(err)
	}

	metadataFile, err := os.OpenFile(fmt.Sprintf("%s.metadata", modelName),
		os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		log.Fatal(err)
	}
	defer metadataFile.Close()

	metadataFile.Write(bMetadata)

	//testPrefix := prefixes("Microsoft", 3)
	//features := stringFeatureToFeature(testPrefix, featureMapping, norm)

	//class := model.Predict(features)

	//numberTagMapping := reverseMapping(tagMapping)

	//fmt.Printf("Predicted class: %s\n", numberTagMapping[int(class)])
}
Example #2
0
func main() {
	problem := golinear.NewProblem()
	problem.Add(golinear.TrainingInstance{0, golinear.FromDenseVector([]float64{1, 1, 1, 0, 0})})
	problem.Add(golinear.TrainingInstance{1, golinear.FromDenseVector([]float64{1, 0, 1, 1, 1})})

	param := golinear.DefaultParameters()
	model, err := golinear.TrainModel(param, problem)
	if err != nil {
		log.Fatal("Could not train the model: " + err.Error())
	}

	label := model.Predict(golinear.FromDenseVector([]float64{1, 1, 0, 0, 0}))

	fmt.Printf("Predicted label: %f\n", label)
}