コード例 #1
0
ファイル: main.go プロジェクト: danieldk/golinear-examples
func main() {
	if len(os.Args) != 3 {
		fmt.Printf("%s model word\n", os.Args[0])
		os.Exit(1)
	}

	modelBasename := os.Args[1]
	model, err := golinear.LoadModel(fmt.Sprintf("%s.model", modelBasename))
	if err != nil {
		log.Fatal(err)
	}

	metadata, err := loadMetadata(modelBasename)
	if err != nil {
		log.Fatal(err)
	}

	word := os.Args[2]
	sfs := word_classification.ApplyTemplates(word_classification.DefaultTemplates, word)
	//	fmt.Printf("%#v", sfs)
	fs := word_classification.StringFeatureToFeature(sfs, metadata.FeatureMapping, metadata.Normalizer)

	class := model.Predict(fs)

	indexToClass := reverseMapping(metadata.ClassMapping)

	fmt.Printf("Predicted class: %s\n", indexToClass[int(class)])
}
コード例 #2
0
ファイル: main.go プロジェクト: danieldk/golinear-examples
func main() {
	if len(os.Args) != 3 {
		os.Exit(1)
	}

	modelBasename := os.Args[1]
	model, err := golinear.LoadModel(fmt.Sprintf("%s.model", modelBasename))
	if err != nil {
		log.Fatal(err)
	}

	metadata, err := loadMetadata(modelBasename)
	if err != nil {
		log.Fatal(err)
	}

	testDict := readDictionary(os.Args[2])
	word_classification.FilterDictionary(testDict, 6)

	indexToClass := reverseMapping(metadata.ClassMapping)

	total, correct, baseline := 0, 0, 0

	errorsPerClass := make(map[string]int)
	totalPerClass := make(map[string]int)

	for word, tagFreq := range testDict {
		sfs := word_classification.ApplyTemplates(word_classification.DefaultTemplates, word)
		fs := word_classification.StringFeatureToFeature(sfs, metadata.FeatureMapping, metadata.Normalizer)

		for tag, freq := range tagFreq {
			var i uint64
			for i = 0; i < freq; i++ {
				class := model.Predict(fs)

				predictedTag := indexToClass[int(class)]

				// Update counts
				if predictedTag == tag {
					correct++
				} else {
					errorsPerClass[predictedTag]++
				}

				if tag == "NN" {
					baseline++
				}

				total++
				totalPerClass[predictedTag]++
			}
		}
	}

	errors := total - correct
	for class, classErrors := range errorsPerClass {
		fmt.Printf("%s:\t\t%.2f%%\t%.2f%%\n", class,
			float64(classErrors)/float64(errors)*100,
			float64(classErrors)/float64(totalPerClass[class])*100)
	}

	//fmt.Printf("Correct classifications: %d, total: %d\n", correct, total)
	fmt.Printf("\nAccuracy: %.2f%%\n", float64(correct)/float64(total)*100)
	fmt.Printf("Baseline: %.2f%%\n", float64(baseline)/float64(total)*100)
}