229 lines
6.1 KiB
Go
229 lines
6.1 KiB
Go
package schema
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"maps"
|
|
"math"
|
|
"slices"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/jsonnumber"
|
|
)
|
|
|
|
type SourceSegment struct {
|
|
ID *sourceID `json:"id,omitempty"`
|
|
Speaker string `json:"speaker"`
|
|
Start float64 `json:"start"`
|
|
End float64 `json:"end"`
|
|
Text string `json:"text"`
|
|
Categories []string `json:"categories,omitempty"`
|
|
}
|
|
|
|
// UnmarshalJSON decodes only the final value of each exact key, then applies
|
|
// case variants in the same key order as the original object round trip.
|
|
func (s *SourceSegment) UnmarshalJSON(raw []byte) error {
|
|
var members map[string]json.RawMessage
|
|
if err := json.Unmarshal(raw, &members); err != nil {
|
|
return err
|
|
}
|
|
for _, key := range slices.Sorted(maps.Keys(members)) {
|
|
var field any
|
|
switch {
|
|
case strings.EqualFold(key, "id"):
|
|
field = &s.ID
|
|
case strings.EqualFold(key, "speaker"):
|
|
field = &s.Speaker
|
|
case strings.EqualFold(key, "start"):
|
|
field = &s.Start
|
|
case strings.EqualFold(key, "end"):
|
|
field = &s.End
|
|
case strings.EqualFold(key, "text"):
|
|
field = &s.Text
|
|
case strings.EqualFold(key, "categories"):
|
|
field = &s.Categories
|
|
default:
|
|
continue
|
|
}
|
|
if err := json.Unmarshal(members[key], field); err != nil {
|
|
return fmt.Errorf("field %q: %w", key, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Segment struct {
|
|
ID int `json:"id"`
|
|
Speaker string `json:"speaker"`
|
|
Start float64 `json:"start"`
|
|
End float64 `json:"end"`
|
|
Text string `json:"text"`
|
|
Categories []string `json:"categories,omitempty"`
|
|
}
|
|
|
|
type Transcript struct {
|
|
Segments []Segment `json:"segments"`
|
|
}
|
|
|
|
type SourceTranscript struct {
|
|
Segments []SourceSegment `json:"segments"`
|
|
}
|
|
|
|
func ParseSourceTranscriptJSON(raw []byte) (*SourceTranscript, error) {
|
|
if !json.Valid(raw) {
|
|
return nil, &ParseError{Message: "transcript is not valid JSON"}
|
|
}
|
|
|
|
segmentsRaw, err := extractSegments(bytes.TrimSpace(raw))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var segments []SourceSegment
|
|
if err := json.Unmarshal(segmentsRaw, &segments); err != nil {
|
|
return nil, &ParseError{Message: fmt.Sprintf("failed to parse segments: %v", err)}
|
|
}
|
|
if len(segments) == 0 {
|
|
return nil, &ParseError{Message: "transcript must contain at least one segment"}
|
|
}
|
|
|
|
if err := validateSourceSegments(segments); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &SourceTranscript{Segments: segments}, nil
|
|
}
|
|
|
|
// Canonical gives segments without source IDs their one-based position.
|
|
func (t *SourceTranscript) Canonical() *Transcript {
|
|
segments := make([]Segment, len(t.Segments))
|
|
for i, s := range t.Segments {
|
|
id := i + 1
|
|
if s.ID != nil {
|
|
id = int(*s.ID)
|
|
}
|
|
segments[i] = Segment{ID: id, Speaker: s.Speaker, Start: s.Start, End: s.End, Text: s.Text, Categories: slices.Clone(s.Categories)}
|
|
}
|
|
return &Transcript{Segments: segments}
|
|
}
|
|
|
|
// Clone copies the segment and category slices for independent mutation.
|
|
func (t *Transcript) Clone() *Transcript {
|
|
if t == nil {
|
|
return &Transcript{}
|
|
}
|
|
segments := slices.Clone(t.Segments)
|
|
for i := range segments {
|
|
segments[i].Categories = slices.Clone(segments[i].Categories)
|
|
}
|
|
return &Transcript{Segments: segments}
|
|
}
|
|
|
|
func extractSegments(raw []byte) (json.RawMessage, error) {
|
|
switch raw[0] {
|
|
case '[':
|
|
return raw, nil
|
|
case '{':
|
|
var envelope map[string]json.RawMessage
|
|
if err := json.Unmarshal(raw, &envelope); err != nil {
|
|
return nil, &ParseError{Message: fmt.Sprintf("failed to parse transcript JSON: %v", err)}
|
|
}
|
|
segments, ok := envelope["segments"]
|
|
if !ok {
|
|
return nil, &ParseError{Message: "transcript object must contain a segments array"}
|
|
}
|
|
segments = bytes.TrimSpace(segments)
|
|
if len(segments) == 0 || segments[0] != '[' {
|
|
return nil, &ParseError{Message: "segments field must be an array"}
|
|
}
|
|
return segments, nil
|
|
default:
|
|
return nil, &ParseError{Message: "transcript must be a JSON array or an object with a segments array"}
|
|
}
|
|
}
|
|
|
|
type sourceID = jsonnumber.Integer
|
|
|
|
func validateSourceSegments(segments []SourceSegment) error {
|
|
for i, s := range segments {
|
|
segLabel := segmentLabel(i, s.ID)
|
|
|
|
if s.Speaker == "" {
|
|
return &ValidationError{Field: fmt.Sprintf("%s.speaker", segLabel), Message: "must not be empty"}
|
|
}
|
|
|
|
if s.Text == "" {
|
|
return &ValidationError{Field: fmt.Sprintf("%s.text", segLabel), Message: "must not be empty"}
|
|
}
|
|
|
|
if err := validateTime(s.Start, fmt.Sprintf("%s.start", segLabel)); err != nil {
|
|
return err
|
|
}
|
|
if err := validateTime(s.End, fmt.Sprintf("%s.end", segLabel)); err != nil {
|
|
return err
|
|
}
|
|
|
|
if s.End < s.Start {
|
|
return &ValidationError{
|
|
Field: fmt.Sprintf("%s.end", segLabel),
|
|
Message: fmt.Sprintf("end (%g) must be greater than or equal to start (%g)", s.End, s.Start),
|
|
}
|
|
}
|
|
|
|
for j, cat := range s.Categories {
|
|
if cat == "" {
|
|
return &ValidationError{
|
|
Field: fmt.Sprintf("%s.categories[%d]", segLabel, j),
|
|
Message: "must not be empty",
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
seenIDs := make(map[int]int)
|
|
for i, s := range segments {
|
|
if s.ID != nil {
|
|
if firstIdx, exists := seenIDs[int(*s.ID)]; exists {
|
|
return &ValidationError{
|
|
Field: fmt.Sprintf("segment[%d].id", i),
|
|
Message: fmt.Sprintf("duplicate id %d (first used at segment[%d])", *s.ID, firstIdx),
|
|
}
|
|
}
|
|
seenIDs[int(*s.ID)] = i
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func validateTime(t float64, field string) error {
|
|
if math.IsNaN(t) || math.IsInf(t, 0) {
|
|
return &ValidationError{Field: field, Message: "must be a finite number"}
|
|
}
|
|
if t < 0 {
|
|
return &ValidationError{Field: field, Message: "must be non-negative"}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func segmentLabel(index int, id *sourceID) string {
|
|
if id != nil {
|
|
return fmt.Sprintf("segment[%d] (id=%d)", index, *id)
|
|
}
|
|
return fmt.Sprintf("segment[%d]", index)
|
|
}
|
|
|
|
// OutputSegments supplies the typed segment array used by both output envelopes.
|
|
// An empty transcript is encoded as an empty JSON array.
|
|
func (t *Transcript) OutputSegments() []Segment {
|
|
if t == nil || t.Segments == nil {
|
|
return []Segment{}
|
|
}
|
|
return t.Segments
|
|
}
|
|
|
|
func TranscriptToJSON(t *Transcript) ([]byte, error) {
|
|
return json.MarshalIndent(t.OutputSegments(), "", " ")
|
|
}
|