OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-agents commits / 9b4a197

More work on the demo
9b4a1978f2cf298422fad632e63d04761b15c891
by Linas Vepštas <linasvepstas@gmail.com> · 2024-04-16T23:33:55.000Z

Changed files (1) +40 -18

scm/pair-agent/count-agent.scm +40 -18
……@@ -9,11 +9,14 @@
99; This source blocks if there's nothing to be read; else it
1010; returns the next PhraseNode. The SRFI's call this a generator.
1111; For now, see `sensory.scm` module and example.
12; It uses a FileReadNode to loop voer a text file.
1213; 2) (Optional) Some way to split one source into multiple sources.
1314; Maybe this:
1415; https://wiki.opencog.org/w/PromiseLink#Multiplex_Example
1516; but what happens if the readers don't both read ???
1617; 3) A Filter that takes above and increments pair counts.
18; Done. Its non-atomic, but so what. Perhaps we can live with
19; that, given that sampliing is statistical, anyway.
1720; 4) Execution control. There are two choices:
1821; Pull: infinite loop polls on promise. Source blocks if
1922; no input.
……@@ -22,22 +25,32 @@
2225; Right now, the general infrastructure supports Pull naturally.
2326; There aren't any push demos.
2427; Attention control is easier with Pull.
28;
29; The below demos this design.
30;
31; Open issues:
32; *) Need to call cog-execute! in a loop to loop over whole file.
33; Would be nicer to not have to do this.
34; *) End-of-file results in a throw from parser. This is a side
35; effect. We need some proper end-of-file handling.
2536
26
27(define text-blob "this is just a bunch of words you know")
28
29
37; --------------------------------------------------------------
3038; Return a text parser that counts edges on a stream. The
3139; `txt-stream` must be an Atom that can serve as a source of text.
3240; Typically, `txt-stream` will be
3341; (ValueOf (Concept "some atom") (Predicate "some key"))
3442; and the Value there will be a LinkStream from some file or
3543; other text source.
44;
45; These sets up a processing pipeline in Atomese, and returns that
46; pipeline. The actual parsing all happens in C++ code, not in scheme
47; code. The scheme here is just to glue the pipeline together.
3648(define (make-parser txt-stream)
37 ; From inside to out:
38 ; LGParseBonds tokenizes a sentence, and then parses it.
39 ; The PureExecLink makes sure that the parsing is done in a sub-AtomSpace
40 ; so that the main AtomSpace is not garbaged up.
49 ;
50 ; Pipeline steps, from inside to out:
51 ; * LGParseBonds tokenizes a sentence, and then parses it.
52 ; * The PureExecLink makes sure that the parsing is done in a
53 ; sub-AtomSpace so that the main AtomSpace is not garbaged up.
4154 ;
4255 ; The result of parsing is a list of pairs. First item in a pair is
4356 ; the list of words in the sentence; the second is a list of the edges.
……@@ -46,8 +59,13 @@
4659 ; (LinkValue (Word "this") (Word "is") (Word "a") (Word "test"))
4760 ; (LinkValue (Edge ...) (Edge ...) ...))
4861 ;
49 ; The Filter matches this, so that (Variable "$x") is equated with the
50 ; list of Edges.
62 ; The outer Filter matches this, so that (Glob "$edge-list") is
63 ; set to the LinkValue of Edges.
64 ;
65 ; The inner Filter loops over the list of edges, and invokes a small
66 ; pipe to increment the count on each edge.
67 ;
68 ; The counter is a non-atomic pipe of (SetValue (Plus 1 (GetValue)))
5169 ;
5270 (define NUML (Number 4))
5371 (define DICT (LgDict "any"))
……@@ -86,9 +104,9 @@
86104 (Type 'LinkValue) ; the word-list
87105 (LinkSignature ; the edge-list
88106 (Type 'LinkValue) ; edge-list wrapper
89 (Glob "$x"))) ; all of the edges
90 ; Rewrite
91 (count-edges (Glob "$x"))
107 (Glob "$edge-list"))) ; all of the edges
108 ; Pipeline to apply to the resulting match.
109 (count-edges (Glob "$edge-list"))
92110 )
93111 (parseli phrali)))
94112
……@@ -96,8 +114,10 @@
96114 (filty txt-stream)
97115)
98116
99; Parse a string.
117; Demo wrapper: Parse a string.
100118(define (obs-txt PLAIN-TEXT)
119
120 ; We don't need to create this over and over; once is enough.
101121 (define txt-stream (ValueOf (Concept "foo") (Predicate "some place")))
102122 (define parser (make-parser txt-stream))
103123
……@@ -110,8 +130,10 @@
110130 (cog-extract-recursive! phrali)
111131)
112132
113; Parse contents of a file.
133; Demo wrapper: Parse contents of a file.
114134(define (obs-file FILE-NAME)
135
136 ; We don't need to create this over and over; once is enough.
115137 (define txt-stream (ValueOf (Concept "foo") (Predicate "some place")))
116138 (define parser (make-parser txt-stream))
117139
……@@ -148,7 +170,7 @@
148170; risks deadlock.
149171;
150172; cog-update-value calls
151; asp->increment_count(h, key, fvp->value()));
173; Ideally, call asp->increment_count(h, key, fvp->value()));
152174
153175; Below works but is a non-atomic increment.
154176(define ed (Edge (Bond "ANY") (List (Word "words") (Word "know"))))
……@@ -158,7 +180,7 @@
158180 (Plus (Number 0 0 1)
159181 (FloatValueOf ed (Predicate "count") (Number 0 0 0))))))
160182
161; Lets work on file-access now.
183; Here's a demo of what file-access looks like.
162184(cog-execute!
163185 (SetValue (Concept "foo") (Predicate "some place")
164186 (FileRead "file:///tmp/demo.txt")))
……@@ -170,7 +192,7 @@
170192; (cog-execute! (LgParseBonds txt-stream-gen DICT NUML))
171193
172194; (load "count-agent.scm")
173; (obs-txt text-blob)
195; (obs-txt "Some kind of sentence to process, hell yeah!")
174196; (obs-file "/tmp/demo.txt")
175197; (cog-report-counts)
176198; (cog-get-atoms 'WordNode)