aboutsummaryrefslogtreecommitdiff
path: root/source/backronyms.d
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-05-12 20:33:57 +0200
committerLinnea Gräf <nea@nea.moe>2025-05-12 20:33:57 +0200
commit4a6ddab6da2bfce1c97a1138ba529f9b9691d998 (patch)
treed2eb64f2b12af7a268cfde5685080db00aef3941 /source/backronyms.d
downloadboobbot-4a6ddab6da2bfce1c97a1138ba529f9b9691d998.tar.gz
boobbot-4a6ddab6da2bfce1c97a1138ba529f9b9691d998.tar.bz2
boobbot-4a6ddab6da2bfce1c97a1138ba529f9b9691d998.zip
Initial commit
Diffstat (limited to 'source/backronyms.d')
-rw-r--r--source/backronyms.d81
1 files changed, 81 insertions, 0 deletions
diff --git a/source/backronyms.d b/source/backronyms.d
new file mode 100644
index 0000000..7b3c9e6
--- /dev/null
+++ b/source/backronyms.d
@@ -0,0 +1,81 @@
+module backronyms;
+import std.stdio;
+import std.algorithm;
+import std.random;
+import std.conv;
+import std.range;
+import std.typecons;
+
+struct BackronymGenerator
+{
+ WordList nouns;
+ WordList adjectives;
+
+ static BackronymGenerator load()
+ {
+ return BackronymGenerator(
+ WordList.fromFile("SimpleWordlists/Wordlist-Nouns-Common-Audited-Len-3-6.txt"),
+ WordList.fromFile("SimpleWordlists/Wordlist-Adjectives-All.txt"),
+ );
+ }
+
+ string formatBackronymList(string word)
+ {
+ string ret;
+ for (int i = 0; i < word.length; i++)
+ {
+ ret ~= (
+ i + 1 == word.length
+ ? this.nouns : this.adjectives
+ ).findRandomWord(word[i]);
+ ret ~= '\n';
+ }
+ return ret;
+ }
+}
+
+struct WordList
+{
+
+ string[][char] words;
+
+ string[] findBackronym(string word)
+ {
+ string[] ret;
+
+ foreach (c; word)
+ {
+ ret ~= this.findRandomWord(c);
+ }
+
+ return ret;
+ }
+
+ string findRandomWord(char start)
+ {
+ return this.words[start].choice;
+ }
+
+ static WordList fromFile(string filePath)
+ {
+ auto file = File(filePath);
+ return fromLines(file.byLine.map!(to!string));
+ }
+
+ static WordList fromLines(R)(R lines)
+ if (isInputRange!R && is(ElementType!R == string))
+ {
+ WordList ret;
+
+ foreach (line; lines)
+ {
+ if (line.length == 0)
+ continue;
+
+ ret.words[line[0]] ~= line;
+ }
+
+ return ret;
+ }
+
+}