From cb2b7de4b3b322398a12a9f4cdcb9416c881c486 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Thu, 9 Mar 2023 15:21:26 +0100 Subject: [PATCH] basic usage now possible --- main.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index 0fd0cab..0b0cdd5 100644 --- a/main.cpp +++ b/main.cpp @@ -1,36 +1,107 @@ +#include +#include #include #include +#include +#include #include +#include #include +#include +#include + namespace po = boost::program_options; int main(int argc, char *argv[]) { std::string letters; + int wordLengthMin{1}; + int wordLengthMax{1024}; + int lineLength{60}; + int numLines{4}; + std::string wordsFile{"/usr/share/dict/ngerman"}; + bool lowercase{false}; po::options_description desc("Erlaubte Optionen"); - desc.add_options()("help,h", "Ausgabe der Hilfe")("letters,l", po::value(&letters), - "Enthaltene Buchstaben"); + desc.add_options()("help,h", "Ausgabe der Hilfe")( + "characters,c", po::value(&letters), "Enthaltene Buchstaben")( + "min-length", po::value(&wordLengthMin), + "Minimale Wortlänge")("max-length", po::value(&wordLengthMax), "Maximale Wortlänge")( + "line-length", po::value(&lineLength), + "Maximale Zeilenlänge")("num-lines,n", po::value(&numLines), "Anzahl Zeilen")( + "lowercase", po::bool_switch(&lowercase), "Ausgabe nur klein geschrieben")( + "dictfile,d", po::value(&wordsFile), "Pfad zur Wörterbuchdatei"); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); - if (vm.count("help")) { + if (vm.contains("help")) { std::cout << desc << std::endl; return 1; } - std::ifstream infile("../wortliste.txt"); + std::ifstream infile(wordsFile); std::string line; - + icu::UnicodeString uline; + std::vector words; + icu::UnicodeString ucLetters = icu::UnicodeString::fromUTF8(letters); + icu::UnicodeString ucLettersUpper = icu::UnicodeString::fromUTF8(letters).toUpper(); long counter{0}; + if (!std::filesystem::exists(wordsFile)) { + std::cerr << "Datei existiert nicht: " << wordsFile << std::endl; + return 1; + } + while (std::getline(infile, line)) { + uline = icu::UnicodeString::fromUTF8(line); + int length = uline.length(); + + if (length < wordLengthMin || length > wordLengthMax) + continue; + + if (ucLetters.length() > 0) { + bool valid = true; + for (int i{0}; i < uline.length(); i++) { + auto ch = uline.charAt(i); + if (ucLetters.indexOf(ch) == -1 && ucLettersUpper.indexOf(ch) == -1) { + valid = false; + break; + } + } + if (!valid) + continue; + } + if (lowercase) { + uline.toLower(); + } + + words.push_back(uline); counter++; } - std::cout << "Anzahl: " << counter << std::endl; + std::cout << "Anzahl infrage kommender Wörter: " << counter << "\n" << std::endl; + + std::random_device rdev; + std::mt19937 rng(rdev()); + std::uniform_int_distribution distrib(0, words.size() - 1); + for (int i{0}; i < numLines; i++) { + icu::UnicodeString lectionLine; + while (lectionLine.length() < lineLength) { + if (lectionLine.isEmpty()) + lectionLine.append(words.at(distrib(rng))); + else + lectionLine.append(" " + words.at(distrib(rng))); + } + + if (lectionLine.length() > lineLength) { + lectionLine.retainBetween(0, lectionLine.lastIndexOf(" ") + 1); + lectionLine.trim(); + } + + std::cout << lectionLine << std::endl; + } } \ No newline at end of file