Compare commits

...

7 Commits

  1. 13
      Makefile
  2. 34
      README.md
  3. 108
      main.c

13
Makefile

@ -1,4 +1,15 @@ @@ -1,4 +1,15 @@
GCC = gcc -O2
GCC = gcc -O3 --static
default: main.c
$(GCC) main.c -o banagram
debug: main.c
gcc -Wall -g -o banagram main.c
install: default
cp banagram /usr/bin/banagram
cp -n words /usr/share/dict/banawords
uninstall:
rm /usr/bin/banagram

34
README.md

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
# Banagram.c
An anagram finder optimised for the game bananagrams-battle. It allows finding
words that contain all the letters of a given word, plus any given number of
letters from a pool of available letters.
To do this use
```
$ # banagram min <num extra letters> <word> <(optional) string of available letters>"
$ banagram min 999 hello gufsv
hello
lughole + 1g 1u
shovelful + 1f 1s 1u 1v
```
Alternatively, to find all words containing exactly all the same letters as a
given word, use:
```
$ # banagram exact word
$ banagram e meal
elam
elma
male
lame
male
meal
```
## TODO:
- Improve interface, replace manual parsing code with `getopt(3)`
- General cleanup and commenting

108
main.c

@ -2,9 +2,12 @@ @@ -2,9 +2,12 @@
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#define DICTFILE "/usr/share/dict/banawords"
/*
* Copyright 2020 Alistair
* Copyright 2020 Alistair Michael
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -27,13 +30,13 @@ @@ -27,13 +30,13 @@
*
*/
#define LINELENGTH 200
// for string qsort
int compar(const void * l1, const void *l2) {
return *(char *)l1 > *(char*)l2;
}
#define LINELENGTH 200
void make_lowercase(char *word) {
int j = 0;
while (word[j] != '\0') {
@ -46,7 +49,7 @@ void make_lowercase(char *word) { @@ -46,7 +49,7 @@ void make_lowercase(char *word) {
int *count_char(char *word) {
make_lowercase(word);
int* counts = calloc(27, sizeof(int));
int* counts = (int*)calloc(27, sizeof(int));
int j = 0;
while (word[j] != '\0') {
@ -60,18 +63,37 @@ int *count_char(char *word) { @@ -60,18 +63,37 @@ int *count_char(char *word) {
return counts;
}
int get_minimum_match(char *word, int added) {
FILE *wfile = fopen("words", "r");
FILE *open_dict_file(char *filename) {
FILE *wfile = fopen(filename, "r");
if (!wfile) {
fprintf(stderr, "Unable to open dict\n");
exit(1);
}
return wfile;
}
// @param allowed: additional characters allowed in anagram match,
// set to NULL to allow matching any set of characters.
int get_minimum_match(char *word, int added, char *allowed) {
FILE* wfile = open_dict_file(DICTFILE);
char line[LINELENGTH];
int err;
int len = strlen(word);
int *min_letter_counts = count_char(word);
if (!min_letter_counts) {
int *orig_word_letter_counts = count_char(word);
if (!orig_word_letter_counts) {
fclose(wfile);
return 1;
}
// loop over every word in the word file and check if it is an allowed
// anagram of word optionally containing some/all of the allowed letters
do {
int *max_letter_counts = NULL;
int *anagram_letter_counts = NULL;
err = fscanf(wfile, "%s\n", line);
if (err == EOF) {
break;
@ -82,40 +104,76 @@ int get_minimum_match(char *word, int added) { @@ -82,40 +104,76 @@ int get_minimum_match(char *word, int added) {
continue;
}
int* word_counts = count_char(line);
if (!word_counts) {
anagram_letter_counts = count_char(line);
if (!anagram_letter_counts) {
continue;
}
int match = 1;
if (allowed) {
max_letter_counts = count_char(allowed);
if (!max_letter_counts) {
return 1;
}
for (int i = 0; i < 26; i++) {
max_letter_counts[i] += orig_word_letter_counts[i];
}
}
bool match = true;
for (int i = 0; i < 26; i++) {
if (min_letter_counts[i] > word_counts[i]) {
match = 0;
for (int i = 0; (i < 26) && match; i++) {
// if word contains a letter that the anagram does not
if (orig_word_letter_counts[i] > anagram_letter_counts[i]) {
match = false;
break;
}
// if the anagram contains a letter that is not in the word, or
// pool of available letters
if (allowed) {
if (max_letter_counts[i] < anagram_letter_counts[i]) {
match = false;
break;
}
}
}
// print the anagram and the additional letters required
if (match) {
printf("%s +", line);
bool s = false;
printf("%s", line);
for (int i = 0; i < 26; i++) {
int diff = word_counts[i] - min_letter_counts[i];
int diff = anagram_letter_counts[i] - orig_word_letter_counts[i];
if (diff > 0) {
if (!s) {
s = true;
printf(" +");
}
printf(" %d%c", diff, i + 'a');
}
}
printf(" ");
printf("\n");
}
free(anagram_letter_counts);
if (max_letter_counts) {
free(max_letter_counts);
}
} while (err != EOF);
free(min_letter_counts);
free(orig_word_letter_counts);
fclose(wfile);
return 0;
}
int get_exact_anag(char *word) {
FILE *wfile = fopen("words", "r");
make_lowercase(word);
FILE* wfile = open_dict_file(DICTFILE);
char line[LINELENGTH];
int err;
@ -151,11 +209,14 @@ int get_exact_anag(char *word) { @@ -151,11 +209,14 @@ int get_exact_anag(char *word) {
}
int main (int argc, char **argv) {
char *usage_string = "cmd min <num extra letters> <word> <(optional) pool "
"of available letters>"
"\ncmd exact <word>\n";
char word[LINELENGTH];
if (argc <= 2) {
printf("cmd min <num extra letters> <word>\ncmd exact <word>\n");
fprintf(stderr, "%s", usage_string);
return 1;
}
@ -167,7 +228,7 @@ int main (int argc, char **argv) { @@ -167,7 +228,7 @@ int main (int argc, char **argv) {
strcpy(word, argv[2]);
get_exact_anag(word);
} else if (argv[1][0] == 'm') {
if (argc != 4) {
if (argc < 4 || argc > 5) {
fprintf(stderr, "Wrong args.\n");
return 1;
}
@ -176,9 +237,12 @@ int main (int argc, char **argv) { @@ -176,9 +237,12 @@ int main (int argc, char **argv) {
if (num == 0) {
get_exact_anag(word);
} else {
get_minimum_match(word, num);
char *allowed = NULL;
if (argc == 5)
allowed = argv[4];
get_minimum_match(word, num, allowed);
}
} else {
printf("cmd min <num extra letters> <word>\ncmd exact <word>\n");
fprintf(stderr, "%s", usage_string);
}
}

Loading…
Cancel
Save