; test-pair-lookup.scm - Mimic C++ pair lookup logic exactly
;
; The C++ code in word-pairs.cc does:
; 1. germ = asp->add_node(WORD_NODE, s)
; 2. rprs = germ->getIncomingSetByType(LIST_LINK)
; 3. For each rawpr in rprs:
; evpr = asp->get_link(EDGE_LINK, hpr, rawpr)
; if evpr exists, check MI and return true
;
; Let's replicate this in Scheme.
(use-modules (opencog) (opencog exec) (opencog lg))
(use-modules (opencog persist) (opencog persist-rocks))
(use-modules (opencog matrix) (opencog learn))
(use-modules (srfi srfi-1)) ; for take
(display "=== Pair Lookup Debug Test ===\n\n")
; Type aliases
(define (ParseNode name) (Anchor (string-append "parse:" name)))
(define (SentenceNode name) (Anchor (string-append "sentence:" name)))
(module-define! (resolve-module '(opencog lg)) 'ParseNode ParseNode)
(module-define! (resolve-module '(opencog lg)) 'SentenceNode SentenceNode)
; Load data
(display "Loading data from RocksDB...\n")
(define rsn (RocksStorageNode "rocks://data/pairs.rdb"))
(cog-open rsn)
(define ala (make-any-link-api))
(define aca (add-count-api ala))
(define pair-stars (add-pair-stars aca))
(pair-stars 'fetch-pairs)
(format #t "Loaded: ~a edges\n\n" (cog-count-atoms 'Edge))
; === Mimic C++ pair lookup ===
(display "=== Mimicking C++ pair lookup ===\n\n")
; Step 1: Get the word (like asp->add_node)
(define test-word "the")
(display "Step 1: Get word atom\n")
(define germ (Word test-word))
(format #t " germ = ~a\n" germ)
(format #t " germ handle = ~a\n\n" (cog-handle germ))
; Step 2: Get incoming Lists (like getIncomingSetByType)
(display "Step 2: Get incoming LIST_LINK atoms\n")
(define rprs (cog-incoming-by-type germ 'List))
(format #t " Found ~a List atoms containing '~a'\n" (length rprs) test-word)
(when (> (length rprs) 0)
(format #t " First few: ~a\n\n" (take rprs (min 3 (length rprs)))))
; Step 3: For each List, try to get the Edge
(display "Step 3: Check Edge lookup for each List\n")
(define hpr (Bond "ANY"))
(format #t " hpr (pair-predicate) = ~a\n" hpr)
(format #t " hpr handle = ~a\n\n" (cog-handle hpr))
(define found-count 0)
(define not-found-count 0)
(define mi-values '())
(for-each
(lambda (rawpr)
; This is what C++ does: asp->get_link(EDGE_LINK, hpr, rawpr)
(define evpr (cog-link 'Edge hpr rawpr))
(if evpr
(begin
(set! found-count (+ found-count 1))
; Get MI value
(let ((mi-val (cog-value evpr (Predicate "*-Mutual Info Key-*"))))
(when (and mi-val (< (length mi-values) 5))
(set! mi-values (cons (list rawpr mi-val) mi-values)))))
(set! not-found-count (+ not-found-count 1))))
rprs)
(format #t "Results:\n")
(format #t " Edges FOUND: ~a\n" found-count)
(format #t " Edges NOT found: ~a\n\n" not-found-count)
(display "Sample MI values:\n")
(for-each
(lambda (item)
(format #t " ~a -> ~a\n" (car item) (cadr item)))
mi-values)
; Step 4: Check the specific lookup that C++ would do
(display "\n=== Direct Edge Lookup Test ===\n")
(define test-list (List (Word "the") (Word "cat")))
(format #t "Looking for: (Edge (Bond \"ANY\") ~a)\n" test-list)
(define direct-edge (cog-link 'Edge (Bond "ANY") test-list))
(if direct-edge
(begin
(format #t "FOUND: ~a\n" direct-edge)
(format #t "MI: ~a\n" (cog-value direct-edge (Predicate "*-Mutual Info Key-*"))))
(display "NOT FOUND!\n"))
; Step 5: Check if Edge type is correct
(display "\n=== Edge Type Verification ===\n")
(define sample-edge (car (cog-get-atoms 'Edge)))
(format #t "Sample edge from AtomSpace: ~a\n" sample-edge)
(format #t "Edge type: ~a\n" (cog-type sample-edge))
(format #t "Edge outgoing: ~a\n" (cog-outgoing-set sample-edge))
; Check if the predicate matches
(define edge-pred (car (cog-outgoing-set sample-edge)))
(format #t "\nEdge predicate: ~a\n" edge-pred)
(format #t "Edge predicate type: ~a\n" (cog-type edge-pred))
(format #t "Is it Bond? ~a\n" (equal? (cog-type edge-pred) 'BondNode))
(format #t "Bond name: ~a\n" (cog-name edge-pred))
; Compare with hpr
(format #t "\nhpr from config: ~a\n" hpr)
(format #t "Are they equal? ~a\n" (equal? edge-pred hpr))
(format #t "Same handle? ~a\n" (= (cog-handle edge-pred) (cog-handle hpr)))
(cog-close rsn)
(display "\n=== Done ===\n")