#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")( "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.contains("help")) { std::cout << desc << std::endl; return 1; } 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 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; } }