OverviewFilesCommitsIssuesDocsReleasesPeers

atomspace-layer-discovery / experiments / ladder / port / linas_c_fresh.scm

46 lines · scm ref:
; Linas's stage C (make-observe-block WIN 12 + LgParseSections dict-pair 3) in a FRESH process, MI read from
; our pairmi dump, printing Sections in g0's format. Used only to check lgsect against Linas's own code.
; make-observe-block counts its windows in storage, so a scratch RocksStorageNode is opened.
;   guile linas_c_fresh.scm <pairmi.tsv> <text> <dict-dir> <nwindows> <scratch-rocks-dir> > out.tsv
(use-modules (opencog) (opencog exec) (opencog persist) (opencog persist-rocks) (opencog lg) (opencog learn))
(use-modules (ice-9 textual-ports) (ice-9 rdelim) (ice-9 regex))
(define args (cdr (command-line)))
(define rsn (RocksStorageNode (string-append "rocks://" (list-ref args 4))))
(cog-open rsn)
(define rx (make-regexp "^\\(EdgeLink \\(BondNode \"ANY\"\\) \\(ListLink \\(WordNode \"([^\"]*)\"\\) \\(WordNode \"([^\"]*)\"\\)\\)\\)\t\\(PredicateNode \"\\*-Mutual Info Key-\\*\"\\)\t([^ ]+) ([^ ]+)$"))
(define key (Predicate "*-Mutual Info Key-*"))
(define npairs 0)
(call-with-input-file (list-ref args 0)
	(lambda (port)
		(let loop ((line (read-line port)))
			(unless (eof-object? line)
				(let ((m (regexp-exec rx line)))
					(when m
						(cog-set-value! (Edge (Bond "ANY") (List (Word (match:substring m 1)) (Word (match:substring m 2))))
							key (FloatValue (string->number (match:substring m 3)) (string->number (match:substring m 4))))
						(set! npairs (+ npairs 1))))
				(loop (read-line port))))))
(format (current-error-port) "loaded ~A pairs\n" npairs)
(define text (call-with-input-file (list-ref args 1) get-string-all))
(define dict (LgDict (list-ref args 2)))
(define maxw (string->number (list-ref args 3)))
(define (atom->str A)
	(if (cog-subtype? 'Node (cog-type A))
		(format #f "(~A ~S)" (cog-type A) (cog-name A))
		(format #f "(~A~{ ~A~})" (cog-type A) (map atom->str (cog-outgoing-set A)))))
(define win 0)
(define (observe TXT)
	(when (< win maxw)
		(let ((R (cog-execute! (LgParseSections (Phrase TXT) dict (Number 3) (cog-atomspace)))))
			(define pi 0)
			(for-each
				(lambda (P)
					(for-each (lambda (A) (format #t "~A\t~A\t~A\n" win pi (atom->str A)))
						(append (cog-value->list (cog-value-ref P 0)) (cog-value->list (cog-value-ref P 1))))
					(set! pi (+ pi 1)))
				(cog-value->list R))
			(force-output)))
	(set! win (+ win 1)))
((make-observe-block observe #:WIN-SIZE 12) text)
(cog-close rsn)