OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-agents commits / 8253c12

Simplify the matching
8253c12d59de63afa48d4553a77f29f87521a887
by Linas Vepštas <linasvepstas@gmail.com> · 2024-05-03T05:32:27.000Z

Changed files (1) +25 -47

examples/pair-count.scm +25 -47
……@@ -135,7 +135,7 @@
135135
136136(define demo-filter
137137 (Filter
138 (Glob "$x")
138 (Variable "$x")
139139 (LgParseBonds (Phrase "this is a test") (LgDict "any") (Number 1))))
140140
141141; Run it.
……@@ -145,76 +145,51 @@
145145(define demo-filter
146146 (Filter
147147 (Rule
148 (Glob "$x") ; Input
149 (Glob "$x")) ; Output
148 (Variable "$x") ; Input
149 (Variable "$x")) ; Output
150150 (LgParseBonds (Phrase "this is a test") (LgDict "any") (Number 1))))
151151
152152; Run it.
153153(cog-execute! demo-filter)
154154
155155; Same as above, but now start matching stuff inside the stream.
156; This is still an almost trivial no-op; but it does unwrap one
157; layer of LinkValue.
156; Each item in the stream is a pair: a list of words, followed
157; by a list of edges. Match these two, and keep the words.
158158(define demo-filter
159159 (Filter
160160 (Rule
161161 ; Match clause - one per parse.
162162 (LinkSignature
163163 (Type 'LinkValue)
164 (Glob "$stuff"))
164 (Variable "$words")
165 (Variable "$edges"))
165166
166167 ; Output
167 (Glob "$stuff"))
168 (Variable "$words"))
168169
169170 (LgParseBonds (Phrase "this is a test") (LgDict "any") (Number 1))))
170171
171172; Run it.
172173(cog-execute! demo-filter)
173174
174; Extracting the desired components is easier if a printer is used for
175; debugging. This allows the innards of the match clause to be viewed
176; directly.
177(define demo-word-filter
178 (Filter
179 (Rule
180 ; Match clause - one per parse.
181 ; The LinkValue wraps two more: one holding the words
182 ; in the sentence, and another holding the word-pairs.
183 (LinkSignature
184 (Type 'LinkValue)
185 (LinkSignature
186 (Type 'LinkValue) ; This matches the word-list
187 (Glob "$words"))
188 (Type 'LinkValue)) ; This matches the edge-list
189
190 ; Output clause
191 (debug-prt (Glob "$words")))
192
193 (LgParseBonds (Phrase "this is a test") (LgDict "any") (Number 1))))
194
195; Run it.
196(cog-execute! demo-word-filter)
197
198175; Same as above, but this time, ignore the words, and get the edges.
199; Also, replace the printer function with a generic function FUNKY.
200; This is a handy place to wire in further processing.
176; Also, apply the function FUNKY to the edge-list. This will be a handy
177; place to wire in further processing.
201178(define (edge-filter FUNKY)
202179 (Filter
203180 (Rule
204181 ; Match clause - one per parse.
205182 (LinkSignature
206 (Type 'LinkValue) ; type of (vertex,edge) pair
207 (Type 'LinkValue) ; the word-list
208 (LinkSignature ; the edge-list
209 (Type 'LinkValue) ; edge-list wrapper
210 (Glob "$edge-list"))) ; all of the edges
183 (Type 'LinkValue)
184 (Variable "$words")
185 (Variable "$edge-list"))
186
211187 ; Apply the functin FUNKY to the edge-list
212 (FUNKY (Glob "$edge-list"))
188 (FUNKY (Variable "$edge-list"))
213189 )
214190 (LgParseBonds (Phrase "this is a test") (LgDict "any") (Number 1))))
215191
216; Try it!
217
192; Try it! Use the debug printer as the function to call.
218193(cog-execute! (edge-filter debug-prt))
219194
220195; --------------------------------------------------------------
……@@ -239,18 +214,21 @@
239214(cog-execute! (ValueOf (Concept "foobar") (Predicate "count")))
240215
241216; Wire it into the earlier pipelines
242(define edge-counter
217(define (edge-counter ITEM)
243218 (Filter
244219 (Rule
245220 ; (TypedVariable (Variable "$edge") (Type 'Edge))
246221 (LinkSignature
247 (TypeNode 'LinkValue)
248 (Glob "$edge"))
249 (debug-prt (Glob "$edge")))
222 (Type 'LinkValue)
223 (Variable "$edge")
224)
225 (debug-prt (Variable "$edge")))
226 ITEM))
227
250228 ; (incr-cnt (Variable "$edge")))
251 edge-filter))
229(cog-execute! (edge-filter edge-counter))
230(cog-execute! (edge-filter debug-prt))
252231
253(cog-execute! edge-counter)
254232
255233; --------------------------------------------------------------
256234; --------------------------------------------------------------