OverviewFilesCommitsIssuesDocsReleasesPeers

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

75 lines · scm ref:
; test-dict-timing.scm - Trace the exact timing of dictionary creation
(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 "=== Dictionary Timing Test ===\n\n")

; DON'T load data yet - just check if dictionary exists
(display "Step 1: Check for any LgDictNode BEFORE loading data...\n")
(define pre-dicts (cog-get-atoms 'LgDictNode))
(format #t "  Pre-existing dicts: ~a\n\n" pre-dicts)

; Now construct the LgParseLink atom (but don't execute it yet)
; This might cause dictionary creation!
(display "Step 2: Construct LgParseLink WITHOUT loading data...\n")
(display "  (This may trigger dictionary creation via LgDict)\n")

; Open storage but don't fetch pairs yet
(define rsn (RocksStorageNode "rocks://data/pairs.rdb"))
(cog-open rsn)

; Just create the parse link structure
(define parse-link-early
  (LgParseLink
    (Phrase "the cat")
    (LgDict "dict-pair")
    (Number 3)
    (cog-atomspace)
    rsn))
(format #t "  Parse link created: ~a\n\n" parse-link-early)

; Check if dictionary was created
(define mid-dicts (cog-get-atoms 'LgDictNode))
(format #t "  LgDictNodes now: ~a\n\n" mid-dicts)

; NOW load data
(display "Step 3: Load data (AFTER LgParseLink construction)...\n")
(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))

; Try to execute the pre-existing parse link
(display "Step 4: Execute the EARLY parse link...\n")
(define result-early (cog-execute! parse-link-early))
(format #t "  Result: ~a\n\n" result-early)

; Now create a NEW parse link (dictionary already exists, but data is loaded)
(display "Step 5: Create and execute NEW parse link (after data load)...\n")
(define parse-link-late
  (LgParseLink
    (Phrase "the cat")
    (LgDict "dict-pair")  ; Same dict - cached!
    (Number 3)
    (cog-atomspace)
    rsn))
(define result-late (cog-execute! parse-link-late))
(format #t "  Result: ~a\n\n" result-late)

; Check: Are both parse links the same atom?
(display "=== Analysis ===\n")
(format #t "Early parse link handle: ~a\n" (cog-handle parse-link-early))
(format #t "Late parse link handle: ~a\n" (cog-handle parse-link-late))
(format #t "Same atom? ~a\n" (equal? parse-link-early parse-link-late))

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