-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathipsumat.c
346 lines (305 loc) · 8.92 KB
/
ipsumat.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
Kernagic a libre spacing tool for Unified Font Objects.
Copyright (C) 2013 Øyvind Kolås
Kernagicis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Kernagic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kernagic. If not, see <http://www.gnu.org/licenses/>. */
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <sys/time.h>
#include <time.h>
#define MAX_WORDS 500
int attempts = 500;
static int print_score = 0;
/** score_string:
* @str input string
* @desired_glyphs string containing highly desired glyphs
*
* Generates an ipsum with an attempt at maximizing the amount of differing
* neighbour pairs.
*
* Returns the score for a string, a higher score is a better string.
*/
static int score_string (const char *str,
const char *desired_glyphs)
{
/* we pick slightly larger than a power of two, to aovid aliasing of things
* starting on multiples of 512 in the unicode set.
*/
#define ADJ_DIM 1023
gunichar *ustr;
char adjacency_matrix[ADJ_DIM*ADJ_DIM]={0,};
gunichar *p;
if (!str || str[0] == 0)
return 0;
ustr = g_utf8_to_ucs4 (str, -1, NULL, NULL, NULL);
if (!ustr)
return 0;
/* walk throguh the string ..*/
for (p = ustr; p[1]; p++)
{
gunichar x = p[0]; /* .. using the current .. */
gunichar y = p[1]; /* .. and the next characters unicode position ..*/
if (x==' ' || y == ' ')
continue; /* (bailing if one of them is a space) */
x %= ADJ_DIM; /* with unicode positions wrapped down to our */
y %= ADJ_DIM; /* matrix dimensions */
/* mark cell in matrix as visited */
adjacency_matrix[y * ADJ_DIM + x] = 1;
}
/* count number of distinct pairs encountered (permitting some collisions,
* in a bloom-filter like manner) */
{
int i;
int sum = 0;
if (desired_glyphs)
for (i = 0; ustr[i]; i++)
{
int j;
for (j = 0; desired_glyphs[j]; j++)
if (desired_glyphs[j] == ustr[i])
sum ++;
}
for (i = 0; i < ADJ_DIM * ADJ_DIM ; i ++)
sum += adjacency_matrix[i] * 2;
g_free (ustr);
return sum;
}
}
char *ipsumat_generate (const char *dict_path,
const char *charset,
const char *desired_glyphs,
int max_wordlen,
int max_words)
{
gunichar *p;
gunichar *ucharset;
gunichar *udesired_glyphs = NULL;
int count = 0;
int best_sentence[MAX_WORDS]={0,};
int sentence[MAX_WORDS]={0,};
char *words_str = NULL;
gunichar *uwords_str = NULL;
GList *words = NULL;
GString *word = g_string_new ("");
int best_score = 0;
int i;
if (!dict_path)
dict_path = "/usr/share/dict/words";
g_file_get_contents (dict_path, &words_str, NULL, NULL);
if (!words_str)
return g_strdup ("problem opening dictionary");
uwords_str = g_utf8_to_ucs4 (words_str, -1, NULL, NULL, NULL);
if (charset == NULL)
charset = "abcdefghijklmnopqrstuvwxyz";
ucharset = g_utf8_to_ucs4 (charset, -1, NULL, NULL, NULL);
if (desired_glyphs)
udesired_glyphs = g_utf8_to_ucs4 (desired_glyphs, -1, NULL, NULL, NULL);
if (max_words > MAX_WORDS)
max_words = MAX_WORDS;
for (p = uwords_str; *p; p++)
{
switch (*p)
{
case '\n':
case '\r':
case ' ':
case '\t':
if (word->len)
{
int skip = 0;
int i;
gunichar *uword = g_utf8_to_ucs4 (word->str, -1, NULL, NULL, NULL);
for (i = 0; uword[i]; i++)
{
int k;
skip++;
for (k = 0; ucharset[k]; k++)
if (ucharset[k]==uword[i])
{
skip--;break;
}
}
if (word->len > max_wordlen)
skip++;
if (!skip)
{
words = g_list_prepend (words, g_strdup (word->str));
count ++;
}
g_free (uword);
}
g_string_assign (word, "");
break;
default:
g_string_append_unichar (word, *p);
break;
}
}
g_free (ucharset);
g_free (words_str);
g_free (uwords_str);
for (i = 0; i < attempts; i ++)
{
GString *example = g_string_new ("");
int j;
for (j = 0; j < max_words; j ++)
{
int n;
const char *str;
n = rand()%count;
{
int k;
for (k = 0; k < j; k++)
if (sentence[k]==n)
{
/* we try once more if it collides with already picked
* random number,. - but this value will stick */
n = rand()%count;
break;
}
}
sentence[j] = n;
str = g_list_nth_data (words, n);
if (str)
{
if (j)
g_string_append (example, " ");
g_string_append (example, str);
}
}
float score = score_string ((void*)example->str, desired_glyphs);
if (score >= best_score)
{
for (j = 0; j < max_words; j ++)
best_sentence[j] = sentence[j];
best_score = score;
}
g_string_free (example, TRUE);
}
if (print_score)
printf ("Score: %i\n", best_score);
{
char *ret = NULL;
int j;
GString *s = g_string_new ("");
if (desired_glyphs && desired_glyphs[0])
{
g_string_append (s, desired_glyphs);
g_string_append (s, " ");
}
for (j = 0; j < max_words; j ++)
{
const char *str;
str = g_list_nth_data (words, best_sentence[j]);
if (str)
{
if (j)
g_string_append (s, " ");
g_string_append (s, str);
}
}
ret = strdup (s->str);
g_string_free (s, TRUE);
g_free (udesired_glyphs);
return ret;
}
}
/* the main used - when executable is named/symlinked ipsum */
int ipsumat (int argc, char **argv)
{
int argno;
int iterations = 1;
int max_wordlen = 7;
int wordcount = 23;
const char *glyphs = "abcdefghijklmnopqrstuvwxyz";
const char *desired_glyphs = "";
const char *dict_path = NULL;
struct timeval time;
gettimeofday (&time, NULL);
srand (time.tv_sec);
for (argno = 1; argno < argc; argno++)
{
if (!strcmp (argv[argno], "--help") ||
!strcmp (argv[argno], "-h"))
{
fprintf (stdout, "\n"
"ipsumat [options]\n"
" -m <maxwordlength, default = 7>\n"
" -w <number of words, default = 23>\n"
" -i <number of iterations>\n"
" -s <seed for random generator - defaults to time of day>\n"
" -g <string of permitted glyphs>\n"
" -d <string of highly desired glyphs>\n"
" -D <path to dictionary file>\n"
" -p print score\n"
"\n");
return 0;
}
else if (!strcmp (argv[argno], "-w"))
{
#define EXPECT_ARG if (!argv[argno+1]) {fprintf (stderr, "expected argument after %s\n", argv[argno]);exit(-1);}
EXPECT_ARG;
wordcount = atoi (argv[++argno]);
}
else if (!strcmp (argv[argno], "-m"))
{
EXPECT_ARG;
max_wordlen = atoi (argv[++argno]);
}
else if (!strcmp (argv[argno], "-g"))
{
EXPECT_ARG;
glyphs = argv[++argno];
}
else if (!strcmp (argv[argno], "-s"))
{
EXPECT_ARG;
srand (atoi(argv[++argno]));
}
else if (!strcmp (argv[argno], "-D"))
{
EXPECT_ARG;
dict_path = argv[++argno];
}
else if (!strcmp (argv[argno], "-d"))
{
EXPECT_ARG;
desired_glyphs = argv[++argno];
}
else if (!strcmp (argv[argno], "-p"))
{
print_score = 1;
}
else if (!strcmp (argv[argno], "-i"))
{
EXPECT_ARG;
iterations = atoi (argv[++argno]);
}
else if (!strcmp (argv[argno], "-a"))
{
EXPECT_ARG;
attempts = atoi (argv[++argno]);
}
}
int i;
for (i = 0; i < iterations; i ++)
printf ("%s\n",
ipsumat_generate (dict_path,
glyphs,
desired_glyphs,
max_wordlen,
wordcount));
return 0;
}