-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomPdf.t5
More file actions
172 lines (141 loc) · 5.78 KB
/
Copy pathRandomPdf.t5
File metadata and controls
172 lines (141 loc) · 5.78 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
PRAGMA Tanager5
PRAGMA Library "Demo.RandomPDF"
-- PRAGMA NoValidation --
-- Random-text-PDF generation in Tanager. --
-- --
-- Copyright 2018 K.D.P.Ross <KDPRoss@gmail.com> --
-- --
-- This code is licensed only for study and personal --
-- enrichment. --
-- /------------------------------------------\
-- | ‘The raison d'être of a program is to |
-- | be a mathematically-elegant object of |
-- | sublime beauty; it's also pretty cool if |
-- | it occasionally computes something.’ |
-- | - K.D.P.Ross |
-- \------------------------------------------/
_ = iter load
[ "<Prelude.Extended>",
"<System.Base>"
]
-- Ah, I guess? This is the only dict file I seem to have.
-- YMMV.
dictFile : String
... = "/usr/share/dict/cracklib-small"
-- Load dict, filter out short words and those with weird
-- char's.
vocab : Array String
... = dictFile |>
readFile-stream >>
(progress-stream _ -1) >>
filter-stream (goodWord and^
longEnough
) >>
stream->list >>
make-array
where goodWord = mr "^[a-z]*$"
longEnough = stringLength >>
(_ >= 10)
genWord : () -> String
... = let strict n = length* vocab
\ () ->
vocab >*> i
where i = n |> random >> (_ + 1)
-- Bit of a hack: Tanager's random-number-generation
-- behaviour uses a heuristic that will generate natural
-- numbers if we pass `1`.
randReal : () -> Num
... () = random 0.9999999999
-- Constants belong somewhere else; meh.
genPunct : () -> String
... _ = case randReal ()
<(_ < commaP)> -> ","
<(_ < semiP)> -> ";"
_ -> ""
where commaP = 0.1
semiP = 0.13
genFinalPunct : () -> String
... _ = case randReal ()
<(_ < questionP)> -> "?"
<(_ < bangP)> -> "!"
_ -> "."
where questionP = 0.1
bangP = 0.11
capitalise : String -> String
... s = case explode s
[] -> ""
c :: cs -> c |>
uppercase >>
(_ :: cs) >>
implode
-- Some of this code could be merged / generalised, but this
-- is ‘rough-'n'-ready’ cowboy code.
genSentence : () -> String
... _ = words'' :@ (word # finalPunct) |>
intersperse " " >>
implode
where (word :: words) = n |>
count >>
map (ignore >>
genWord
)
words' = ,(Cons capitalise _) words
words'' = zipWith (#) words' puncts
finalPunct = genFinalPunct ()
puncts = words |>
map (ignore >>
genPunct
)
n = random (maxWords - minWords + 1) + minWords
minWords = 3
maxWords = 20
genParagraph : () -> String
... _ = n |>
count >>
map (ignore >>
genSentence
) >>
intersperse " " >>
implode
where n = random (maxSentences - minSentences) + minSentences
minSentences = 5
maxSentences = 25
-- Tie things together: For object-level processing, load the
-- standard libraries, set a random title and the author, etc.
genDoc : String -> Num -> ()
... f n = writeFile f text
where text = header ++ body ++ footer
body = n |>
count >>
map (ignore >>
genParagraph
) >>
intersperse "[]"
header = intersperse ""
[ "@ topLevel|loadFile 'All.All.lib.t5'",
"@ docAuthor := " # quote # author # quote,
"@ docTitle := " # quote # title # quote,
"@ processLatex := processLatexMakeClosed",
""
]
footer = []
author = "Big Bubsy" -- Because … who else would it be‽
title = numTitleWords |>
count >>
map (ignore >>
genWord >>
capitalise
) >>
intersperse " " >>
implode
numTitleWords = 6
-- Generate a .pdf with specified number of paragraphs; run
-- Tanager on it … from Tanager.
genPdf : String -> Num -> ()
... f n = genDoc t5file n
evalNoResult cmd
where t5file = f # t5Ext
t5Ext = ".txt.t5"
latexExt = ".tex"
cmd = "cd " # dir # " ; Tanager -batch -i " # t5file # " -o " # f # latexExt # " -s Quick"
[ dir ] = evalResult <| "dirname " # f