From f74483db531e1dbefb0b3768b6927635ae5d94fd Mon Sep 17 00:00:00 2001 From: Philip Gatt Date: Tue, 28 Jun 2016 15:40:00 -0700 Subject: [PATCH] Serialize ARFF to Writer in Addition to a File --- base/arff.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/base/arff.go b/base/arff.go index 1098c6b..bb8c455 100644 --- a/base/arff.go +++ b/base/arff.go @@ -37,8 +37,14 @@ func SerializeInstancesToDenseARFFWithAttributes(inst FixedDataGrid, rawAttrs [] } defer f.Close() + return SerializeInstancesToWriterDenseARFFWithAttributes(f, inst, rawAttrs, relation) + + +} + +func SerializeInstancesToWriterDenseARFFWithAttributes(w io.Writer, inst FixedDataGrid, rawAttrs []Attribute, relation string) error { // Write @relation header - f.WriteString(fmt.Sprintf("@relation %s\n\n", relation)) + fmt.Fprintf(w, "@relation %s\n\n", relation) // Get all Attribute specifications attrs := ResolveAttributes(inst, rawAttrs) @@ -51,17 +57,17 @@ func SerializeInstancesToDenseARFFWithAttributes(inst FixedDataGrid, rawAttrs [] vals := a.GetValues() t = fmt.Sprintf("{%s}", strings.Join(vals, ", ")) } - f.WriteString(fmt.Sprintf("@attribute %s %s\n", attr.GetName(), t)) + fmt.Fprintf(w, "@attribute %s %s\n", attr.GetName(), t) } - f.WriteString("\n@data\n") + fmt.Fprint(w, "\n@data\n") buf := make([]string, len(attrs)) inst.MapOverRows(attrs, func(val [][]byte, row int) (bool, error) { for i, v := range val { buf[i] = attrs[i].attr.GetStringFromSysVal(v) } - f.WriteString(strings.Join(buf, ",")) - f.WriteString("\n") + fmt.Fprint(w, strings.Join(buf, ",")) + fmt.Fprint(w, "\n") return true, nil })