-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile-read.scm
144 lines (125 loc) · 5.09 KB
/
file-read.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
;
; file-read.scm -- file-reading demo
;
; Demo opening a file and streaming the contents into a processing
; pipeline.
;
(use-modules (opencog) (opencog exec) (opencog sensory))
; Before running this demo, copy `demo.txt` to the /tmp directory.
; This is a text file, it will be read and processed in this demo.
; --------------------------------------------------------
; Basic demo: Open a file for reading, at a fixed absolute location
; in the filesystem. Executing the OpenLink will return a stream
; value that will deliver text strings, one line at a time.
(define txt-stream
(cog-execute!
(Open
(Type 'TextFileStream)
(SensoryNode "file:///tmp/demo.txt"))))
; Repeated references to the stream will return single lines from
; the file.
txt-stream
txt-stream
txt-stream
txt-stream
txt-stream
; Eventually, this will return an empty stream. This denotes end-of-file.
; --------------------------------------------------------
; Demo: Perform indirect streaming. The file-stream will be placed
; as a Value on some Atom, where it can be accessed and processed.
;
; Open the file, get the stream, and place it somewhere.
(cog-set-value! (Concept "foo") (Predicate "some place")
(cog-execute!
(Open (Type 'TextFileStream)
(Sensory "file:///tmp/demo.txt"))))
; A better, all-Atomese version of the above. Note that the SetValueLink
; will execute the TextFileNode, grab whatever it gets from that exec,
; and then places it at the indicated location.
(cog-execute!
(SetValue (Concept "foo") (Predicate "some place")
(Open (Type 'TextFileStream)
(Sensory "file:///tmp/demo.txt"))))
; Define an executable node that will feed the stream of text.
; The ValueOf Atom is a kind of promise about the future: when
; it is executed, it will return the Value, whatever it is, at
; that time (at the time when the executiion is done).
(define txt-stream-gen
(ValueOf (Concept "foo") (Predicate "some place")))
; Access the file contents. Each time this is executed, it gets the
; next line in the file.
(cog-execute! txt-stream-gen)
(cog-execute! txt-stream-gen)
(cog-execute! txt-stream-gen)
(cog-execute! txt-stream-gen)
(cog-execute! txt-stream-gen)
; --------------------------------------------------------
; Demo: Perform processing on the stream. For each line of the
; input file, apply a rule to rewrite it into a different form.
; This rule just makes two copies of each input line, interleaving
; it with other text. It is built with the same ValueOf promise, as
; above.
(define rule-applier
; When a filter is executed, it applies the first argument
; to the second. The first argument here is a Rule, the
; second is the text file stream.
(Filter
; A rewrite rule has three parts: a variable declaration,
; a pattern to match, and the rewrite to apply. Here, the
; pattern match is trivial: `(Variable "$x")` matches
; everything, the entire body of the input, which will be
; a line from the text file. More pecisely, and ItemNode
; holding that line. The rewrite below is just some
; silliness that makes two copies of the input.
;
; The LinkSignatureLink is a constructor: it creates either
; a LinkValue or a Link of the specified type. In this demo,
; just using a plain-old List (instead of the LinkSignature)
; would have been OK. But if the Variable had been a Value,
; then using the LinkSignature would have been required.
(Rule
(TypedVariable (Variable "$x") (Type 'ItemNode))
(Variable "$x")
(LinkSignature ; Or use List here and skip next line.
(Type 'LinkValue)
(Variable "$x")
(Item "yo the first\n")
(Variable "$x")
(Item "yo the second\n")
(Item "====\n")))
txt-stream-gen))
; The previous demo ran the input file to end-of-file; we need to
; restart at the beginning.
(cog-execute!
(SetValue (Concept "foo") (Predicate "some place")
(Open (Type 'TextFileStream)
(Sensory "file:///tmp/demo.txt"))))
; Run the rule, once.
(cog-execute! rule-applier)
; Repeat it again, over and over, till the end-of-file.
(cog-execute! rule-applier)
(cog-execute! rule-applier)
(cog-execute! rule-applier)
(cog-execute! rule-applier)
(cog-execute! rule-applier)
(cog-execute! rule-applier)
; --------------------------------------------------------
; Demo: Perform processing on the stream. In this case, parse the
; input stream into token pairs. Use the LG "any" parser for this.
(use-modules (opencog nlp) (opencog nlp lg-parse))
; As above: rewind the stream to the beginning:
(cog-execute!
(SetValue (Concept "foo") (Predicate "some place")
(Open (Type 'TextFileStream)
(Sensory "file:///tmp/demo.txt"))))
; Gentle reminder of how to fetch this:
(define txt-stream-gen
(ValueOf (Concept "foo") (Predicate "some place")))
; Parse the file contents, one line at a time. The "any" dict generates
; random word-pairs. The (Number 1) asks for only one parse per
; sentence.
(cog-execute! (LgParseBonds txt-stream-gen (LgDict "any") (Number 1)))
(cog-execute! (LgParseBonds txt-stream-gen (LgDict "any") (Number 1)))
(cog-execute! (LgParseBonds txt-stream-gen (LgDict "any") (Number 1)))
; --------------------------------------------------------
; The End! That's All, Folks!