From ce48be18ff6d36326521cfb28eea203c562f391c Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Tue, 2 Nov 2021 11:23:53 -0400 Subject: Break down project into smaller chunks --- talk.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 talk.c (limited to 'talk.c') diff --git a/talk.c b/talk.c new file mode 100644 index 0000000..c4dfff8 --- /dev/null +++ b/talk.c @@ -0,0 +1,110 @@ +#include "jdtalk.h" + +char *talkf(struct Dictionary *dict, const char *fmt, char **parts) { + static char buf[OUTPUT_SIZE_MAX]; + buf[0] = '\0'; + + if (!fmt || !strlen(fmt)) { + return NULL; + } + + size_t len; + len = strlen(fmt); + for (size_t i = 0; i < len; i++) { + char *word = NULL; + switch (fmt[i]) { + case 'a': + word = dictionary_word(dict, WT_ADJECTIVE); + break; + case 'd': + word = dictionary_word(dict, WT_ADVERB); + break; + case 'n': + word = dictionary_word(dict, WT_NOUN); + break; + case 'v': + word = dictionary_word(dict, WT_VERB); + break; + default: + break; + } + + if (parts) + parts[i] = word; + + strncat(buf, word, OUTPUT_SIZE_MAX); + + if (i < len - 1) + strcat(buf, " "); + } + return buf; +} + +char *talk_salad(struct Dictionary *dict, size_t limit, char **parts) { + static char buf[OUTPUT_SIZE_MAX]; + buf[0] = '\0'; + for (size_t i = 0; i < limit; i++) { + char *word = NULL; + word = dictionary_word(dict, WT_ANY); + parts[i] = word; + strncat(buf, word, OUTPUT_SIZE_MAX); + if (i < limit - 1) { + strcat(buf, " "); + } + } + return buf; +} + +char *talk_acronym(struct Dictionary *dict, const char *fmt, char *s, char **parts) { + size_t len; + static char buf[OUTPUT_SIZE_MAX]; + buf[0] = '\0'; + + len = strlen(s); + for (size_t i = 0; i < strlen(s); i++) { + char *word = NULL; + while(1) { + word = dictionary_word(dict, WT_ANY); + if (*word == s[i]) { + break; + } + /* TODO: Formatted acronyms are too slow. Need a better way. + if (strlen(fmt) < strlen(s)) { + return NULL; + } + + char letter[2] = {'\0', '\0'}; + for (size_t f = 0; f < strlen(fmt); f++) { + *letter = fmt[f]; + word = talkf(dict, letter, NULL); + if (*word == s[i]) { + done = 1; + break; + } + } + */ + } + parts[i] = word; + strncat(buf, word, OUTPUT_SIZE_MAX); + if (i < len - 1) { + strcat(buf, " "); + } + } + return buf; +} + +int acronym_safe(const char *acronym, const char *pattern) { + size_t acronym_len; + int pattern_valid; + pattern_valid = 0; + acronym_len = strlen(acronym); + for (size_t i = 0; i < acronym_len; i++) { + if (*pattern == acronym[i]) { + pattern_valid = 1; + break; + } + } + + return pattern_valid; +} + -- cgit