Share occurrence duplicate grouping bookkeeping
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package locationoccurrences
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
)
|
||||
|
||||
func TestCollapseDuplicatesPreservesIndependentEvidenceAndOriginalIndices(t *testing.T) {
|
||||
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 10}, {ID: 20}}}
|
||||
ref := func(id int) source.SourceRef {
|
||||
return source.SourceRef{SourceID: doc.ID, StartUnitID: id, EndUnitID: id}
|
||||
}
|
||||
a := dnd.LocationOccurrence{LocationID: "place", Name: "Inn", Kind: dnd.LocationOccurrenceKindMentioned, SourceRefs: []source.SourceRef{ref(10)}}
|
||||
b := dnd.LocationOccurrence{LocationID: "place", Name: "Inn", Kind: dnd.LocationOccurrenceKindMentioned, SourceRefs: []source.SourceRef{ref(20)}}
|
||||
invalid := dnd.LocationOccurrence{LocationID: "place", Name: "Inn", Kind: dnd.LocationOccurrenceKindMentioned, SourceRefs: []source.SourceRef{ref(999)}}
|
||||
records := []normalizedRecord{{a, 10}, {b, 20}, {invalid, 30}, {b, 40}, {invalid, 50}, {a, 60}}
|
||||
output, findings := collapseDuplicates(records, source.NewDocumentIndex(doc))
|
||||
if want := []dnd.LocationOccurrence{a, b, invalid, invalid}; !reflect.DeepEqual(output, want) {
|
||||
t.Fatalf("output = %#v, want %#v", output, want)
|
||||
}
|
||||
if len(findings) != 2 || !strings.Contains(findings[0].Message, "retained input index 10") || !strings.Contains(findings[0].Message, "removed input index 60") || !strings.Contains(findings[1].Message, "retained input index 20") || !strings.Contains(findings[1].Message, "removed input index 40") {
|
||||
t.Fatalf("findings = %#v", findings)
|
||||
}
|
||||
_, reversed := collapseDuplicates([]normalizedRecord{{b, 20}, {a, 10}, {a, 60}, {b, 40}}, source.NewDocumentIndex(doc))
|
||||
if len(reversed) != 2 || !strings.Contains(reversed[0].Message, "retained input index 20") || !strings.Contains(reversed[1].Message, "retained input index 10") {
|
||||
t.Fatalf("permuted findings = %#v", reversed)
|
||||
}
|
||||
output[0].SourceRefs[0].StartUnitID = 20
|
||||
if records[0].occurrence.SourceRefs[0].StartUnitID != 10 {
|
||||
t.Fatal("output aliases input evidence")
|
||||
}
|
||||
}
|
||||
@@ -164,44 +164,29 @@ func sourceRefsEqual(left, right []source.SourceRef) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type duplicateGroup struct {
|
||||
retainedIndex int
|
||||
removed []int
|
||||
}
|
||||
|
||||
func collapseDuplicates(records []normalizedRecord, documentIndex source.DocumentIndex) ([]dnd.LocationOccurrence, []diagnostics.Finding) {
|
||||
if len(records) == 0 {
|
||||
return make([]dnd.LocationOccurrence, 0), nil
|
||||
}
|
||||
keep := make([]bool, len(records))
|
||||
groups := make([]duplicateGroup, 0)
|
||||
groupByKey := make(map[string]int)
|
||||
for index, record := range records {
|
||||
if !validSourceRefs(documentIndex, record.occurrence.SourceRefs) {
|
||||
keep[index] = true
|
||||
continue
|
||||
// This family owns eligibility and maps shared positions to original indices.
|
||||
retained, groups := shared.GroupDuplicates(len(records), func(index int) (string, bool) {
|
||||
occurrence := records[index].occurrence
|
||||
if !validSourceRefs(documentIndex, occurrence.SourceRefs) {
|
||||
return "", false
|
||||
}
|
||||
key := exactIdentity(record.occurrence)
|
||||
groupIndex, exists := groupByKey[key]
|
||||
if !exists {
|
||||
groupByKey[key] = len(groups)
|
||||
groups = append(groups, duplicateGroup{retainedIndex: record.inputIndex})
|
||||
keep[index] = true
|
||||
continue
|
||||
}
|
||||
groups[groupIndex].removed = append(groups[groupIndex].removed, record.inputIndex)
|
||||
return exactIdentity(occurrence), true
|
||||
})
|
||||
output := make([]dnd.LocationOccurrence, 0, len(retained))
|
||||
for _, index := range retained {
|
||||
output = append(output, cloneOccurrence(records[index].occurrence))
|
||||
}
|
||||
output := make([]dnd.LocationOccurrence, 0, len(records))
|
||||
for index, record := range records {
|
||||
if keep[index] {
|
||||
output = append(output, cloneOccurrence(record.occurrence))
|
||||
}
|
||||
}
|
||||
findings := make([]diagnostics.Finding, 0)
|
||||
findings := make([]diagnostics.Finding, 0, len(groups))
|
||||
for _, group := range groups {
|
||||
if len(group.removed) > 0 {
|
||||
findings = append(findings, duplicateFinding(group.retainedIndex, group.removed))
|
||||
removed := make([]int, len(group.Removed))
|
||||
for index, position := range group.Removed {
|
||||
removed[index] = records[position].inputIndex
|
||||
}
|
||||
findings = append(findings, duplicateFinding(records[group.Representative].inputIndex, removed))
|
||||
}
|
||||
if len(records) == 0 {
|
||||
return output, nil
|
||||
}
|
||||
return output, findings
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package npcoccurrences
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
)
|
||||
|
||||
func TestCollapseDuplicatesPreservesIndependentEvidenceAndOriginalIndices(t *testing.T) {
|
||||
doc := testDocument()
|
||||
ref := func(id int) source.SourceRef {
|
||||
return source.SourceRef{SourceID: doc.ID, StartUnitID: id, EndUnitID: id}
|
||||
}
|
||||
a := occurrence("Ária", dnd.NPCOccurrenceKindDialogue, ref(10))
|
||||
b := occurrence("Ária", dnd.NPCOccurrenceKindDialogue, ref(20))
|
||||
invalid := occurrence("Ária", dnd.NPCOccurrenceKindDialogue, ref(999))
|
||||
records := []normalizedRecord{{a, 10}, {b, 20}, {invalid, 30}, {b, 40}, {invalid, 50}, {a, 60}}
|
||||
output, findings := collapseDuplicates(records, source.NewDocumentIndex(doc))
|
||||
if want := []dnd.NPCOccurrence{a, b, invalid, invalid}; !reflect.DeepEqual(output, want) {
|
||||
t.Fatalf("output = %#v, want %#v", output, want)
|
||||
}
|
||||
if len(findings) != 2 || !strings.Contains(findings[0].Message, "retained input index 10") || !strings.Contains(findings[0].Message, "removed input index 60") || !strings.Contains(findings[1].Message, "retained input index 20") || !strings.Contains(findings[1].Message, "removed input index 40") {
|
||||
t.Fatalf("findings = %#v", findings)
|
||||
}
|
||||
_, reversed := collapseDuplicates([]normalizedRecord{{b, 20}, {a, 10}, {a, 60}, {b, 40}}, source.NewDocumentIndex(doc))
|
||||
if len(reversed) != 2 || !strings.Contains(reversed[0].Message, "retained input index 20") || !strings.Contains(reversed[1].Message, "retained input index 10") {
|
||||
t.Fatalf("permuted findings = %#v", reversed)
|
||||
}
|
||||
output[0].SourceRefs[0].StartUnitID = 30
|
||||
if records[0].occurrence.SourceRefs[0].StartUnitID != 10 {
|
||||
t.Fatal("output aliases input evidence")
|
||||
}
|
||||
}
|
||||
@@ -160,44 +160,29 @@ func cloneOccurrence(input dnd.NPCOccurrence) dnd.NPCOccurrence {
|
||||
return output
|
||||
}
|
||||
|
||||
type duplicateGroup struct {
|
||||
retainedIndex int
|
||||
removed []int
|
||||
}
|
||||
|
||||
func collapseDuplicates(records []normalizedRecord, documentIndex source.DocumentIndex) ([]dnd.NPCOccurrence, []diagnostics.Finding) {
|
||||
if len(records) == 0 {
|
||||
return make([]dnd.NPCOccurrence, 0), nil
|
||||
}
|
||||
keep := make([]bool, len(records))
|
||||
groups := make([]duplicateGroup, 0)
|
||||
groupByKey := make(map[string]int)
|
||||
for index, record := range records {
|
||||
if !occurrencemodel.ValidSourceRefs(documentIndex, record.occurrence.SourceRefs) {
|
||||
keep[index] = true
|
||||
continue
|
||||
// This family owns eligibility and maps shared positions to original indices.
|
||||
retained, groups := shared.GroupDuplicates(len(records), func(index int) (string, bool) {
|
||||
occurrence := records[index].occurrence
|
||||
if !occurrencemodel.ValidSourceRefs(documentIndex, occurrence.SourceRefs) {
|
||||
return "", false
|
||||
}
|
||||
key := occurrencemodel.ExactIdentity(record.occurrence)
|
||||
groupIndex, exists := groupByKey[key]
|
||||
if !exists {
|
||||
groupByKey[key] = len(groups)
|
||||
groups = append(groups, duplicateGroup{retainedIndex: record.inputIndex})
|
||||
keep[index] = true
|
||||
continue
|
||||
}
|
||||
groups[groupIndex].removed = append(groups[groupIndex].removed, record.inputIndex)
|
||||
return occurrencemodel.ExactIdentity(occurrence), true
|
||||
})
|
||||
output := make([]dnd.NPCOccurrence, 0, len(retained))
|
||||
for _, index := range retained {
|
||||
output = append(output, cloneOccurrence(records[index].occurrence))
|
||||
}
|
||||
output := make([]dnd.NPCOccurrence, 0, len(records))
|
||||
for index, record := range records {
|
||||
if keep[index] {
|
||||
output = append(output, cloneOccurrence(record.occurrence))
|
||||
}
|
||||
}
|
||||
findings := make([]diagnostics.Finding, 0)
|
||||
findings := make([]diagnostics.Finding, 0, len(groups))
|
||||
for _, group := range groups {
|
||||
if len(group.removed) != 0 {
|
||||
findings = append(findings, duplicateFinding(group.retainedIndex, group.removed))
|
||||
removed := make([]int, len(group.Removed))
|
||||
for index, position := range group.Removed {
|
||||
removed[index] = records[position].inputIndex
|
||||
}
|
||||
findings = append(findings, duplicateFinding(records[group.Representative].inputIndex, removed))
|
||||
}
|
||||
if len(records) == 0 {
|
||||
return output, nil
|
||||
}
|
||||
return output, findings
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user