| … | … | @@ -1,19 +1,13 @@ |
| 1 | 1 | ; |
| 2 | 2 | ; count-agent.scm -- Agent that performs word-pair counting. |
| 3 | 3 | ; |
| 4 | | ; Prototype hand-built agent capable of counting things observed |
| 5 | | ; in the external environment. |
| 6 | | ; |
| 7 | | ; Demos counting of word-pairs observed by looking at a text file. |
| 8 | | ; The Sensory API allows easy generalization to other text sources, |
| 9 | | ; so working with files is OK for now. |
| 4 | ; Prototype hand-built agent capable of counting things observed in |
| 5 | ; the external environment. Based on the `pair-count.scm` demo in the |
| 6 | ; examples directory. |
| 10 | 7 | ; |
| 11 | 8 | ; The dataflow pipeline is hand-crafted. (The sensory API is supposed |
| 12 | 9 | ; to eventually auto-build these pipelines, but that code is not working |
| 13 | | ; yet.) You'll notice that the pipeline below is ... complicated. |
| 14 | | ; |
| 15 | | ; Most of the demo is in the `examples/pair-count.scm` file. The below |
| 16 | | ; is a condensation of that demo into it's working core. |
| 10 | ; yet.) |
| 17 | 11 | ; |
| 18 | 12 | ; The "rest of the demo", where we do something with the word pairs, |
| 19 | 13 | ; is in "generate.scm". |
| … | … | @@ -63,44 +57,41 @@ |
| 63 | 57 | (define (incr-cnt edge) |
| 64 | 58 | (SetValue edge (Predicate "count") |
| 65 | 59 | (Plus (Number 0 0 1) |
| 66 | | (FloatValueOf edge (Predicate "count") (FloatValueOf (Number 0 0 0)))))) |
| 60 | (FloatValueOf edge (Predicate "count") |
| 61 | (FloatValueOf (Number 0 0 0)))))) |
| 67 | 62 | |
| 68 | 63 | ; Given a list (an Atomese LinkValue list) of parse results, |
| 69 | 64 | ; extract the edges and increment the count on them. |
| 70 | | (define (count-edges parsed-stuff) |
| 65 | (define (edge-counter EDGE-LIST) |
| 71 | 66 | (Filter |
| 72 | 67 | (Rule |
| 73 | | ; (Variable "$edge") |
| 74 | 68 | (TypedVariable (Variable "$edge") (Type 'Edge)) |
| 75 | 69 | (Variable "$edge") |
| 76 | 70 | (incr-cnt (Variable "$edge"))) |
| 77 | | parsed-stuff)) |
| 78 | | |
| 79 | | ; Parse text in a private space. |
| 80 | | (define (parseli phrali) |
| 81 | | (PureExecLink (LgParseBonds phrali DICT NUML))) |
| 71 | EDGE-LIST)) |
| 82 | 72 | |
| 83 | | ; Given an Atom `phrali` holding text to be parsed, get that text, |
| 84 | | ; parse it, and increment the counts on the edges. |
| 85 | | (define (filty phrali) |
| 73 | ; Given PASRC holding a stream of parses, filter out the edges, |
| 74 | ; and then applu function FUNKY to each edge. |
| 75 | (define (edge-filter PASRC FUNKY) |
| 86 | 76 | (Filter |
| 87 | 77 | (Rule |
| 88 | | ; Type decl |
| 89 | | (Glob "$x") |
| 90 | | ; Match clause - one per parse. |
| 91 | 78 | (LinkSignature |
| 92 | | (Type 'LinkValue) ; the wrapper for the pair |
| 93 | | (Type 'LinkValue) ; the word-list |
| 94 | | (LinkSignature ; the edge-list |
| 95 | | (Type 'LinkValue) ; edge-list wrapper |
| 96 | | (Glob "$edge-list"))) ; all of the edges |
| 97 | | ; Pipeline to apply to the resulting match. |
| 98 | | (count-edges (Glob "$edge-list")) |
| 99 | | ) |
| 100 | | (parseli phrali))) |
| 79 | (Type 'LinkValue) |
| 80 | (Variable "$words") |
| 81 | (Variable "$edge-list")) |
| 82 | ; Apply the function FUNKY to the edge-list |
| 83 | (FUNKY (Variable "$edge-list"))) |
| 84 | PASRC)) |
| 85 | |
| 86 | ; Parse text in a private space. |
| 87 | (define (priv-parse TXT-SRC) |
| 88 | (PureExecLink (LgParseBonds TXT-SRC DICT NUML))) |
| 89 | |
| 90 | ; I dunno. Is private parsing needed? |
| 91 | (define (regular-parse TXT-SRC) (LgParseBonds TXT-SRC DICT NUML)) |
| 101 | 92 | |
| 102 | 93 | ; Return the parser. |
| 103 | | (filty txt-stream) |
| 94 | (edge-filter edge-counter (regular-parse txt-stream)) |
| 104 | 95 | ) |
| 105 | 96 | |
| 106 | 97 | ; -------------------------------------------------------------- |
| … | … | @@ -108,14 +99,15 @@ |
| 108 | 99 | (define (obs-file FILE-NAME) |
| 109 | 100 | |
| 110 | 101 | ; We don't need to create this over and over; once is enough. |
| 111 | | (define txt-stream (ValueOf (Concept "foo") (Predicate "some place"))) |
| 102 | (define txt-stream |
| 103 | (ValueOf (Anchor "parse pipe") (Predicate "text src"))) |
| 112 | 104 | (define parser (make-parser txt-stream)) |
| 113 | 105 | |
| 114 | 106 | (define phrali (Open (Type 'TextFileStream) |
| 115 | 107 | (Sensory (string-append "file://" FILE-NAME)))) |
| 116 | 108 | |
| 117 | 109 | (cog-execute! |
| 118 | | (SetValue (Concept "foo") (Predicate "some place") phrali)) |
| 110 | (SetValue (Anchor "parse pipe") (Predicate "text src") phrali)) |
| 119 | 111 | |
| 120 | 112 | ; Parse only first line of file: |
| 121 | 113 | ; (cog-execute! parser) |