OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-agents commits / cd80593

Start working on counting
cd8059309b9ab257f1a264709676191b13e31f98
by Linas Vepštas <linasvepstas@gmail.com> · 2024-05-03T05:05:52.000Z

Changed files (1) +54 -6

examples/pair-count.scm +54 -6
……@@ -11,6 +11,12 @@
1111; auto-generate these kinds of pipelines. This demo is meant to
1212; illustrate a non-trivial pipeline.
1313;
14; The steps proceed as follows:
15; 1) Example of using the Link Grammar parser to generate word-pairs.
16; 2) A debug printing utility.
17; 3) Using filters to extract subelements of a data stream.
18; 4) Using filters to increment counts on elements of a data stream.
19;
1420(use-modules (opencog) (opencog exec) (opencog persist))
1521(use-modules (opencog nlp) (opencog nlp lg-parse))
1622(use-modules (opencog sensory))
……@@ -168,7 +174,7 @@
168174; Extracting the desired components is easier if a printer is used for
169175; debugging. This allows the innards of the match clause to be viewed
170176; directly.
171(define word-filter
177(define demo-word-filter
172178 (Filter
173179 (Rule
174180 ; Match clause - one per parse.
……@@ -187,10 +193,12 @@
187193 (LgParseBonds (Phrase "this is a test") (LgDict "any") (Number 1))))
188194
189195; Run it.
190(cog-execute! word-filter)
196(cog-execute! demo-word-filter)
191197
192198; Same as above, but this time, ignore the words, and get the edges.
193(define edge-filter
199; Also, replace the printer function with a generic function FUNKY.
200; This is a handy place to wire in further processing.
201(define (edge-filter FUNKY)
194202 (Filter
195203 (Rule
196204 ; Match clause - one per parse.
……@@ -200,13 +208,53 @@
200208 (LinkSignature ; the edge-list
201209 (Type 'LinkValue) ; edge-list wrapper
202210 (Glob "$edge-list"))) ; all of the edges
203 ; Pipeline to apply to the resulting match.
204 (debug-prt (Glob "$edge-list"))
211 ; Apply the functin FUNKY to the edge-list
212 (FUNKY (Glob "$edge-list"))
205213 )
206214 (LgParseBonds (Phrase "this is a test") (LgDict "any") (Number 1))))
207215
208(cog-execute! edge-filter)
216; Try it!
217
218(cog-execute! (edge-filter debug-prt))
219
220; --------------------------------------------------------------
221; The above showed hoe to pull out the edges from the parse stream.
222; The next goal is to increment the count on the edges, whenever they
223; are witnessed.
224
225; A handy-dandy counter utility.
226(define (incr-cnt edge)
227 (SetValue edge (Predicate "count")
228 (Plus (Number 0 0 1)
229 (FloatValueOf edge (Predicate "count") (FloatValueOf (Number 0 0 0))))))
230
231; Try it:
232(cog-execute! (incr-cnt (Concept "foobar")))
233
234; The count is stored at the key (Predicate "count").
235; It can be viewed directly with scheme:
236(cog-value (Concept "foobar") (Predicate "count"))
237
238; ... or, equivalently, in Atomese:
239(cog-execute! (ValueOf (Concept "foobar") (Predicate "count")))
209240
241; Wire it into the earlier pipelines
242(define edge-counter
243 (Filter
244 (Rule
245 ; (TypedVariable (Variable "$edge") (Type 'Edge))
246 (LinkSignature
247 (TypeNode 'LinkValue)
248 (Glob "$edge"))
249 (debug-prt (Glob "$edge")))
250 ; (incr-cnt (Variable "$edge")))
251 edge-filter))
252
253(cog-execute! edge-counter)
254
255; --------------------------------------------------------------
256; --------------------------------------------------------------
257; --------------------------------------------------------------
210258; --------------------------------------------------------------
211259; This demo starts where the `file-read.scm` demo in the sensory
212260; project leaves off. See