OverviewFilesCommitsIssuesDocsReleasesPeers

hypercore-kb / opencog-kb / language-learning / mvp / test-lgparse2.scm

88 lines · scm ref:
; test-lgparse2.scm - Test with proper ordering
(use-modules (opencog) (opencog exec) (opencog lg))
(use-modules (opencog persist) (opencog persist-rocks))
(use-modules (opencog matrix) (opencog learn))

(display "Test LG parsing with proper ordering\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)

; Step 1: Open RocksDB and load pairs FIRST
(display "Step 1: Loading pairs 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, ~a words\n\n"
        (cog-count-atoms 'Edge)
        (length (cog-get-atoms 'WordNode)))

; Step 2: Verify we have pairs with MI values
(display "Step 2: Verify MI values exist...\n")
(define sample-word (Word "the"))
(define sample-list (car (cog-incoming-by-type sample-word 'List)))
(define sample-edge (car (cog-incoming-set sample-list)))
(format #t "Sample edge: ~a\n" sample-edge)
(format #t "Keys on edge: ~a\n" (cog-keys sample-edge))
(format #t "MI value: ~a\n\n"
        (cog-value sample-edge (Predicate "*-Mutual Info Key-*")))

; Step 3: NOW create and test dict-pair
; The LgDictNode will be fresh and use our AtomSpace
(display "Step 3: Parse with dict-pair dictionary...\n")

; Set LG verbosity high
(setenv "LG_VERBOSITY" "5")

(catch #t
  (lambda ()
    ; Create LgParseLink with AtomSpace argument
    (define parse-link
      (LgParseLink
        (Phrase "the cat sat")
        (LgDict "dict-pair")
        (Number 3)
        (cog-atomspace)
        rsn))
    (format #t "Created parse link: ~a\n" parse-link)

    ; Execute
    (define result (cog-execute! parse-link))
    (format #t "Parse result: ~a\n" result)

    ; Check if any linkages were found
    (if (null? (cog-value->list result))
        (display "No linkages found - checking why...\n")
        (format #t "Found ~a parse results!\n" (length (cog-value->list result)))))
  (lambda (key . args)
    (format #t "Error: ~a ~a\n" key args)))

; Step 4: Debug - check if LG is finding pairs
(display "\nStep 4: Debug info...\n")
(format #t "Pairs containing 'the': ~a\n"
        (length (cog-incoming-by-type (Word "the") 'List)))
(format #t "Pairs containing 'cat': ~a\n"
        (length (cog-incoming-by-type (Word "cat") 'List)))

; Check if 'the-cat' pair exists
(define the-cat (List (Word "the") (Word "cat")))
(define the-cat-edge (cog-link 'Edge (Bond "ANY") the-cat))
(if the-cat-edge
    (begin
      (format #t "\nEdge 'the-cat': ~a\n" the-cat-edge)
      (format #t "MI for 'the-cat': ~a\n"
              (cog-value the-cat-edge (Predicate "*-Mutual Info Key-*"))))
    (display "\nNo 'the-cat' edge found!\n"))

; Close storage
(cog-close rsn)
(display "\nDone.\n")