| … | … | @@ -11,6 +11,12 @@ |
| 11 | 11 | ; auto-generate these kinds of pipelines. This demo is meant to |
| 12 | 12 | ; illustrate a non-trivial pipeline. |
| 13 | 13 | ; |
| 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 | ; |
| 14 | 20 | (use-modules (opencog) (opencog exec) (opencog persist)) |
| 15 | 21 | (use-modules (opencog nlp) (opencog nlp lg-parse)) |
| 16 | 22 | (use-modules (opencog sensory)) |
| … | … | @@ -168,7 +174,7 @@ |
| 168 | 174 | ; Extracting the desired components is easier if a printer is used for |
| 169 | 175 | ; debugging. This allows the innards of the match clause to be viewed |
| 170 | 176 | ; directly. |
| 171 | | (define word-filter |
| 177 | (define demo-word-filter |
| 172 | 178 | (Filter |
| 173 | 179 | (Rule |
| 174 | 180 | ; Match clause - one per parse. |
| … | … | @@ -187,10 +193,12 @@ |
| 187 | 193 | (LgParseBonds (Phrase "this is a test") (LgDict "any") (Number 1)))) |
| 188 | 194 | |
| 189 | 195 | ; Run it. |
| 190 | | (cog-execute! word-filter) |
| 196 | (cog-execute! demo-word-filter) |
| 191 | 197 | |
| 192 | 198 | ; 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) |
| 194 | 202 | (Filter |
| 195 | 203 | (Rule |
| 196 | 204 | ; Match clause - one per parse. |
| … | … | @@ -200,13 +208,53 @@ |
| 200 | 208 | (LinkSignature ; the edge-list |
| 201 | 209 | (Type 'LinkValue) ; edge-list wrapper |
| 202 | 210 | (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")) |
| 205 | 213 | ) |
| 206 | 214 | (LgParseBonds (Phrase "this is a test") (LgDict "any") (Number 1)))) |
| 207 | 215 | |
| 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"))) |
| 209 | 240 | |
| 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 | ; -------------------------------------------------------------- |
| 210 | 258 | ; -------------------------------------------------------------- |
| 211 | 259 | ; This demo starts where the `file-read.scm` demo in the sensory |
| 212 | 260 | ; project leaves off. See |