Order spell and NPC extraction by document position

This commit is contained in:
2026-07-24 14:29:19 +00:00
parent 83fde83a58
commit 9bbf2535dd
8 changed files with 138 additions and 111 deletions

View File

@@ -6,18 +6,19 @@ import (
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
)
func canonicalizeResponse(response *extractionResponse, doc *source.SourceDocument) {
func canonicalizeResponse(response *extractionResponse, order shared.SourceRefOrder, sourceID string) {
if response == nil {
return
}
for index := range response.NPCs {
canonicalizeNPC(&response.NPCs[index])
canonicalizeNPC(&response.NPCs[index], order, sourceID)
}
sort.SliceStable(response.NPCs, func(i, j int) bool {
left, leftOK := earliestSourceIndex(doc, response.NPCs[i])
right, rightOK := earliestSourceIndex(doc, response.NPCs[j])
left, leftOK := order.EarliestValid(canonicalSourceRefs(response.NPCs[i].SourceRefs, sourceID))
right, rightOK := order.EarliestValid(canonicalSourceRefs(response.NPCs[j].SourceRefs, sourceID))
if leftOK != rightOK {
return leftOK
}
@@ -28,67 +29,11 @@ func canonicalizeResponse(response *extractionResponse, doc *source.SourceDocume
})
}
func canonicalizeNPC(npc *npcResponse) {
func canonicalizeNPC(npc *npcResponse, order shared.SourceRefOrder, sourceID string) {
if npc == nil {
return
}
sort.SliceStable(npc.SourceRefs, func(i, j int) bool {
left := npc.SourceRefs[i]
right := npc.SourceRefs[j]
if unitSortValue(left.StartUnitID) != unitSortValue(right.StartUnitID) {
return unitSortValue(left.StartUnitID) < unitSortValue(right.StartUnitID)
}
return unitSortValue(left.EndUnitID) < unitSortValue(right.EndUnitID)
})
npc.SourceRefs = dedupeSourceRefs(npc.SourceRefs)
}
func dedupeSourceRefs(refs []npcSourceRefResponse) []npcSourceRefResponse {
if len(refs) < 2 {
return refs
}
out := refs[:0]
var previous npcSourceRefResponse
for index, ref := range refs {
if index > 0 && sameSourceRef(previous, ref) {
continue
}
out = append(out, ref)
previous = ref
}
return out
}
func sameSourceRef(left npcSourceRefResponse, right npcSourceRefResponse) bool {
return left.StartUnitID == right.StartUnitID && left.EndUnitID == right.EndUnitID
}
func earliestSourceIndex(doc *source.SourceDocument, npc npcResponse) (int, bool) {
earliest := 0
found := false
for _, ref := range npc.SourceRefs {
start := ref.StartUnitID
end := ref.EndUnitID
if start > 0 && end > 0 {
startIndex, startOK := source.UnitIndex(doc, start)
endIndex, endOK := source.UnitIndex(doc, end)
if !startOK || !endOK || startIndex > endIndex {
continue
}
if !found || startIndex < earliest {
earliest = startIndex
found = true
}
}
}
return earliest, found
}
func unitSortValue(value int) int {
if value <= 0 {
return int(^uint(0) >> 1)
}
return value
npc.SourceRefs = npcResponseRefs(order.Canonicalize(canonicalSourceRefs(npc.SourceRefs, sourceID)))
}
func canonicalNPCList(response extractionResponse, sourceID string) dnd.NPCList {
@@ -120,3 +65,14 @@ func canonicalSourceRefs(values []npcSourceRefResponse, sourceID string) []sourc
}
return out
}
func npcResponseRefs(values []source.SourceRef) []npcSourceRefResponse {
if values == nil {
return nil
}
out := make([]npcSourceRefResponse, len(values))
for index, value := range values {
out[index] = npcSourceRefResponse{StartUnitID: value.StartUnitID, EndUnitID: value.EndUnitID}
}
return out
}