| … | … | @@ -16,6 +16,7 @@ |
| 16 | 16 | ; 2) A debug printing utility. |
| 17 | 17 | ; 3) Using filters to extract subelements of a data stream. |
| 18 | 18 | ; 4) Using filters to increment counts on elements of a data stream. |
| 19 | ; 5) Reading text from a file. |
| 19 | 20 | ; |
| 20 | 21 | (use-modules (opencog) (opencog exec) (opencog persist)) |
| 21 | 22 | (use-modules (opencog nlp) (opencog nlp lg-parse)) |
| … | … | @@ -182,7 +183,9 @@ |
| 182 | 183 | (cog-execute! demo-filter) |
| 183 | 184 | |
| 184 | 185 | ; 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. |
| 186 | 189 | ; This function is meant to be a handy place to wire in more processing, |
| 187 | 190 | ; |
| 188 | 191 | ; Note that the result of parsing appears to be double-wrapped in |
| … | … | @@ -191,7 +194,7 @@ |
| 191 | 194 | ; while the inner one groups the edges in one parse. Thus, the |
| 192 | 195 | ; function FUNKY will be called once per parse, and its argument |
| 193 | 196 | ; will be the list of words in that parse. |
| 194 | | (define (edge-filter FUNKY) |
| 197 | (define (edge-filter PASRC FUNKY) |
| 195 | 198 | (Filter |
| 196 | 199 | (Rule |
| 197 | 200 | ; Match clause - one per parse. |
| … | … | @@ -200,16 +203,18 @@ |
| 200 | 203 | (Variable "$words") |
| 201 | 204 | (Variable "$edge-list")) |
| 202 | 205 | |
| 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))) |
| 207 | 212 | |
| 208 | 213 | ; 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)) |
| 210 | 215 | |
| 211 | 216 | ; -------------------------------------------------------------- |
| 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. |
| 213 | 218 | ; The next goal is to increment the count on the edges, whenever they |
| 214 | 219 | ; are witnessed. |
| 215 | 220 | |
| … | … | @@ -244,19 +249,40 @@ |
| 244 | 249 | EDGE-LIST)) |
| 245 | 250 | |
| 246 | 251 | ; Run it. Watch the counts increment |
| 247 | | (cog-execute! (edge-filter edge-counter)) |
| 252 | (cog-execute! (edge-filter parse-stuff edge-counter)) |
| 248 | 253 | |
| 249 | | |
| 250 | | ; -------------------------------------------------------------- |
| 251 | | ; -------------------------------------------------------------- |
| 252 | 254 | ; -------------------------------------------------------------- |
| 253 | 255 | ; -------------------------------------------------------------- |
| 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 | ; |
| 256 | 260 | ; https://github.com/opencog/sensory/raw/master/examples/file-read.scm |
| 261 | ; |
| 262 | ; Repeated here. |
| 257 | 263 | |
| 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)) |
| 260 | 286 | |
| 261 | 287 | ; -------------------------------------------------------------- |
| 262 | 288 | ; Return a text parser that counts edges on a stream. The |