Advertisement
siniarskimar

RandomWordFromDictionary1

May 21st, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <filesystem>
  3. #include <random>
  4. #include <fstream>
  5. #include <string>
  6. #include <list>
  7. #include <limits>
  8. #include <boost/program_options.hpp>
  9.  
  10. namespace fs = std::filesystem;
  11.  
  12. class WordDictionary {
  13.     public:
  14.     WordDictionary() {}
  15.     ~WordDictionary() {
  16.         close();
  17.     }
  18.     bool open(const fs::path& dictionaryPath) noexcept {
  19.         close();
  20.         f.open(dictionaryPath);
  21.         if(!f.is_open())
  22.             return false;
  23.         while(!f.eof()) {
  24.             f.ignore(std::numeric_limits<unsigned int>::max(), '\n');
  25.             mSize++;
  26.         }
  27.         return true;
  28.     }
  29.     void close() noexcept {
  30.         if(f.is_open()) {
  31.             f.close();
  32.         }
  33.     }
  34.     std::string get(unsigned int x) noexcept {
  35.  
  36.         // After EOF flag is set, stream position will always be -1
  37.         // In that case clear it
  38.         if(f.eof()) {
  39.             f.clear();
  40.         }
  41.         f.seekg(0, f.beg);
  42.         for(unsigned int i = 0; i < x; i++) {
  43.             f.ignore(std::numeric_limits<unsigned int>::max(), '\n');
  44.         }
  45.  
  46.         std::string word;
  47.         f >> word;
  48.  
  49.         if(!word.empty()) {
  50.             if(word[word.length() - 1] == '\r') {
  51.                 word.erase(word.end() - 1);
  52.             }
  53.         }
  54.  
  55.         return word;
  56.     }
  57.     // Convinience overload for calling this->get()
  58.     std::string operator[](unsigned int x) noexcept {
  59.         return get(x);
  60.     }
  61.  
  62.     int size() noexcept {
  63.         return mSize;
  64.     }
  65.     private:
  66.     unsigned int mSize;
  67.     std::ifstream f;
  68. };
  69.  
  70. int main(int argc, char* argv[]) {
  71.     namespace po = boost::program_options;
  72.  
  73.     po::options_description optDesc("Options");
  74.     optDesc.add_options()
  75.         ("help,h", "Print help message")
  76.         ("dictionary,d", po::value<fs::path>(),"Specify dictionary file")
  77.         ("count,c", po::value<unsigned int>()->default_value(1), "Specify word count (default: 1)")
  78.         ("oneline", "Output all words seperated by spaces in one line");
  79.  
  80.     po::variables_map optVm;
  81.     po::store(po::parse_command_line(argc,argv, optDesc), optVm);
  82.     po::notify(optVm);
  83.    
  84.     if(optVm.count("help")) {
  85.         std::cout << optDesc << std::endl;
  86.         return 0;
  87.     }
  88.     if(optVm.count("dictionary") < 0) {
  89.         // stdout (cout) is the output of this program
  90.         // cerr is linked to cout
  91.         // but clog isn't
  92.         std::clog << "No dictionary specified" << std::endl;
  93.         return 1;
  94.     }
  95.  
  96.     std::random_device randomDevice;
  97.     std::mt19937 randEngine(randomDevice());
  98.     WordDictionary dict;
  99.  
  100.     unsigned int desiredWordCount = optVm["count"].as<unsigned int>();
  101.     fs::path dictionaryPath = optVm["dictionary"].as<fs::path>();
  102.     bool oneline = false;
  103.  
  104.     if(optVm.count("oneline")) {
  105.         oneline = true;
  106.     }
  107.  
  108.     if(!dict.open(dictionaryPath)) {
  109.         std::clog << "Could not open '" << dictionaryPath << '\'' << std::endl;
  110.         return 2;
  111.     }
  112.     std::uniform_int_distribution<unsigned int> randDist(1, dict.size());
  113.  
  114.     for(unsigned int i = 0; i < desiredWordCount; i++) {
  115.         std::cout << dict[randDist(randEngine)];
  116.         if(oneline) {
  117.             std::cout << ' ';
  118.         } else {
  119.             std::cout << '\n';
  120.         }
  121.         std::cout << std::flush;
  122.     }
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement