48 lines
843 B
Go
48 lines
843 B
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func (s *SemanticFeatures) UnmarshalJSON(data []byte) error {
|
|
type alias SemanticFeatures
|
|
var raw struct {
|
|
alias
|
|
Confidence interface{} `json:"confidence"`
|
|
}
|
|
if err := json.Unmarshal(data, &raw); err != nil {
|
|
return err
|
|
}
|
|
*s = SemanticFeatures(raw.alias)
|
|
switch v := raw.Confidence.(type) {
|
|
case float64:
|
|
s.Confidence = v
|
|
case string:
|
|
s.Confidence = confidenceLabelToFloat(v)
|
|
case nil:
|
|
s.Confidence = 0
|
|
default:
|
|
s.Confidence = 0
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func confidenceLabelToFloat(label string) float64 {
|
|
label = strings.ToLower(strings.TrimSpace(label))
|
|
switch label {
|
|
case "high":
|
|
return 0.88
|
|
case "medium":
|
|
return 0.70
|
|
case "low":
|
|
return 0.50
|
|
default:
|
|
if v, err := strconv.ParseFloat(label, 64); err == nil {
|
|
return v
|
|
}
|
|
return 0
|
|
}
|
|
}
|