OverviewFilesCommitsIssuesDocsReleasesPeers

hypercore-kb / opencog-kb / language-learning / mvp / test-fresh-dict.scm

83 lines · scm ref:
; test-fresh-dict.scm - Use fresh dictionary name to bypass caching
(use-modules (opencog) (opencog exec) (opencog lg))
(use-modules (opencog persist) (opencog persist-rocks))
(use-modules (opencog matrix) (opencog learn))

; 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)

(display "=== Fresh Dictionary Test ===\n\n")

; Step 1: Load ALL data first before touching any LgDict
(display "Step 1: Load data FIRST (before any LgDict reference)...\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 into AtomSpace\n\n" (cog-count-atoms 'Edge))

; Step 2: Check for pre-existing LgDictNodes
(display "Step 2: Check for pre-existing LgDictNodes...\n")
(define existing-dicts (cog-get-atoms 'LgDictNode))
(format #t "  Existing LgDictNodes: ~a\n\n" existing-dicts)

; Step 3: Create a FRESH dictionary with unique name
; This forces a new C++ LgDictNode object to be created
; which will then call dictionary_create_lang with current globals
(define unique-dict-name (string-append "dict-pair-" (number->string (current-time))))
(format #t "Step 3: Create fresh LgDict with unique name: ~a\n" unique-dict-name)

; But wait - we need the dict config files to match!
; The dict name must exist in LG_DICTIONARY_PATH
; Let's just use dict-pair but REMOVE it from atomspace first

(display "\nStep 3b: Remove any existing dict-pair LgDictNode...\n")
(define old-dict (cog-node 'LgDictNode "dict-pair"))
(if old-dict
    (begin
      (format #t "  Found existing LgDictNode: ~a\n" old-dict)
      (format #t "  Removing from AtomSpace...\n")
      (cog-extract! old-dict)
      (format #t "  Removed: ~a\n" (not (cog-node 'LgDictNode "dict-pair"))))
    (display "  No existing dict-pair node\n"))

; Step 4: Now create fresh LgDict
(display "\nStep 4: Create fresh LgDict 'dict-pair'...\n")
(define fresh-dict (LgDict "dict-pair"))
(format #t "  Created: ~a\n" fresh-dict)
(format #t "  Handle: ~a\n\n" (cog-handle fresh-dict))

; Step 5: Parse
(display "Step 5: Parse with fresh dictionary...\n")
(define parse-link
  (LgParseLink
    (Phrase "the cat")
    fresh-dict
    (Number 3)
    (cog-atomspace)
    rsn))
(format #t "Parse link: ~a\n\n" parse-link)

(display "Executing parse...\n")
(define result (cog-execute! parse-link))
(format #t "Result: ~a\n" result)
(format #t "Result type: ~a\n" (cog-type result))

(if (equal? (cog-type result) 'LinkValue)
    (let ((vals (cog-value->list result)))
      (format #t "Number of results: ~a\n" (length vals))
      (when (> (length vals) 0)
        (display "\nFirst parse result:\n")
        (display (car vals))
        (newline)))
    (format #t "Unexpected result type: ~a\n" result))

(cog-close rsn)
(display "\n=== Done ===\n")