diff options
-rw-r--r-- | lib/jdtalk/core.d | 45 | ||||
-rw-r--r-- | source/app.d | 27 |
2 files changed, 71 insertions, 1 deletions
diff --git a/lib/jdtalk/core.d b/lib/jdtalk/core.d index aafe64a..c6a2c6b 100644 --- a/lib/jdtalk/core.d +++ b/lib/jdtalk/core.d @@ -140,18 +140,39 @@ bool hasWord(string match, string str) { } +bool wordStartsWith(ref string[] words, char ch) { + foreach (word; words) { + if (word.startsWith(ch.to!string)) { + return true; + } + } + return false; +} + + bool searchDict(dict_t dict, string pattern) { bool has_pattern = false; foreach (wordList; [dict.noun, dict.verb, dict.adverb, dict.adjective]) { foreach (word; wordList) { has_pattern = wordList.canFind(pattern); - break; + if (has_pattern) break; } if (has_pattern) break; } return has_pattern; } +char acronymSafe(ref dict_t dict, string acronym) { + string[] words; + words = dict.noun ~ dict.verb ~ dict.adverb ~ dict.adjective; + for (int i = 0; i < acronym.length; i++) { + if (!wordStartsWith(words, acronym[i])) { + return acronym[i]; + } + } + return 0; +} + dict_t getData(string root) { // override data root with environment variable @@ -178,6 +199,7 @@ string talk(ref dict_t dict) { return output; } + string talkSalad(ref dict_t dict, int words) { string[] salad; for (int i = 0; i < words; i++) { @@ -187,6 +209,7 @@ string talkSalad(ref dict_t dict, int words) { return salad.join(" "); } + string talkf(ref dict_t dict, string fmt) { string[] output; for (int i = 0; i < fmt.length; i++) { @@ -211,3 +234,23 @@ string talkf(ref dict_t dict, string fmt) { } return output.join(" "); } + + +string talkAcronym(ref dict_t dict, string abbrev) { + string[] words; + string[] output; + + words = dict.noun ~ dict.verb ~ dict.adverb ~ dict.adjective; + + for (int i = 0; i < abbrev.length; i++) { + string word = ""; + while ((word = words.choice) !is null) { + if (word.startsWith(abbrev[i].to!char)) { + break; + } + } + + output ~= word; + } + return output.join(" "); +} diff --git a/source/app.d b/source/app.d index e816d8b..08cd3f4 100644 --- a/source/app.d +++ b/source/app.d @@ -24,6 +24,7 @@ int main(string[] args) bool rCase = false; bool hCase = false; bool haxor = false; + string acronym; string custom_format = null; string pattern = null; string dataRoot = absolutePath(getcwd()); @@ -35,6 +36,7 @@ int main(string[] args) auto opt = getopt( args, "format|f", "Produce words with formatted string", &custom_format, + "acronym|a", "Expand characters to acronym", &acronym, "limit|c", format("(default: %d)", limit), &limit, "pattern|p", "Limit output to a root word", &pattern, "exact|e", format("Exact matches only (default: %s)", exactMatch ? "true" : "false"), &exactMatch, @@ -62,12 +64,23 @@ int main(string[] args) return 1; } + if (acronym) { + char result = 0; + if ((result = acronymSafe(dict, acronym)) > 0) { + stderr.writefln("No words start with: '%c'", result); + return 1; + } + } + while(true) { string output; if (salad) { output = talkSalad(dict, salad); } + else if (acronym) { + output = talkAcronym(dict, acronym); + } else if (custom_format) { output = talkf(dict, custom_format); } @@ -94,6 +107,20 @@ int main(string[] args) output = leetSpeak(output); } + if (output == null) { + writeln("An error has occurred."); + return 1; + } + + if (acronym) { + // Capitalize each word in the acronym + string[] parts; + foreach (word; output.split(" ")) { + parts ~= word.capitalize; + } + output = parts.join(" "); + } + writeln(output); if (limit > 0) { |