Add output validation against a defined JSON schema
This commit is contained in:
68
internal/artifact/transcript.go
Normal file
68
internal/artifact/transcript.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package artifact
|
||||
|
||||
import (
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/config"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/model"
|
||||
"gitea.maximumdirect.net/eric/seriatim/schema"
|
||||
)
|
||||
|
||||
const (
|
||||
ApplicationName = "seriatim"
|
||||
Version = "dev"
|
||||
)
|
||||
|
||||
// FromMerged converts the internal merged transcript model into the public
|
||||
// serialized output contract.
|
||||
func FromMerged(cfg config.Config, merged model.MergedTranscript) schema.Transcript {
|
||||
segments := make([]schema.Segment, len(merged.Segments))
|
||||
for index, segment := range merged.Segments {
|
||||
segments[index] = schema.Segment{
|
||||
ID: segment.ID,
|
||||
Source: segment.Source,
|
||||
SourceSegmentIndex: copyIntPtr(segment.SourceSegmentIndex),
|
||||
SourceRef: segment.SourceRef,
|
||||
DerivedFrom: append([]string(nil), segment.DerivedFrom...),
|
||||
Speaker: segment.Speaker,
|
||||
Start: segment.Start,
|
||||
End: segment.End,
|
||||
Text: segment.Text,
|
||||
Categories: append([]string(nil), segment.Categories...),
|
||||
OverlapGroupID: segment.OverlapGroupID,
|
||||
}
|
||||
}
|
||||
|
||||
overlapGroups := make([]schema.OverlapGroup, len(merged.OverlapGroups))
|
||||
for index, group := range merged.OverlapGroups {
|
||||
overlapGroups[index] = schema.OverlapGroup{
|
||||
ID: group.ID,
|
||||
Start: group.Start,
|
||||
End: group.End,
|
||||
Segments: append([]string(nil), group.Segments...),
|
||||
Speakers: append([]string(nil), group.Speakers...),
|
||||
Class: group.Class,
|
||||
Resolution: group.Resolution,
|
||||
}
|
||||
}
|
||||
|
||||
return schema.Transcript{
|
||||
Metadata: schema.Metadata{
|
||||
Application: ApplicationName,
|
||||
Version: Version,
|
||||
InputReader: cfg.InputReader,
|
||||
InputFiles: append([]string(nil), cfg.InputFiles...),
|
||||
PreprocessingModules: append([]string(nil), cfg.PreprocessingModules...),
|
||||
PostprocessingModules: append([]string(nil), cfg.PostprocessingModules...),
|
||||
OutputModules: append([]string(nil), cfg.OutputModules...),
|
||||
},
|
||||
Segments: segments,
|
||||
OverlapGroups: overlapGroups,
|
||||
}
|
||||
}
|
||||
|
||||
func copyIntPtr(value *int) *int {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
copied := *value
|
||||
return &copied
|
||||
}
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"os"
|
||||
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/config"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/model"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/report"
|
||||
"gitea.maximumdirect.net/eric/seriatim/schema"
|
||||
)
|
||||
|
||||
type jsonOutputWriter struct{}
|
||||
@@ -16,7 +16,7 @@ func (jsonOutputWriter) Name() string {
|
||||
return "json"
|
||||
}
|
||||
|
||||
func (jsonOutputWriter) Write(ctx context.Context, out model.FinalTranscript, rpt report.Report, cfg config.Config) ([]report.Event, error) {
|
||||
func (jsonOutputWriter) Write(ctx context.Context, out schema.Transcript, rpt report.Report, cfg config.Config) ([]report.Event, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -34,6 +34,6 @@ func (jsonOutputWriter) Write(ctx context.Context, out model.FinalTranscript, rp
|
||||
}
|
||||
|
||||
return []report.Event{
|
||||
report.Info("output", "json", "wrote placeholder transcript JSON"),
|
||||
report.Info("output", "json", "wrote transcript JSON"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/artifact"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/autocorrect"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/backchannel"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/coalesce"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/model"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/overlap"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/report"
|
||||
"gitea.maximumdirect.net/eric/seriatim/schema"
|
||||
)
|
||||
|
||||
type noopPostprocessor struct {
|
||||
@@ -52,6 +54,27 @@ func (assignIDs) Process(ctx context.Context, in model.MergedTranscript, cfg con
|
||||
}, nil
|
||||
}
|
||||
|
||||
type validateOutput struct{}
|
||||
|
||||
func (validateOutput) Name() string {
|
||||
return "validate-output"
|
||||
}
|
||||
|
||||
func (validateOutput) Process(ctx context.Context, in model.MergedTranscript, cfg config.Config) (model.MergedTranscript, []report.Event, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return model.MergedTranscript{}, nil, err
|
||||
}
|
||||
|
||||
transcript := artifact.FromMerged(cfg, in)
|
||||
if err := schema.ValidateTranscript(transcript); err != nil {
|
||||
return model.MergedTranscript{}, nil, fmt.Errorf("validate-output: %w", err)
|
||||
}
|
||||
|
||||
return in, []report.Event{
|
||||
report.Info("postprocessing", "validate-output", fmt.Sprintf("validated %d output segment(s)", len(in.Segments))),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type detectOverlaps struct{}
|
||||
|
||||
func (detectOverlaps) Name() string {
|
||||
|
||||
60
internal/builtin/postprocess_test.go
Normal file
60
internal/builtin/postprocess_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package builtin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/config"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/model"
|
||||
)
|
||||
|
||||
func TestValidateOutputSucceedsAfterAssignIDs(t *testing.T) {
|
||||
merged := model.MergedTranscript{
|
||||
Segments: []model.Segment{
|
||||
{Source: "input.json", Speaker: "Alice", Start: 1, End: 2, Text: "hello"},
|
||||
{Source: "input.json", Speaker: "Alice", Start: 3, End: 4, Text: "again"},
|
||||
},
|
||||
}
|
||||
|
||||
withIDs, _, err := assignIDs{}.Process(context.Background(), merged, testConfig())
|
||||
if err != nil {
|
||||
t.Fatalf("assign IDs: %v", err)
|
||||
}
|
||||
got, events, err := validateOutput{}.Process(context.Background(), withIDs, testConfig())
|
||||
if err != nil {
|
||||
t.Fatalf("validate output: %v", err)
|
||||
}
|
||||
if len(got.Segments) != 2 {
|
||||
t.Fatalf("segment count = %d, want 2", len(got.Segments))
|
||||
}
|
||||
if len(events) != 1 || !strings.Contains(events[0].Message, "validated 2 output segment(s)") {
|
||||
t.Fatalf("events = %#v", events)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateOutputFailsBeforeAssignIDs(t *testing.T) {
|
||||
merged := model.MergedTranscript{
|
||||
Segments: []model.Segment{
|
||||
{Source: "input.json", Speaker: "Alice", Start: 1, End: 2, Text: "hello"},
|
||||
},
|
||||
}
|
||||
|
||||
_, _, err := validateOutput{}.Process(context.Background(), merged, testConfig())
|
||||
if err == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "segment 0 has id 0; want 1") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func testConfig() config.Config {
|
||||
return config.Config{
|
||||
InputReader: config.DefaultInputReader,
|
||||
InputFiles: []string{"input.json"},
|
||||
PreprocessingModules: []string{"validate-raw", "normalize-speakers", "trim-text"},
|
||||
PostprocessingModules: []string{"assign-ids", "validate-output"},
|
||||
OutputModules: []string{"json"},
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ func NewRegistry() *pipeline.Registry {
|
||||
registry.RegisterPostprocessor(fillerPostprocessor{})
|
||||
registry.RegisterPostprocessor(coalescePostprocessor{})
|
||||
registry.RegisterPostprocessor(assignIDs{})
|
||||
registry.RegisterPostprocessor(noopPostprocessor{name: "validate-output"})
|
||||
registry.RegisterPostprocessor(validateOutput{})
|
||||
registry.RegisterPostprocessor(autocorrectPostprocessor{})
|
||||
registry.RegisterOutputWriter(jsonOutputWriter{})
|
||||
|
||||
|
||||
@@ -102,6 +102,9 @@ func TestMergeWritesMergedOutputAndReport(t *testing.T) {
|
||||
if !equalStrings(gotModules, wantModules) {
|
||||
t.Fatalf("report event order mismatch:\ngot %v\nwant %v", gotModules, wantModules)
|
||||
}
|
||||
if !hasReportEvent(rpt, "postprocessing", "validate-output", "validated 3 output segment(s)") {
|
||||
t.Fatal("expected validate-output report event")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeTieBreakOrder(t *testing.T) {
|
||||
@@ -153,6 +156,28 @@ func TestMergeTieBreakOrder(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeValidateOutputBeforeAssignIDsFails(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
input := writeJSONFile(t, dir, "input.json", `{
|
||||
"segments": [
|
||||
{"start": 1, "end": 2, "text": "hello"}
|
||||
]
|
||||
}`)
|
||||
output := filepath.Join(dir, "merged.json")
|
||||
|
||||
err := executeMerge(
|
||||
"--input-file", input,
|
||||
"--output-file", output,
|
||||
"--postprocessing-modules", "validate-output,assign-ids",
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "validate-output: segment 0 has id 0; want 1") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeDetectsOverlapGroups(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
inputA := writeJSONFile(t, dir, "a.json", `{
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/config"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/model"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/report"
|
||||
"gitea.maximumdirect.net/eric/seriatim/schema"
|
||||
)
|
||||
|
||||
// ModelState identifies which representation a preprocessing module consumes.
|
||||
@@ -52,5 +53,5 @@ type Postprocessor interface {
|
||||
// OutputWriter emits final artifacts.
|
||||
type OutputWriter interface {
|
||||
Name() string
|
||||
Write(ctx context.Context, out model.FinalTranscript, rpt report.Report, cfg config.Config) ([]report.Event, error)
|
||||
Write(ctx context.Context, out schema.Transcript, rpt report.Report, cfg config.Config) ([]report.Event, error)
|
||||
}
|
||||
|
||||
@@ -4,14 +4,16 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/artifact"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/config"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/model"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/report"
|
||||
"gitea.maximumdirect.net/eric/seriatim/schema"
|
||||
)
|
||||
|
||||
const (
|
||||
applicationName = "seriatim"
|
||||
version = "dev"
|
||||
applicationName = artifact.ApplicationName
|
||||
version = artifact.Version
|
||||
)
|
||||
|
||||
// Run validates module composition, executes the pipeline, and emits outputs.
|
||||
@@ -139,29 +141,8 @@ func validatePreprocessors(modules []Preprocessor) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func finalizeTranscript(cfg config.Config, merged model.MergedTranscript) model.FinalTranscript {
|
||||
segments := make([]model.Segment, len(merged.Segments))
|
||||
copy(segments, merged.Segments)
|
||||
for index := range segments {
|
||||
segments[index].Words = nil
|
||||
segments[index].DerivedFrom = append([]string(nil), segments[index].DerivedFrom...)
|
||||
}
|
||||
overlapGroups := make([]model.OverlapGroup, len(merged.OverlapGroups))
|
||||
copy(overlapGroups, merged.OverlapGroups)
|
||||
|
||||
return model.FinalTranscript{
|
||||
Metadata: model.OutputMetadata{
|
||||
Application: applicationName,
|
||||
Version: version,
|
||||
InputReader: cfg.InputReader,
|
||||
InputFiles: append([]string(nil), cfg.InputFiles...),
|
||||
PreprocessingModules: append([]string(nil), cfg.PreprocessingModules...),
|
||||
PostprocessingModules: append([]string(nil), cfg.PostprocessingModules...),
|
||||
OutputModules: append([]string(nil), cfg.OutputModules...),
|
||||
},
|
||||
Segments: segments,
|
||||
OverlapGroups: overlapGroups,
|
||||
}
|
||||
func finalizeTranscript(cfg config.Config, merged model.MergedTranscript) schema.Transcript {
|
||||
return artifact.FromMerged(cfg, merged)
|
||||
}
|
||||
|
||||
func finalizeReport(cfg config.Config, events []report.Event) report.Report {
|
||||
|
||||
Reference in New Issue
Block a user