Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

bsm/arff

Repository files navigation

ARFF

Build Status GoDoc Go Report Card License

Small utility for reading Attribute-Relation File Format (ARFF) data. For now, only a subset of features is supported.

Supported:

  • Numeric attributes
  • String attributes
  • Nominal attributes
  • Date attributes (ISO-8601 UTC only)
  • Weighted data
  • Unicode

Not-supported:

  • Relational attributes
  • Sparse format

Example: Reader

import(
  "bytes"
  "fmt"

  "github.com/bsm/arff"
)

func main() {
	data, err := arff.Open("./testdata/weather.arff")
	if err != nil {
		panic("failed to open file: " + err.Error())
	}
	defer data.Close()

	for data.Next() {
		fmt.Println(data.Row().Values...)
	}
	if err := data.Err(); err != nil {
		panic("failed to read file: " + err.Error())
	}

}

Example: Writer

import (
  "bytes"
  "fmt"

  "github.com/bsm/arff"
)

func main() {
	buf := new(bytes.Buffer)
	w, err := arff.NewWriter(buf, &arff.Relation{
		Name:	"data relation",
		Attributes: []arff.Attribute{
			{Name: "outlook", DataType: arff.DataTypeNominal, NominalValues: []string{"sunny", "overcast", "rainy"}},
			{Name: "temperature", DataType: arff.DataTypeNumeric},
			{Name: "humidity", DataType: arff.DataTypeNumeric},
			{Name: "windy", DataType: arff.DataTypeNominal, NominalValues: []string{"TRUE", "FALSE"}},
			{Name: "play", DataType: arff.DataTypeNominal, NominalValues: []string{"yes", "no"}},
		},
	})
	if err != nil {
		panic("failed to create writer: " + err.Error())
	}

	if err := w.Append(&arff.DataRow{Values: []interface{}{"sunny", 85, 85, "FALSE", "no"}}); err != nil {
		panic("failed to append row: " + err.Error())
	}
	if err := w.Append(&arff.DataRow{Values: []interface{}{"overcast", 83, 86, "FALSE", "yes"}}); err != nil {
		panic("failed to append row: " + err.Error())
	}
	if err := w.Append(&arff.DataRow{Values: []interface{}{"rainy", 65, 70, "TRUE", "no"}}); err != nil {
		panic("failed to append row: " + err.Error())
	}
	if err := w.Close(); err != nil {
		panic("failed to close writer: " + err.Error())
	}

	fmt.Println(buf.String())

}