tipplektionen/main.cpp

107 lines
3.3 KiB
C++
Raw Normal View History

2023-03-09 15:21:26 +01:00
#include <algorithm>
#include <filesystem>
2023-03-08 21:01:11 +01:00
#include <fstream>
#include <iostream>
2023-03-09 15:21:26 +01:00
#include <locale>
#include <random>
2023-03-08 21:01:11 +01:00
#include <string>
2023-03-09 15:21:26 +01:00
#include <vector>
2023-03-08 21:01:11 +01:00
#include <boost/program_options.hpp>
2023-03-09 15:21:26 +01:00
#include <unicode/unistr.h>
#include <unicode/ustream.h>
2023-03-08 21:01:11 +01:00
namespace po = boost::program_options;
int main(int argc, char *argv[])
{
2023-03-08 21:16:15 +01:00
std::string letters;
2023-03-09 15:21:26 +01:00
int wordLengthMin{1};
int wordLengthMax{1024};
int lineLength{60};
int numLines{4};
std::string wordsFile{"/usr/share/dict/ngerman"};
bool lowercase{false};
2023-03-08 21:16:15 +01:00
2023-03-08 21:01:11 +01:00
po::options_description desc("Erlaubte Optionen");
2023-03-09 15:21:26 +01:00
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");
2023-03-08 21:01:11 +01:00
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
2023-03-09 15:21:26 +01:00
if (vm.contains("help")) {
2023-03-08 21:01:11 +01:00
std::cout << desc << std::endl;
return 1;
}
2023-03-09 15:21:26 +01:00
std::ifstream infile(wordsFile);
2023-03-08 21:01:11 +01:00
std::string line;
2023-03-09 15:21:26 +01:00
icu::UnicodeString uline;
std::vector<icu::UnicodeString> words;
icu::UnicodeString ucLetters = icu::UnicodeString::fromUTF8(letters);
icu::UnicodeString ucLettersUpper = icu::UnicodeString::fromUTF8(letters).toUpper();
2023-03-08 21:01:11 +01:00
long counter{0};
2023-03-09 15:21:26 +01:00
if (!std::filesystem::exists(wordsFile)) {
std::cerr << "Datei existiert nicht: " << wordsFile << std::endl;
return 1;
}
2023-03-08 21:01:11 +01:00
while (std::getline(infile, line)) {
2023-03-09 15:21:26 +01:00
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);
2023-03-08 21:01:11 +01:00
counter++;
}
2023-03-09 15:21:26 +01:00
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;
}
2023-03-08 21:01:11 +01:00
}