/
date.go
31 lines (23 loc) · 926 Bytes
/
date.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package featuremill
// feature index determinestic by murmur32(uniqueHashPrefixStr+<user field>+<interval>)
import (
"fmt"
"github.com/araddon/dateparse"
"github.com/spaolacci/murmur3"
)
// ExtractDate returns a slice of 2 scaled seasonality vectors: day of week, and month of year
// each with a deterministic feature id
func ExtractDate(field, date string) ([]string, error) {
var out []string
dt, err := dateparse.ParseAny(date)
if err != nil {
return out, err
}
dayOfWeek := int(dt.Weekday())
dayOfWeekFeatureID := int32(murmur3.Sum32([]byte(uniqueHashPrefixStr + field + "dayOfWeek")))
out = append(out, fmt.Sprintf("%d:%f", dayOfWeekFeatureID, float32(dayOfWeek)/6))
monthOfYear := dt.Month()
monthOfYearFeatureID := int32(murmur3.Sum32([]byte(uniqueHashPrefixStr + field + "monthOfYear")))
out = append(out, fmt.Sprintf("%d:%f", monthOfYearFeatureID, float32(monthOfYear)/12))
return out, nil
}