diff options
author | tomsmeding <tom.smeding@gmail.com> | 2020-01-02 22:19:39 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2020-01-02 22:44:37 +0100 |
commit | 08bf951fb7f9d976bb9f2d9ffd7376fab2b764cf (patch) | |
tree | f1a9afe6c976e50439c2125a350bf7f52028e5b2 /modules | |
parent | b41a370dfc8e602eca6c0b37a6eac22e8de8e1bb (diff) |
Let abbrgen work in Docker
Diffstat (limited to 'modules')
-rw-r--r-- | modules/abbrgen/abbreviation_gen.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/modules/abbrgen/abbreviation_gen.cpp b/modules/abbrgen/abbreviation_gen.cpp new file mode 100644 index 0000000..708bfdf --- /dev/null +++ b/modules/abbrgen/abbreviation_gen.cpp @@ -0,0 +1,58 @@ +#include <iostream> +#include <fstream> +#include <vector> +#include <string> +#include <algorithm> +#include <cstring> +#include <cctype> +#include <sys/time.h> + +using namespace std; + +int main(int argc, char **argv) { + if (argc != 2 && argc != 3) { + cout << "Usage: " << argv[0] << " <abbreviation> [number of answers]" << endl; + return 1; + } + + char *abbr = argv[1]; + int abbrlen = strlen(abbr); + transform(abbr, abbr+abbrlen, abbr, ::tolower); + for (int i = 0; i < abbrlen; i++) { + if (abbr[i] < 'a' || abbr[i] > 'z') { + cout << "Abbreviation cannot contain '" << abbr[i] << "'!" << endl; + return 1; + } + } + + struct timeval tv; + gettimeofday(&tv, NULL); + srand(1000000 * tv.tv_sec + tv.tv_usec); + int numanswers = 1; + if (argc == 3) { + numanswers = strtol(argv[2], nullptr, 10); + if (numanswers <= 0) return 0; + } + + vector<string> dict[26]; + ifstream dictfile("/usr/share/dict/words"); + string line; + + while (getline(dictfile, line)) { + transform(line.begin(), line.end(), line.begin(), ::tolower); + if (line[0] >= 'a' && line[0] <= 'z' && all_of(line.begin(), line.end(), ::islower)) + dict[line[0] - 'a'].push_back(line); + } + + while (numanswers --> 0) { + for (int i = 0; i < abbrlen; i++) { + if (i > 0) cout << ' '; + size_t index = rand() % dict[abbr[i] - 'a'].size(); + cout << dict[abbr[i] - 'a'][index]; + } + cout << '\n'; + } + cout.flush(); + + return 0; +} |