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
|
implement Writestrokes;
#
# write structures to classifier files
#
include "sys.m";
sys: Sys;
include "bufio.m";
bufio: Bufio;
Iobuf: import bufio;
include "strokes.m";
strokes: Strokes;
Penpoint, Stroke: import strokes;
init(s: Strokes)
{
sys = load Sys Sys->PATH;
bufio = load Bufio Bufio->PATH;
strokes = s;
}
write_examples(fd: ref Sys->FD, names: array of string, examples: array of list of ref Stroke): string
{
fp := bufio->fopen(fd, Bufio->OWRITE);
nclass := len names;
fp.puts(sys->sprint("%d\n", nclass));
for(i := 0; i < nclass; i++){
exl := examples[i];
fp.puts(sys->sprint("%d %s\n", len exl, names[i]));
for(; exl != nil; exl = tl exl){
putpoints(fp, hd exl);
fp.putc('\n');
}
}
if(fp.flush() == Bufio->ERROR)
return sys->sprint("write error: %r");
fp.close();
return nil;
}
write_digest(fd: ref Sys->FD, cnames: array of string, dompts: array of ref Stroke): string
{
fp := bufio->fopen(fd, Bufio->OWRITE);
n := len cnames;
for(i := 0; i < n; i++){
d := dompts[i];
npts := d.npts;
fp.puts(cnames[i]);
putpoints(fp, d);
fp.putc('\n');
}
if(fp.flush() == Bufio->ERROR)
return sys->sprint("write error: %r");
fp.close();
return nil;
}
putpoints(fp: ref Iobuf, d: ref Stroke)
{
fp.puts(sys->sprint(" %d", d.npts));
for(j := 0; j < d.npts; j++){
p := d.pts[j];
fp.puts(sys->sprint(" %d %d", p.x, p.y));
}
}
|