libgsjj  1
Deterministic finite automata learning library
utils.cpp
Go to the documentation of this file.
1 #include "gsjj/passive/utils.h"
2 
3 #include <algorithm>
4 #include <fstream>
5 #include <random>
6 
7 #include "MapleCOMSPS_LRB/utils/System.h"
8 
9 namespace gsjj {
10  namespace passive {
11  std::set<std::string> computeS(const std::set<std::string> &Sp, const std::set<std::string> &Sm) {
12  std::set<std::string> S;
13  std::set_union(Sp.begin(), Sp.end(), Sm.begin(), Sm.end(), std::inserter(S, S.begin()));
14  return S;
15  }
16 
17  std::set<std::string> computePrefixes(const std::set<std::string> &S) {
18  std::set<std::string> prefixes;
19  for (const auto &s : S) {
20  for (std::size_t i = 0; i <= s.size(); i++) {
21  prefixes.insert(s.substr(0, i));
22  }
23  }
24  return prefixes;
25  }
26 
27  std::set<char> computeAlphabet(const std::set<std::string> &S) {
28  std::set<char> alphabet;
29  for (const auto &s : S) {
30  for (const auto &c : s) {
31  alphabet.insert(c);
32  }
33  }
34  return alphabet;
35  }
36 
37  void readFromFile(const std::string &inputFile, std::set<std::string> &Sp, std::set<std::string> &Sm) {
38  std::ifstream in(inputFile);
39  std::string s;
40  bool accept = true;
41  while (getline(in, s)) {
42  if (s == "=====") {
43  accept = false;
44  }
45  else if (accept) {
46  Sp.insert(s);
47  }
48  else {
49  Sm.insert(s);
50  }
51  }
52  }
53 
54  void generateRandomly(unsigned int numberWords, unsigned int minLength, unsigned int maxLength, unsigned int alphabetSize, std::set<std::string> &Sp, std::set<std::string> &Sm, double SpProbability) {
55  std::default_random_engine generator(time(0));
56  std::bernoulli_distribution toAcceptDistribution(SpProbability);
57  std::uniform_int_distribution<char> symbolDistribution('a', 'a' + alphabetSize - 1);
58  std::uniform_int_distribution<unsigned int> lengthDistribution(minLength, maxLength);
59 
60  while (Sp.size() + Sm.size() < numberWords) {
61  std::string word;
62 
63  do {
64  unsigned int length = lengthDistribution(generator);
65  word.clear();
66  word.reserve(length);
67  for (std::size_t j = 0 ; j < length ; j++) {
68  word.push_back(symbolDistribution(generator));
69  }
70  } while (Sp.find(word) != Sp.end() || Sm.find(word) != Sm.end());
71 
72  if (toAcceptDistribution(generator)) {
73  Sp.insert(word);
74  }
75  else {
76  Sm.insert(word);
77  }
78  }
79  }
80 
81  double getCPUTime() {
82  return Minisat::cpuTime();
83  }
84  }
85 }
The namespace of the library.
Definition: binary.h:11
std::set< std::string > computePrefixes(const std::set< std::string > &S)
Computes .
Definition: utils.cpp:17
double getCPUTime()
Definition: utils.cpp:81
std::set< std::string > computeS(const std::set< std::string > &Sp, const std::set< std::string > &Sm)
Computes .
Definition: utils.cpp:11
void readFromFile(const std::string &inputFile, std::set< std::string > &Sp, std::set< std::string > &Sm)
Reads the and sets from a file.
Definition: utils.cpp:37
std::set< char > computeAlphabet(const std::set< std::string > &S)
Computes the alphabet from the words in .
Definition: utils.cpp:27
void generateRandomly(unsigned int numberWords, unsigned int minLength, unsigned int maxLength, unsigned int alphabetSize, std::set< std::string > &Sp, std::set< std::string > &Sm, double SpProbability=0.5)
Generates randomly a sample set.
Definition: utils.cpp:54