OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-agents commits / 144b77a

Final step of pipeline wiring
144b77a8e29ff8d4f4103e0bc04a289a5d3e9c43
by Linas Vepštas <linasvepstas@gmail.com> · 2024-05-03T13:18:46.000Z

Changed files (1) +42 -16

examples/pair-count.scm +42 -16
……@@ -16,6 +16,7 @@
1616; 2) A debug printing utility.
1717; 3) Using filters to extract subelements of a data stream.
1818; 4) Using filters to increment counts on elements of a data stream.
19; 5) Reading text from a file.
1920;
2021(use-modules (opencog) (opencog exec) (opencog persist))
2122(use-modules (opencog nlp) (opencog nlp lg-parse))
……@@ -182,7 +183,9 @@
182183(cog-execute! demo-filter)
183184
184185; Same as above, but this time, ignore the words, and get the edges.
185; Also, apply the function FUNKY to the edge-list.
186; Write it as a pipeline component, with two arguments: a parse source
187; PASRC, so we don't have to keep working with the same sentence over
188; and over, and a function FUNKY that will be applied to the edge-list.
186189; This function is meant to be a handy place to wire in more processing,
187190;
188191; Note that the result of parsing appears to be double-wrapped in
……@@ -191,7 +194,7 @@
191194; while the inner one groups the edges in one parse. Thus, the
192195; function FUNKY will be called once per parse, and its argument
193196; will be the list of words in that parse.
194(define (edge-filter FUNKY)
197(define (edge-filter PASRC FUNKY)
195198 (Filter
196199 (Rule
197200 ; Match clause - one per parse.
……@@ -200,16 +203,18 @@
200203 (Variable "$words")
201204 (Variable "$edge-list"))
202205
203 ; Apply the functin FUNKY to the edge-list
204 (FUNKY (Variable "$edge-list"))
205 )
206 (LgParseBonds (Phrase "this is a test") (LgDict "any") (Number 1))))
206 ; Apply the function FUNKY to the edge-list
207 (FUNKY (Variable "$edge-list")))
208 PASRC))
209
210(define parse-stuff
211 (LgParseBonds (Phrase "this is a test") (LgDict "any") (Number 1)))
207212
208213; Try it! Use the debug printer as the function to call.
209(cog-execute! (edge-filter debug-prt))
214(cog-execute! (edge-filter parse-stuff debug-prt))
210215
211216; --------------------------------------------------------------
212; The above showed hoe to pull out the edges from the parse stream.
217; The above showed how to pull out the edges from the parse stream.
213218; The next goal is to increment the count on the edges, whenever they
214219; are witnessed.
215220
……@@ -244,19 +249,40 @@
244249 EDGE-LIST))
245250
246251; Run it. Watch the counts increment
247(cog-execute! (edge-filter edge-counter))
252(cog-execute! (edge-filter parse-stuff edge-counter))
248253
249
250; --------------------------------------------------------------
251; --------------------------------------------------------------
252254; --------------------------------------------------------------
253255; --------------------------------------------------------------
254; This demo starts where the `file-read.scm` demo in the sensory
255; project leaves off. See
256; To complete the pipeline, text is to be streamed from a text file.
257; The `file-read.scm` demo in the sensory project already shows the
258; this step. See
259;
256260; https://github.com/opencog/sensory/raw/master/examples/file-read.scm
261;
262; Repeated here.
257263
258
259
264; Opening the text stream will create a ValueStream. It needs to be
265; placed where the parser can find it. Anywhere will do, as long as the
266; parser attaches to it at the same place. Also: a demo text file must
267; be present at /tmp/demo.txt for this to work.
268(cog-execute!
269 (SetValue (Anchor "pipe demo") (Predicate "text src")
270 (Open (Type 'TextFileStream)
271 (Sensory "file:///tmp/demo.txt"))))
272
273; Parsing proceeds as before, with the text string replaced by the
274; text stream. In the current design, the parser does not expose a
275; stream, and so cog-execute! must be called once per line of text.
276; This may change. Streams probably really should stream.
277(define parse-stream
278 (LgParseBonds
279 (ValueOf (Anchor "pipe demo") (Predicate "text src"))
280 (LgDict "any") (Number 1)))
281
282(cog-execute! parse-stream)
283
284; That's it. Now wire it all together:
285(cog-execute! (edge-filter parse-stream edge-counter))
260286
261287; --------------------------------------------------------------
262288; Return a text parser that counts edges on a stream. The