Added a module to coalesce adjacent same-speaker segments

This commit is contained in:
2026-04-27 19:30:00 -05:00
parent 13d972cb24
commit aab6d12730
12 changed files with 919 additions and 28 deletions

View File

@@ -14,9 +14,13 @@ const (
DefaultInputReader = "json-files"
DefaultOutputModules = "json"
DefaultPreprocessingModules = "validate-raw,normalize-speakers,trim-text"
DefaultPostprocessingModules = "detect-overlaps,resolve-overlaps,detect-overlaps,autocorrect,assign-ids,validate-output"
DefaultPostprocessingModules = "detect-overlaps,resolve-overlaps,coalesce,detect-overlaps,autocorrect,assign-ids,validate-output"
DefaultOverlapWordRunGap = 0.75
DefaultWordRunReorderWindow = 0.4
DefaultCoalesceGap = 3.0
DefaultCoalesceGapValue = "3.0"
OverlapWordRunGapEnv = "SERIATIM_OVERLAP_WORD_RUN_GAP"
WordRunReorderWindowEnv = "SERIATIM_OVERLAP_WORD_RUN_REORDER_WINDOW"
)
// MergeOptions captures raw CLI option values before validation.
@@ -30,6 +34,7 @@ type MergeOptions struct {
OutputModules string
PreprocessingModules string
PostprocessingModules string
CoalesceGap string
}
// Config is the validated runtime configuration for a merge invocation.
@@ -44,6 +49,8 @@ type Config struct {
PreprocessingModules []string
PostprocessingModules []string
OverlapWordRunGap float64
WordRunReorderWindow float64
CoalesceGap float64
}
// NewMergeConfig validates raw merge options and returns normalized config.
@@ -54,6 +61,8 @@ func NewMergeConfig(opts MergeOptions) (Config, error) {
PreprocessingModules: nil,
PostprocessingModules: nil,
OverlapWordRunGap: DefaultOverlapWordRunGap,
WordRunReorderWindow: DefaultWordRunReorderWindow,
CoalesceGap: DefaultCoalesceGap,
}
if cfg.InputReader == "" {
@@ -119,6 +128,14 @@ func NewMergeConfig(opts MergeOptions) (Config, error) {
if err != nil {
return Config{}, err
}
cfg.WordRunReorderWindow, err = parseWordRunReorderWindow()
if err != nil {
return Config{}, err
}
cfg.CoalesceGap, err = parseCoalesceGap(opts.CoalesceGap)
if err != nil {
return Config{}, err
}
return cfg, nil
}
@@ -198,21 +215,45 @@ func requireFile(path string, flag string) error {
}
func parseOverlapWordRunGap() (float64, error) {
value := strings.TrimSpace(os.Getenv(OverlapWordRunGapEnv))
return parsePositiveFloatEnv(OverlapWordRunGapEnv, DefaultOverlapWordRunGap)
}
func parseWordRunReorderWindow() (float64, error) {
return parsePositiveFloatEnv(WordRunReorderWindowEnv, DefaultWordRunReorderWindow)
}
func parseCoalesceGap(value string) (float64, error) {
value = strings.TrimSpace(value)
if value == "" {
return DefaultOverlapWordRunGap, nil
return DefaultCoalesceGap, nil
}
gap, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, fmt.Errorf("%s must be a positive number of seconds: %w", OverlapWordRunGapEnv, err)
return 0, fmt.Errorf("--coalesce-gap must be a non-negative number of seconds: %w", err)
}
if gap <= 0 {
return 0, fmt.Errorf("%s must be positive", OverlapWordRunGapEnv)
if gap < 0 {
return 0, fmt.Errorf("--coalesce-gap must be non-negative")
}
return gap, nil
}
func parsePositiveFloatEnv(name string, defaultValue float64) (float64, error) {
value := strings.TrimSpace(os.Getenv(name))
if value == "" {
return defaultValue, nil
}
parsed, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, fmt.Errorf("%s must be a positive number of seconds: %w", name, err)
}
if parsed <= 0 {
return 0, fmt.Errorf("%s must be positive", name)
}
return parsed, nil
}
func contains(values []string, target string) bool {
for _, value := range values {
if value == target {

View File

@@ -126,6 +126,186 @@ func TestOverlapWordRunGapRejectsInvalidEnvOverride(t *testing.T) {
}
}
func TestWordRunReorderWindowDefaultsTo04(t *testing.T) {
t.Setenv(WordRunReorderWindowEnv, "")
dir := t.TempDir()
input := writeTempFile(t, dir, "input.json")
output := filepath.Join(dir, "merged.json")
cfg, err := NewMergeConfig(MergeOptions{
InputFiles: []string{input},
OutputFile: output,
InputReader: DefaultInputReader,
OutputModules: DefaultOutputModules,
PreprocessingModules: DefaultPreprocessingModules,
PostprocessingModules: DefaultPostprocessingModules,
})
if err != nil {
t.Fatalf("config failed: %v", err)
}
if cfg.WordRunReorderWindow != DefaultWordRunReorderWindow {
t.Fatalf("window = %f, want %f", cfg.WordRunReorderWindow, DefaultWordRunReorderWindow)
}
}
func TestWordRunReorderWindowUsesValidEnvOverride(t *testing.T) {
t.Setenv(WordRunReorderWindowEnv, "0.2")
dir := t.TempDir()
input := writeTempFile(t, dir, "input.json")
output := filepath.Join(dir, "merged.json")
cfg, err := NewMergeConfig(MergeOptions{
InputFiles: []string{input},
OutputFile: output,
InputReader: DefaultInputReader,
OutputModules: DefaultOutputModules,
PreprocessingModules: DefaultPreprocessingModules,
PostprocessingModules: DefaultPostprocessingModules,
})
if err != nil {
t.Fatalf("config failed: %v", err)
}
if cfg.WordRunReorderWindow != 0.2 {
t.Fatalf("window = %f, want 0.2", cfg.WordRunReorderWindow)
}
}
func TestWordRunReorderWindowRejectsInvalidEnvOverride(t *testing.T) {
tests := []struct {
name string
value string
want string
}{
{name: "non-numeric", value: "fast", want: "must be a positive number"},
{name: "zero", value: "0", want: "must be positive"},
{name: "negative", value: "-0.1", want: "must be positive"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Setenv(WordRunReorderWindowEnv, test.value)
dir := t.TempDir()
input := writeTempFile(t, dir, "input.json")
output := filepath.Join(dir, "merged.json")
_, err := NewMergeConfig(MergeOptions{
InputFiles: []string{input},
OutputFile: output,
InputReader: DefaultInputReader,
OutputModules: DefaultOutputModules,
PreprocessingModules: DefaultPreprocessingModules,
PostprocessingModules: DefaultPostprocessingModules,
})
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), test.want) {
t.Fatalf("expected error to contain %q, got %v", test.want, err)
}
})
}
}
func TestCoalesceGapDefaultsTo3(t *testing.T) {
dir := t.TempDir()
input := writeTempFile(t, dir, "input.json")
output := filepath.Join(dir, "merged.json")
cfg, err := NewMergeConfig(MergeOptions{
InputFiles: []string{input},
OutputFile: output,
InputReader: DefaultInputReader,
OutputModules: DefaultOutputModules,
PreprocessingModules: DefaultPreprocessingModules,
PostprocessingModules: DefaultPostprocessingModules,
})
if err != nil {
t.Fatalf("config failed: %v", err)
}
if cfg.CoalesceGap != DefaultCoalesceGap {
t.Fatalf("coalesce gap = %f, want %f", cfg.CoalesceGap, DefaultCoalesceGap)
}
}
func TestCoalesceGapUsesValidOverride(t *testing.T) {
dir := t.TempDir()
input := writeTempFile(t, dir, "input.json")
output := filepath.Join(dir, "merged.json")
cfg, err := NewMergeConfig(MergeOptions{
InputFiles: []string{input},
OutputFile: output,
InputReader: DefaultInputReader,
OutputModules: DefaultOutputModules,
PreprocessingModules: DefaultPreprocessingModules,
PostprocessingModules: DefaultPostprocessingModules,
CoalesceGap: "1.5",
})
if err != nil {
t.Fatalf("config failed: %v", err)
}
if cfg.CoalesceGap != 1.5 {
t.Fatalf("coalesce gap = %f, want 1.5", cfg.CoalesceGap)
}
}
func TestCoalesceGapAllowsZero(t *testing.T) {
dir := t.TempDir()
input := writeTempFile(t, dir, "input.json")
output := filepath.Join(dir, "merged.json")
cfg, err := NewMergeConfig(MergeOptions{
InputFiles: []string{input},
OutputFile: output,
InputReader: DefaultInputReader,
OutputModules: DefaultOutputModules,
PreprocessingModules: DefaultPreprocessingModules,
PostprocessingModules: DefaultPostprocessingModules,
CoalesceGap: "0",
})
if err != nil {
t.Fatalf("config failed: %v", err)
}
if cfg.CoalesceGap != 0 {
t.Fatalf("coalesce gap = %f, want 0", cfg.CoalesceGap)
}
}
func TestCoalesceGapRejectsInvalidOverride(t *testing.T) {
tests := []struct {
name string
value string
want string
}{
{name: "non-numeric", value: "fast", want: "must be a non-negative number"},
{name: "negative", value: "-0.1", want: "must be non-negative"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
dir := t.TempDir()
input := writeTempFile(t, dir, "input.json")
output := filepath.Join(dir, "merged.json")
_, err := NewMergeConfig(MergeOptions{
InputFiles: []string{input},
OutputFile: output,
InputReader: DefaultInputReader,
OutputModules: DefaultOutputModules,
PreprocessingModules: DefaultPreprocessingModules,
PostprocessingModules: DefaultPostprocessingModules,
CoalesceGap: test.value,
})
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), test.want) {
t.Fatalf("expected error to contain %q, got %v", test.want, err)
}
})
}
}
func writeTempFile(t *testing.T, dir string, name string) string {
t.Helper()