| … | … | @@ -18,9 +18,9 @@ |
| 18 | 18 | (use-modules (srfi srfi-1)) |
| 19 | 19 | |
| 20 | 20 | ; -------------------------------------------------------------- |
| 21 | | ; Return a text parser that counts edges on a stream. The |
| 22 | | ; `txt-stream` must be an Atom that can serve as a source of text. |
| 23 | | ; Typically, `txt-stream` will be |
| 21 | ; Return a text parser that counts words and word-pairs obtained from |
| 22 | ; parsing text on a stream. The `txt-stream` must be an Atom that can |
| 23 | ; serve as a source of text. Typically, `txt-stream` will be |
| 24 | 24 | ; (ValueOf (Concept "some atom") (Predicate "some key")) |
| 25 | 25 | ; and the Value there will be a LinkStream from some file or |
| 26 | 26 | ; other text source. |
| … | … | @@ -28,6 +28,8 @@ |
| 28 | 28 | ; These sets up a processing pipeline in Atomese, and returns that |
| 29 | 29 | ; pipeline. The actual parsing all happens in C++ code, not in scheme |
| 30 | 30 | ; code. The scheme here is just to glue the pipeline together. |
| 31 | ; |
| 32 | ; This is attempting to be backwards-compat with old pair-counting code. |
| 31 | 33 | (define (make-parser txt-stream) |
| 32 | 34 | ; |
| 33 | 35 | ; Pipeline steps, from inside to out: |
| … | … | @@ -50,8 +52,9 @@ |
| 50 | 52 | ; |
| 51 | 53 | ; The counter is a non-atomic pipe of (SetValue (Plus 1 (GetValue))) |
| 52 | 54 | ; |
| 53 | | (define NUML (Number 4)) |
| 55 | (define NUML (Number 6)) |
| 54 | 56 | (define DICT (LgDict "any")) |
| 57 | (define any-parse (ParseNode "ANY")) |
| 55 | 58 | |
| 56 | 59 | ; Increment the count on one atom. |
| 57 | 60 | (define (incr-cnt atom) |
| … | … | @@ -82,9 +85,11 @@ |
| 82 | 85 | (Type 'LinkValue) |
| 83 | 86 | (Variable "$word-list") |
| 84 | 87 | (Variable "$edge-list")) |
| 85 | | ; Apply the function FUNKY to the edge-list |
| 88 | ; Apply the function FUNKY to the word and edge lists. |
| 86 | 89 | (FUNKY (Variable "$word-list")) |
| 87 | | (FUNKY (Variable "$edge-list"))) |
| 90 | (FUNKY (Variable "$edge-list")) |
| 91 | ; Increment by one for each parse |
| 92 | (incr-cnt any-parse)) |
| 88 | 93 | PASRC)) |
| 89 | 94 | |
| 90 | 95 | (define parser (LgParseBonds txt-stream DICT NUML)) |
| … | … | @@ -98,6 +103,28 @@ |
| 98 | 103 | (stream-splitter parser atom-counter) |
| 99 | 104 | ) |
| 100 | 105 | |
| 106 | ; -------------------------------------------------------------- |
| 107 | ; Demo wrapper: Parse a single text string. |
| 108 | (define (obs-texty TXT-STRING) |
| 109 | |
| 110 | ; We don't need to create this over and over; once is enough. |
| 111 | (define txt-stream |
| 112 | (ValueOf (Anchor "parse pipe") (Predicate "text src"))) |
| 113 | (define parser (make-parser txt-stream)) |
| 114 | |
| 115 | (define phrali (Item TXT-STRING)) |
| 116 | (cog-execute! |
| 117 | (SetValue (Anchor "parse pipe") (Predicate "text src") phrali)) |
| 118 | |
| 119 | ; Run parser once. |
| 120 | (cog-execute! parser) |
| 121 | (cog-extract-recursive! phrali) |
| 122 | ) |
| 123 | |
| 124 | ; Example: |
| 125 | ; (obs-texty "this is a test") |
| 126 | ; (cog-execute! (ValueOf (ParseNode "ANY") (Predicate "count")) |
| 127 | |
| 101 | 128 | ; -------------------------------------------------------------- |
| 102 | 129 | ; Demo wrapper: Parse contents of a file. |
| 103 | 130 | (define (obs-file FILE-NAME) |