OverviewFilesCommitsIssuesDocsReleasesPeers

hypercore-kb / opencog-kb / language-learning / mvp / test-gpu-standalone.scm

99 lines · scm ref:
;; test-gpu-standalone.scm — Standalone GPU AtomSpace test (no CogServer)
;;
;; Tests the full pipeline: count → MI → sections → cosine
;; Run: guile -l test-gpu-standalone.scm

(load "gpu-atomspace.scm")

(format #t "~%=== GPU AtomSpace Standalone Test ===~%~%")

;; Init
(gpu-init!)
(gpu-status)

;; Define test sentences + MST edges (position pairs)
;; Edges = which word positions are linked by MST
(define test-sentences
  '(("the" "cat" "sat" "on" "the" "mat")
    ("a" "dog" "ran" "in" "the" "park")
    ("the" "bird" "flew" "over" "the" "tree")
    ("the" "cat" "ran" "in" "the" "park")
    ("a" "bird" "sat" "on" "a" "tree")
    ("the" "dog" "flew" "over" "the" "mat")
    ("she" "sat" "on" "the" "mat")
    ("he" "ran" "in" "the" "park")
    ("she" "flew" "over" "the" "tree")
    ("he" "sat" "on" "a" "mat")))

;; Simple MST: chain linking each word to the next (positions 0-1, 1-2, 2-3, ...)
(define test-edges
  (map (lambda (sent)
         (let ((n (length sent)))
           (let loop ((i 0) (edges '()))
             (if (>= i (- n 1))
                 (reverse edges)
                 (loop (+ i 1) (cons (cons i (+ i 1)) edges))))))
       test-sentences))

;; Process sentences (counting)
(format #t "~%--- Processing ~a sentences ---~%" (length test-sentences))
(gpu-process-sentences! test-sentences)
(gpu-status)

;; Compute MI
(format #t "~%--- Computing MI ---~%")
(gpu-compute-mi!)

;; Read top pairs
(format #t "~%--- Top pairs by MI ---~%")
(let ((pairs (gpu-read-pairs #:max-pairs 50)))
  (format #t "Got ~a pairs~%" (length pairs))
  (when (> (length pairs) 0)
    (let ((sorted (sort pairs (lambda (a b) (> (cadddr a) (cadddr b))))))
      (for-each
        (lambda (p)
          (format #t "  ~a - ~a  count=~,1f  MI=~,3f~%"
                  (car p) (cadr p) (caddr p) (cadddr p)))
        (take sorted (min 15 (length sorted)))))))

;; Extract sections from sentences + MST edges
(format #t "~%--- Extracting sections ---~%")
(gpu-extract-sections! test-sentences test-edges)
(gpu-corpus-status)

;; Cosine candidates
(format #t "~%--- Cosine candidates (threshold=0.1) ---~%")
(let ((cands (gpu-cosine-candidates #:threshold 0.1 #:max-output 100)))
  (format #t "Got ~a candidates~%" (length cands))
  (when (> (length cands) 0)
    (for-each
      (lambda (c)
        (format #t "  ~a - ~a  cosine=~,3f~%"
                (car c) (cadr c) (caddr c)))
      (take cands (min 20 (length cands))))))

;; Test class assignment + substitution
(format #t "~%--- Testing class assignment ---~%")
(gpu-assign-classes! '(("he" . 0) ("she" . 0)
                       ("the" . 1) ("a" . 1)
                       ("cat" . 2) ("dog" . 2) ("bird" . 2)))
(format #t "Assigned 3 classes~%")

(format #t "~%--- Substituting classes ---~%")
(gpu-substitute!)
(format #t "Substitution done~%")

;; Re-check cosine after substitution
(format #t "~%--- Cosine after substitution (threshold=0.1) ---~%")
(let ((cands (gpu-cosine-candidates #:threshold 0.1 #:max-output 100)))
  (format #t "Got ~a candidates~%" (length cands))
  (when (> (length cands) 0)
    (for-each
      (lambda (c)
        (format #t "  ~a - ~a  cosine=~,3f~%"
                (car c) (cadr c) (caddr c)))
      (take cands (min 20 (length cands))))))

(format #t "~%=== DONE ===~%")
(gpu-shutdown!)