| … | … | @@ -65,21 +65,25 @@ |
| 65 | 65 | (define (atom-counter ATOM-LIST) |
| 66 | 66 | (Filter |
| 67 | 67 | (Rule |
| 68 | | (Variable "$edge") |
| 69 | | (Variable "$edge") |
| 70 | | (incr-cnt (Variable "$edge"))) |
| 68 | ; We could type for safety, but seems like no need... |
| 69 | ; (TypedVariable (Variable "$atom") |
| 70 | ; (TypeChoice (Type 'Edge) (Type 'Word))) |
| 71 | (Variable "$atom") ; vardecl |
| 72 | (Variable "$atom") ; body to match |
| 73 | (incr-cnt (Variable "$atom"))) |
| 71 | 74 | ATOM-LIST)) |
| 72 | 75 | |
| 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) |
| 76 | ; Given PASRC holding a stream of parses, split it into a list of |
| 77 | ; words, and a list of edges, and apply FUNKY to both lists. |
| 78 | (define (stream-splitter PASRC FUNKY) |
| 76 | 79 | (Filter |
| 77 | 80 | (Rule |
| 78 | 81 | (LinkSignature |
| 79 | 82 | (Type 'LinkValue) |
| 80 | | (Variable "$words") |
| 83 | (Variable "$word-list") |
| 81 | 84 | (Variable "$edge-list")) |
| 82 | 85 | ; Apply the function FUNKY to the edge-list |
| 86 | (FUNKY (Variable "$word-list")) |
| 83 | 87 | (FUNKY (Variable "$edge-list"))) |
| 84 | 88 | PASRC)) |
| 85 | 89 | |
| … | … | @@ -88,8 +92,10 @@ |
| 88 | 92 | ; Is there any benefit to private parsing? |
| 89 | 93 | ; (edge-filter (PureExecLink parser) edge-counter) |
| 90 | 94 | |
| 91 | | ; Return the parser. |
| 92 | | (edge-filter parser atom-counter) |
| 95 | ; Return the assembled counting pipeline. |
| 96 | ; All that the user needs to do is to call `cog-execute!` on it, |
| 97 | ; until end of file is reached. |
| 98 | (stream-splitter parser atom-counter) |
| 93 | 99 | ) |
| 94 | 100 | |
| 95 | 101 | ; -------------------------------------------------------------- |
| … | … | @@ -113,7 +119,7 @@ |
| 113 | 119 | ; Loop over all lines. |
| 114 | 120 | (define (looper) (cog-execute! parser) (looper)) |
| 115 | 121 | |
| 116 | | ; At end of file, exception will be thrown. Catch it. |
| 122 | ; At end of file, exception will be thrown. Catch it. Print it. |
| 117 | 123 | (catch #t looper |
| 118 | 124 | (lambda (key . args) (format #t "The end ~A\n" key))) |
| 119 | 125 | |
| … | … | @@ -127,6 +133,9 @@ |
| 127 | 133 | ; Look at results |
| 128 | 134 | ; (cog-report-counts) |
| 129 | 135 | ; (cog-execute! (ValueOf (Word "is") (Predicate "count"))) |
| 136 | ; (cog-execute! (ValueOf |
| 137 | ; (Edge (Bond "ANY") (List (Word "is") (Word "a"))) |
| 138 | ; (Predicate "count"))) |
| 130 | 139 | ; |
| 131 | 140 | ; Erase all words, so we can try again. |
| 132 | 141 | ; (extract-type 'WordNode) |