basic usage now possible

This commit is contained in:
Martin Brodbeck 2023-03-09 15:21:26 +01:00
parent 06c7e144db
commit cb2b7de4b3

View file

@ -1,36 +1,107 @@
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <locale>
#include <random>
#include <string>
#include <vector>
#include <boost/program_options.hpp>
#include <unicode/unistr.h>
#include <unicode/ustream.h>
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<std::string>(&letters),
"Enthaltene Buchstaben");
desc.add_options()("help,h", "Ausgabe der Hilfe")(
"characters,c", po::value<std::string>(&letters), "Enthaltene Buchstaben")(
"min-length", po::value<int>(&wordLengthMin),
"Minimale Wortlänge")("max-length", po::value<int>(&wordLengthMax), "Maximale Wortlänge")(
"line-length", po::value<int>(&lineLength),
"Maximale Zeilenlänge")("num-lines,n", po::value<int>(&numLines), "Anzahl Zeilen")(
"lowercase", po::bool_switch(&lowercase), "Ausgabe nur klein geschrieben")(
"dictfile,d", po::value<std::string>(&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<icu::UnicodeString> 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<std::mt19937::result_type> 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;
}
}