Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON serialization #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

JSON serialization #30

wants to merge 4 commits into from

Conversation

nnqq
Copy link

@nnqq nnqq commented Jul 24, 2020

Hi. My edits:

  • JSON serialization as an option (gob by default, so it have full backward compatibility). Also JSON have around 25% less file size
  • Minor codestyle fixes - error check in defer, spread operator in test
  • Go mod file
2020/07/24 20:32:04 gob [One Two Three Four Five Six Seven Eight Nine Ten]
2020/07/24 20:32:04 gob size 816
2020/07/24 20:32:04 json [One Two Three Four Five Six Seven Eight Nine Ten]
2020/07/24 20:32:04 json size 611
package main

import (
	"github.com/jbrukh/bayesian"
	"log"
	"os"
	"path"
)

func write(ser bayesian.Serializer) {
	const (
		One   bayesian.Class = "One"
		Two   bayesian.Class = "Two"
		Three bayesian.Class = "Three"
		Four  bayesian.Class = "Four"
		Five  bayesian.Class = "Five"
		Six   bayesian.Class = "Six"
		Seven bayesian.Class = "Seven"
		Eight bayesian.Class = "Eight"
		Nine  bayesian.Class = "Nine"
		Ten   bayesian.Class = "Ten"
	)

	classifier := bayesian.NewClassifier(One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten)
	oneStuff := []string{"lorem", "ipsum", "dolor"}
	twoStuff := []string{"sit", "amet", "consectetur"}
	threeStuff := []string{"adipiscing", "elit", "sed"}
	fourStuff := []string{"do", "eiusmod", "tempor"}
	fiveStuff := []string{"incididunt", "ut", "labore"}
	sixStuff := []string{"et", "dolore", "magna"}
	sevenStuff := []string{"aliqua", "ut", "enim"}
	eightStuff := []string{"ad", "minim", "veniam"}
	nineStuff := []string{"quis", "nostrud", "exercitation"}
	tenStuff := []string{"ullamco", "laboris", "nisi"}

	classifier.Learn(oneStuff, One)
	classifier.Learn(twoStuff, Two)
	classifier.Learn(threeStuff, Three)
	classifier.Learn(fourStuff, Four)
	classifier.Learn(fiveStuff, Five)
	classifier.Learn(sixStuff, Six)
	classifier.Learn(sevenStuff, Seven)
	classifier.Learn(eightStuff, Eight)
	classifier.Learn(nineStuff, Nine)
	classifier.Learn(tenStuff, Ten)

	wd, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	err = classifier.WriteToFile(path.Join(wd, "out_"+string(ser)), ser)
	if err != nil {
		panic(err)
	}
}

func read(ser bayesian.Serializer) {
	wd, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	file := path.Join(wd, "out_"+string(ser))

	classifier, err := bayesian.NewClassifierFromFile(file, ser)
	if err != nil {
		panic(err)
	}

	f, err := os.Open(file)
	if err != nil {
		panic(err)
	}
	info, err := f.Stat()
	if err != nil {
		panic(err)
	}

	log.Println(ser, classifier.Classes)
	log.Println(ser, "size", info.Size())
}

func main() {
	write(bayesian.Gob)
	read(bayesian.Gob)

	write(bayesian.JSON)
	read(bayesian.JSON)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant