Implemented a module to detect backchannel segments, and updated the coalesce module to ignore them when coalescing same-speaker turns
This commit is contained in:
60
internal/backchannel/backchannel.go
Normal file
60
internal/backchannel/backchannel.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package backchannel
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/model"
|
||||
)
|
||||
|
||||
const Category = "backchannel"
|
||||
|
||||
var patterns = []*regexp.Regexp{
|
||||
regexp.MustCompile(`(?i)^(yeah|yep|yes|right|okay|ok|sure|mm+h?m+|uh[- ]huh|mhm|mm-hmm)\.?$`),
|
||||
regexp.MustCompile(`(?i)^(yeah|yep|right|okay|ok)([,.\s]+(yeah|yep|right|okay|ok))*\.?$`),
|
||||
regexp.MustCompile(`(?i)^(i see|got it|makes sense|that makes sense|fair enough|sounds good)\.?$`),
|
||||
}
|
||||
|
||||
// Apply tags matching short acknowledgement segments.
|
||||
func Apply(in model.MergedTranscript) (model.MergedTranscript, int) {
|
||||
tagged := 0
|
||||
for index := range in.Segments {
|
||||
if !matches(in.Segments[index]) {
|
||||
continue
|
||||
}
|
||||
if hasCategory(in.Segments[index], Category) {
|
||||
continue
|
||||
}
|
||||
in.Segments[index].Categories = append(in.Segments[index].Categories, Category)
|
||||
tagged++
|
||||
}
|
||||
return in, tagged
|
||||
}
|
||||
|
||||
func matches(segment model.Segment) bool {
|
||||
text := strings.TrimSpace(segment.Text)
|
||||
if text == "" {
|
||||
return false
|
||||
}
|
||||
if len(strings.Fields(text)) > 3 {
|
||||
return false
|
||||
}
|
||||
if segment.End-segment.Start > 1.0 {
|
||||
return false
|
||||
}
|
||||
for _, pattern := range patterns {
|
||||
if pattern.MatchString(text) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func hasCategory(segment model.Segment, category string) bool {
|
||||
for _, existing := range segment.Categories {
|
||||
if existing == category {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
104
internal/backchannel/backchannel_test.go
Normal file
104
internal/backchannel/backchannel_test.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package backchannel
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/model"
|
||||
)
|
||||
|
||||
func TestApplyTagsVerySafeBackchannels(t *testing.T) {
|
||||
for _, text := range []string{"yeah", "Yep.", "mmhm", "uh-huh", "mm-hmm"} {
|
||||
t.Run(text, func(t *testing.T) {
|
||||
got, tagged := Apply(transcript(segment(text, 1, 1.5)))
|
||||
if tagged != 1 {
|
||||
t.Fatalf("tagged = %d, want 1", tagged)
|
||||
}
|
||||
assertCategories(t, got.Segments[0], []string{Category})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyTagsRepeatedBackchannels(t *testing.T) {
|
||||
got, tagged := Apply(transcript(segment("Yeah, okay yep.", 1, 1.8)))
|
||||
if tagged != 1 {
|
||||
t.Fatalf("tagged = %d, want 1", tagged)
|
||||
}
|
||||
assertCategories(t, got.Segments[0], []string{Category})
|
||||
}
|
||||
|
||||
func TestApplyTagsShortAcknowledgements(t *testing.T) {
|
||||
for _, text := range []string{"i see", "Got it.", "sounds good"} {
|
||||
t.Run(text, func(t *testing.T) {
|
||||
got, tagged := Apply(transcript(segment(text, 1, 1.8)))
|
||||
if tagged != 1 {
|
||||
t.Fatalf("tagged = %d, want 1", tagged)
|
||||
}
|
||||
assertCategories(t, got.Segments[0], []string{Category})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyMatchesTrimAwareCaseInsensitive(t *testing.T) {
|
||||
got, tagged := Apply(transcript(segment(" YES. ", 1, 1.2)))
|
||||
if tagged != 1 {
|
||||
t.Fatalf("tagged = %d, want 1", tagged)
|
||||
}
|
||||
assertCategories(t, got.Segments[0], []string{Category})
|
||||
}
|
||||
|
||||
func TestApplyDoesNotTagNonMatches(t *testing.T) {
|
||||
got, tagged := Apply(transcript(segment("yeah I think so", 1, 1.5)))
|
||||
if tagged != 0 {
|
||||
t.Fatalf("tagged = %d, want 0", tagged)
|
||||
}
|
||||
assertCategories(t, got.Segments[0], nil)
|
||||
}
|
||||
|
||||
func TestApplyRejectsWordCountOverThree(t *testing.T) {
|
||||
got, tagged := Apply(transcript(segment("that makes sense okay", 1, 1.5)))
|
||||
if tagged != 0 {
|
||||
t.Fatalf("tagged = %d, want 0", tagged)
|
||||
}
|
||||
assertCategories(t, got.Segments[0], nil)
|
||||
}
|
||||
|
||||
func TestApplyRejectsDurationOverOneSecond(t *testing.T) {
|
||||
got, tagged := Apply(transcript(segment("yeah", 1, 2.1)))
|
||||
if tagged != 0 {
|
||||
t.Fatalf("tagged = %d, want 0", tagged)
|
||||
}
|
||||
assertCategories(t, got.Segments[0], nil)
|
||||
}
|
||||
|
||||
func TestApplyPreservesExistingCategoriesAndAvoidsDuplicate(t *testing.T) {
|
||||
existing := segment("yeah", 1, 1.2)
|
||||
existing.Categories = []string{"manual", Category}
|
||||
|
||||
got, tagged := Apply(transcript(existing))
|
||||
if tagged != 0 {
|
||||
t.Fatalf("tagged = %d, want 0", tagged)
|
||||
}
|
||||
assertCategories(t, got.Segments[0], []string{"manual", Category})
|
||||
}
|
||||
|
||||
func transcript(segments ...model.Segment) model.MergedTranscript {
|
||||
return model.MergedTranscript{Segments: segments}
|
||||
}
|
||||
|
||||
func segment(text string, start float64, end float64) model.Segment {
|
||||
return model.Segment{
|
||||
Source: "input.json",
|
||||
Speaker: "Alice",
|
||||
Start: start,
|
||||
End: end,
|
||||
Text: text,
|
||||
}
|
||||
}
|
||||
|
||||
func assertCategories(t *testing.T, segment model.Segment, want []string) {
|
||||
t.Helper()
|
||||
if !reflect.DeepEqual(segment.Categories, want) {
|
||||
t.Fatalf("categories = %v, want %v", segment.Categories, want)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user