Last active
December 31, 2015 14:19
-
-
Save bldewolf/7999897 to your computer and use it in GitHub Desktop.
"Anagram" guesser using Aspell. Also guesses on substrings, because that's more fun.
This file contains hidden or 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
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <aspell.h> | |
/* gcc -o anagram anagram.c -O2 -Wall -laspell | |
*/ | |
void permut(char *chs, char *str, int cur, int len, AspellSpeller * spell_checker) { | |
int i; | |
if(cur > 0) { | |
str[cur] = 0; | |
if(aspell_speller_check(spell_checker, str, -1)) | |
printf("%s\n", str); | |
// else | |
// printf("%s 0\n", str); | |
} | |
if(cur == len) { | |
// str[cur] = 0; | |
// printf("%s\n", str); | |
return; | |
} | |
for(i = cur; i < len; i++) { | |
if(strchr(chs + cur, chs[i]) != chs + i) /* skip guessing repeat chars */ | |
continue; | |
/* swap the i-th char of chs to cur, so we move all of our | |
* 'guessed guesses' to <cur in chs */ | |
char c = chs[i]; | |
chs[i] = chs[cur]; | |
chs[cur] = c; | |
str[cur] = c; | |
permut(chs, str, cur+1, len, spell_checker); | |
/* don't forget to swap our guess back! */ | |
chs[cur] = chs[i]; | |
chs[i] = c; | |
} | |
} | |
int main(int argc, char *argv[]) { | |
char *chs, *str; | |
AspellConfig * spell_config; | |
AspellSpeller * spell_checker; | |
AspellCanHaveError * possible_err; | |
if(argc < 2) { | |
exit(1); | |
} | |
chs = strdup(argv[1]); | |
str = malloc(strlen(argv[1]) + 1); | |
spell_config = new_aspell_config(); | |
aspell_config_replace(spell_config, "lang", "en_US"); | |
possible_err = new_aspell_speller(spell_config); | |
if (aspell_error_number(possible_err) != 0) | |
puts(aspell_error_message(possible_err)); | |
else | |
spell_checker = to_aspell_speller(possible_err); | |
if(aspell_error_number(possible_err) != 0) { | |
puts(aspell_error_message(possible_err)); | |
exit(1); | |
} | |
permut(chs, str, 0, strlen(chs), spell_checker); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment