-
Notifications
You must be signed in to change notification settings - Fork 3
/
wordsub.c
44 lines (42 loc) · 995 Bytes
/
wordsub.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
const char *rcsid = "$Id: wordsub.c,v 1.1 2012/04/11 11:30:50 dyuret Exp $";
const char *usage = "Usage: wordsub [-s seed] < input > output\n"
"Input format: word sub1 logp1 sub2 logp2 ...\n"
"Output format: word random-sub\n";
// #define _GNU_SOURCE
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "dlib.h"
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "s:h")) != -1) {
switch (opt) {
case 's': srand(atoi(optarg)); break;
default: fputs(usage, stderr); exit(0);
}
}
forline(buf, NULL) {
double sum = 0;
int col = 0;
char *word = NULL;
char *sub = NULL;
char *last = NULL;
fortok(tok, buf) {
if (col == 0) {
word = tok;
} else if (col % 2) {
last = tok;
} else {
double logp = atof(tok);
double p = pow(10.0, logp);
sum += p;
if (sum * rand() / RAND_MAX <= p) {
sub = last;
}
}
col++;
}
printf("%s\t%s\n", word, sub);
}
}