Resolve citations using coalesced source ranges
This commit is contained in:
@@ -2,6 +2,7 @@ package shared
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
@@ -24,23 +25,41 @@ func NewCitationResolver(doc *source.SourceDocument) (*CitationResolver, error)
|
||||
// CitedText resolves cited source ranges in document order, including each
|
||||
// source unit once, and joins the resulting text with newlines.
|
||||
func (r *CitationResolver) CitedText(refs []source.SourceRef) (string, error) {
|
||||
included := make([]bool, len(r.doc.Units))
|
||||
type interval struct{ start, end int }
|
||||
intervals := make([]interval, 0, len(refs))
|
||||
for _, ref := range refs {
|
||||
if err := r.index.ValidateRef(ref); err != nil {
|
||||
return "", fmt.Errorf("resolve cited source range: %w", err)
|
||||
}
|
||||
start, _ := r.index.Position(ref.StartUnitID)
|
||||
end, _ := r.index.Position(ref.EndUnitID)
|
||||
for index := start; index <= end; index++ {
|
||||
included[index] = true
|
||||
intervals = append(intervals, interval{start: start, end: end})
|
||||
}
|
||||
sort.Slice(intervals, func(i, j int) bool {
|
||||
if intervals[i].start != intervals[j].start {
|
||||
return intervals[i].start < intervals[j].start
|
||||
}
|
||||
return intervals[i].end < intervals[j].end
|
||||
})
|
||||
merged := intervals[:0]
|
||||
for _, item := range intervals {
|
||||
if len(merged) == 0 || item.start > merged[len(merged)-1].end+1 {
|
||||
merged = append(merged, item)
|
||||
} else {
|
||||
previous := &merged[len(merged)-1]
|
||||
previous.end = max(previous.end, item.end)
|
||||
}
|
||||
}
|
||||
|
||||
parts := make([]string, 0, len(r.doc.Units))
|
||||
for index, unit := range r.doc.Units {
|
||||
if included[index] {
|
||||
parts = append(parts, unit.Text)
|
||||
var text strings.Builder
|
||||
first := true
|
||||
for _, item := range merged {
|
||||
for position := item.start; position <= item.end; position++ {
|
||||
if !first {
|
||||
text.WriteByte('\n')
|
||||
}
|
||||
first = false
|
||||
text.WriteString(r.doc.Units[position].Text)
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, "\n"), nil
|
||||
return text.String(), nil
|
||||
}
|
||||
|
||||
@@ -75,6 +75,38 @@ func TestNewCitationResolverRejectsNilDocument(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCitationResolverUsesSourceOrderAndPreservesEmptyText(t *testing.T) {
|
||||
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{
|
||||
{ID: 40, Text: "first"}, {ID: 10, Text: ""}, {ID: 70, Text: "third"}, {ID: 20, Text: "last"},
|
||||
}}
|
||||
resolver, err := NewCitationResolver(doc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
refs []source.SourceRef
|
||||
want string
|
||||
}{
|
||||
{"sparse", []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}, {SourceID: "session", StartUnitID: 40, EndUnitID: 40}}, "first\nlast"},
|
||||
{"nested and overlapping", []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 70}, {SourceID: "session", StartUnitID: 40, EndUnitID: 70}, {SourceID: "session", StartUnitID: 70, EndUnitID: 20}}, "first\n\nthird\nlast"},
|
||||
{"empty text", []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 10}}, ""},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := resolver.CitedText(tc.refs)
|
||||
if err != nil || got != tc.want {
|
||||
t.Fatalf("CitedText() = %q, %v; want %q", got, err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
firstInvalid := source.SourceRef{SourceID: "other", StartUnitID: 40, EndUnitID: 40}
|
||||
_, firstErr := resolver.CitedText([]source.SourceRef{firstInvalid})
|
||||
_, err = resolver.CitedText([]source.SourceRef{firstInvalid, {SourceID: "session", StartUnitID: 20, EndUnitID: 40}})
|
||||
if firstErr == nil || err == nil || err.Error() != firstErr.Error() {
|
||||
t.Fatalf("CitedText() error = %v, want first reference error %v", err, firstErr)
|
||||
}
|
||||
}
|
||||
|
||||
func citationDocument() *source.SourceDocument {
|
||||
return &source.SourceDocument{
|
||||
ID: "session",
|
||||
|
||||
Reference in New Issue
Block a user