From 4f9c53cdf6c077f8b80318c3e7dd88046dffbafa Mon Sep 17 00:00:00 2001 From: salmonumbrella <182032677+salmonumbrella@users.noreply.github.com> Date: Sun, 15 Feb 2026 00:10:43 -0800 Subject: [PATCH] Fix discourse complement direction overlap bug (#292) --- .../registerDiscourseDatalogTranslators.ts | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/utils/registerDiscourseDatalogTranslators.ts b/src/utils/registerDiscourseDatalogTranslators.ts index c079d8f9..e2ddc0f1 100644 --- a/src/utils/registerDiscourseDatalogTranslators.ts +++ b/src/utils/registerDiscourseDatalogTranslators.ts @@ -221,26 +221,33 @@ const registerDiscourseDatalogTranslators = () => { registerDatalogTranslator({ key: label, callback: ({ source, target, uid }) => { - const filteredRelations = discourseRelations - .map((r) => - (r.label === label || ANY_RELATION_REGEX.test(label)) && - doesDiscourseRelationMatchCondition(r, { source, target }) - ? { ...r, forward: true } - : doesDiscourseRelationMatchCondition( - { source: r.destination, destination: r.source }, - { source, target } - ) && - (r.complement === label || ANY_RELATION_REGEX.test(label)) - ? { ...r, forward: false } - : undefined - ) - .filter( - ( - r - ): r is ReturnType[number] & { - forward: boolean; - } => !!r - ); + const isAnyRelation = ANY_RELATION_REGEX.test(label); + const filteredRelations = discourseRelations.flatMap((r) => { + const forwardMatches = + (r.label === label || isAnyRelation) && + doesDiscourseRelationMatchCondition(r, { source, target }); + const backwardMatches = + (r.complement === label || isAnyRelation) && + doesDiscourseRelationMatchCondition( + { source: r.destination, destination: r.source }, + { source, target } + ); + if (forwardMatches && backwardMatches) { + const sourceDestinationOverlap = + r.source === r.destination || + r.source === "*" || + r.destination === "*"; + if (r.label === r.complement && sourceDestinationOverlap) { + return [ + { ...r, forward: true }, + { ...r, forward: false }, + ] as const; + } + } + if (forwardMatches) return [{ ...r, forward: true }] as const; + if (backwardMatches) return [{ ...r, forward: false }] as const; + return []; + }); if (!filteredRelations.length) return []; const andParts = filteredRelations.map( ({