Add grounded NPC interaction extractor
This commit is contained in:
113
internal/modules/dnd/extract/npcinteractions/canonicalize.go
Normal file
113
internal/modules/dnd/extract/npcinteractions/canonicalize.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package npcinteractions
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
)
|
||||
|
||||
func canonicalizeResponse(response *extractionResponse, doc *source.SourceDocument) {
|
||||
if response == nil {
|
||||
return
|
||||
}
|
||||
for index := range response.Interactions {
|
||||
canonicalizeInteraction(&response.Interactions[index])
|
||||
}
|
||||
sort.SliceStable(response.Interactions, func(i, j int) bool {
|
||||
left, leftOK := earliestSourcePosition(doc, response.Interactions[i])
|
||||
right, rightOK := earliestSourcePosition(doc, response.Interactions[j])
|
||||
if leftOK != rightOK {
|
||||
return leftOK
|
||||
}
|
||||
if !leftOK {
|
||||
return false
|
||||
}
|
||||
return left < right
|
||||
})
|
||||
}
|
||||
|
||||
func canonicalizeInteraction(interaction *interactionResponse) {
|
||||
if interaction == nil {
|
||||
return
|
||||
}
|
||||
sort.SliceStable(interaction.SourceRefs, func(i, j int) bool {
|
||||
left := interaction.SourceRefs[i]
|
||||
right := interaction.SourceRefs[j]
|
||||
if unitSortValue(left.StartUnitID) != unitSortValue(right.StartUnitID) {
|
||||
return unitSortValue(left.StartUnitID) < unitSortValue(right.StartUnitID)
|
||||
}
|
||||
return unitSortValue(left.EndUnitID) < unitSortValue(right.EndUnitID)
|
||||
})
|
||||
interaction.SourceRefs = dedupeSourceRefs(interaction.SourceRefs)
|
||||
}
|
||||
|
||||
func dedupeSourceRefs(refs []interactionSourceRefResponse) []interactionSourceRefResponse {
|
||||
if len(refs) < 2 {
|
||||
return refs
|
||||
}
|
||||
out := refs[:0]
|
||||
var previous interactionSourceRefResponse
|
||||
for index, ref := range refs {
|
||||
if index > 0 && previous == ref {
|
||||
continue
|
||||
}
|
||||
out = append(out, ref)
|
||||
previous = ref
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func earliestSourcePosition(doc *source.SourceDocument, interaction interactionResponse) (int, bool) {
|
||||
if doc == nil {
|
||||
return 0, false
|
||||
}
|
||||
found := false
|
||||
earliest := 0
|
||||
for _, ref := range interaction.SourceRefs {
|
||||
candidate := source.SourceRef{SourceID: doc.ID, StartUnitID: ref.StartUnitID, EndUnitID: ref.EndUnitID}
|
||||
if err := source.ValidateRef(doc, candidate); err != nil {
|
||||
continue
|
||||
}
|
||||
index, ok := source.UnitIndex(doc, candidate.StartUnitID)
|
||||
if !ok || (found && index >= earliest) {
|
||||
continue
|
||||
}
|
||||
earliest = index
|
||||
found = true
|
||||
}
|
||||
return earliest, found
|
||||
}
|
||||
|
||||
func unitSortValue(value int) int {
|
||||
if value <= 0 {
|
||||
return int(^uint(0) >> 1)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func canonicalInteractionList(response extractionResponse, sourceID string) dnd.NPCInteractionList {
|
||||
interactions := make([]dnd.NPCInteraction, len(response.Interactions))
|
||||
for index, interaction := range response.Interactions {
|
||||
interactions[index] = dnd.NPCInteraction{
|
||||
Name: interaction.Name,
|
||||
Kind: dnd.NPCInteractionKind(interaction.Kind),
|
||||
SourceRefs: canonicalSourceRefs(interaction.SourceRefs, sourceID),
|
||||
}
|
||||
}
|
||||
if response.Interactions == nil {
|
||||
interactions = nil
|
||||
}
|
||||
return dnd.NPCInteractionList{Interactions: interactions}
|
||||
}
|
||||
|
||||
func canonicalSourceRefs(refs []interactionSourceRefResponse, sourceID string) []source.SourceRef {
|
||||
if refs == nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]source.SourceRef, len(refs))
|
||||
for index, ref := range refs {
|
||||
out[index] = source.SourceRef{SourceID: sourceID, StartUnitID: ref.StartUnitID, EndUnitID: ref.EndUnitID}
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user