Created
February 18, 2024 22:11
-
-
Save colematt/af11dc7ca4ad2f53f4e6043fc4f1856f to your computer and use it in GitHub Desktop.
[Get a random word from wordnet] #c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sudo apt update && apt install wordnet-dev | |
#include <stdio.h> | |
#include <stdlib.h> | |
char *pickword(char *file) | |
{ | |
FILE *fp = fopen(file, "r"); | |
if (!fp) perror(file), exit(1); | |
fseek(fp, 0, SEEK_END); | |
long end = ftell(fp); | |
if (RAND_MAX < end) puts("RAND_MAX too small"), exit(1); | |
int c; | |
do | |
fseek(fp, rand()%end, SEEK_SET), | |
fscanf(fp, "%*[^\n]%*c"), // skip partial line | |
c = getc(fp); | |
while (c <= ' '); // header lines start with space | |
ungetc(c, fp); | |
static char word[72]; | |
fscanf(fp, "%71s", word); | |
fclose(fp); | |
return word; | |
} | |
int main() | |
{ | |
srand(time(NULL)); | |
puts(pickword("/usr/share/wordnet/index.noun")); | |
puts(pickword("/usr/share/wordnet/index.verb")); | |
puts(pickword("/usr/share/wordnet/index.adv")); | |
puts(pickword("/usr/share/wordnet/index.adj")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment