Markopy
Utilizing Markov Models for brute forcing attacks
Python.Markopy.MarkovPasswordsCLI Class Reference

Extension of Python.Markopy.Base.BaseCLI for Markov::API::MarkovPasswords. More...

Inheritance diagram for Python.Markopy.MarkovPasswordsCLI:
Collaboration diagram for Python.Markopy.MarkovPasswordsCLI:

Public Member Functions

def __init__ (self, bool add_help=True)
 initialize base CLI More...
 
def add_arguments (self)
 
def help (self)
 
def help (self)
 
def parse (self)
 
def parse (self)
 
def init_post_arguments (self)
 
def init_post_arguments (self)
 
def parse_arguments (self)
 
def parse_arguments (self)
 
def import_model (self, str filename)
 Import a model file. More...
 
def import_model (self, str filename)
 Import a model file. More...
 
def train (self, str dataset, str seperator, str output, bool output_forced=False, bool bulk=False)
 Train a model via CLI parameters. More...
 
def train (self, str dataset, str seperator, str output, bool output_forced=False, bool bulk=False)
 Train a model via CLI parameters. More...
 
def export (self, str filename)
 Export model to a file. More...
 
def export (self, str filename)
 Export model to a file. More...
 
def generate (self, str wordlist, bool bulk=False)
 Generate strings from the model. More...
 
def generate (self, str wordlist, bool bulk=False)
 Generate strings from the model. More...
 
def process (self)
 Process parameters for operation. More...
 
def process (self)
 Process parameters for operation. More...
 
def Import (str filename)
 
bool Import (std::ifstream *)
 Import a file to construct the model. More...
 
bool Import (const char *filename)
 Open a file to import with filename, and call bool Model::Import with std::ifstream. More...
 
def Export (str filename)
 
bool Export (std::ofstream *)
 Export a file of the model. More...
 
bool Export (const char *filename)
 Open a file to export with filename, and call bool Model::Export with std::ofstream. More...
 
def Train (str dataset, str seperator, int threads)
 
void Train (const char *datasetFileName, char delimiter, int threads)
 Train the model with the dataset file. More...
 
def Generate (int count, str wordlist, int minlen, int maxlen, int threads)
 
void Generate (unsigned long int n, const char *wordlistFileName, int minLen=6, int maxLen=12, int threads=20)
 Call Markov::Model::RandomWalk n times, and collect output. More...
 
std::ifstream * OpenDatasetFile (const char *filename)
 Open dataset file and return the ifstream pointer. More...
 
std::ofstream * Save (const char *filename)
 Export model to file. More...
 
void Buff (const char *str, double multiplier, bool bDontAdjustSelfLoops=true, bool bDontAdjustExtendedLoops=false)
 Buff expression of some characters in the model. More...
 
char * RandomWalk (Markov::Random::RandomEngine *randomEngine, int minSetting, int maxSetting, char *buffer)
 Do a random walk on this model. More...
 
void AdjustEdge (const char *payload, long int occurrence)
 Adjust the model with a single string. More...
 
Node< char > * StarterNode ()
 Return starter Node. More...
 
std::vector< Edge< char > * > * Edges ()
 Return a vector of all the edges in the model. More...
 
std::map< char, Node< char > * > * Nodes ()
 Return starter Node. More...
 
void OptimizeEdgeOrder ()
 Sort edges of all nodes in the model ordered by edge weights. More...
 

Static Public Member Functions

def check_import_path (str filename)
 check import path for validity More...
 
def check_import_path (str filename)
 check import path for validity More...
 
def check_corpus_path (str filename)
 check import path for validity More...
 
def check_corpus_path (str filename)
 check import path for validity More...
 
def check_export_path (str filename)
 check import path for validity More...
 
def check_export_path (str filename)
 check import path for validity More...
 

Public Attributes

 model
 
 parser
 
 parser
 
 print_help
 
 print_help
 
 args
 
 args
 

Private Member Functions

def _generate (self, wordlist)
 
void TrainThread (Markov::API::Concurrency::ThreadSharedListHandler *listhandler, char delimiter)
 A single thread invoked by the Train function. More...
 
void GenerateThread (std::mutex *outputLock, unsigned long int n, std::ofstream *wordlist, int minLen, int maxLen)
 A single thread invoked by the Generate function. More...
 

Private Attributes

std::ifstream * datasetFile
 
std::ofstream * modelSavefile
 Dataset file input of our system
More...
 
std::ofstream * outputFile
 File to save model of our system
More...
 
std::map< char, Node< char > * > nodes
 Map LeftNode is the Nodes NodeValue Map RightNode is the node pointer. More...
 
Node< char > * starterNode
 Starter Node of this model. More...
 
std::vector< Edge< char > * > edges
 A list of all edges in this model. More...
 

Detailed Description

Extension of Python.Markopy.Base.BaseCLI for Markov::API::MarkovPasswords.

adds -st/–stdout arguement to the command line.

Definition at line 17 of file mp.py.

Constructor & Destructor Documentation

◆ __init__()

def Python.Markopy.MarkovPasswordsCLI.__init__ (   self,
bool  add_help = True 
)

initialize base CLI

Parameters
add_helpdecide to overload the help function or not

Reimplemented from Python.Markopy.BaseCLI.

Definition at line 26 of file mp.py.

26  def __init__(self, add_help:bool=True):
27  "! @brief initialize model with Markov::API::MarkovPasswords"
28  super().__init__(add_help)
29  self.model = markopy.MarkovPasswords()
30 

Member Function Documentation

◆ _generate()

def Python.Markopy.MarkovPasswordsCLI._generate (   self,
  wordlist 
)
private

Definition at line 31 of file mp.py.

31  def _generate(self, wordlist):
32  "! @brief map generation function to Markov::API::MarkovPasswords::Generate"
33  self.model.Generate(int(self.args.count), wordlist, int(self.args.min), int(self.args.max), int(self.args.threads))
34 

References Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, Python.Markopy.MarkopyCLI.args, Python.Markopy.MarkovModel.Generate(), Python.CudaMarkopy.CudaModelMatrixCLI.model, Python.Markopy.BaseCLI.model, Python.Markopy.ModelMatrixCLI.model, Python.Markopy.MarkovPasswordsCLI.model, and Markov::GUI::MarkovPasswordsGUI.model().

Referenced by Python.Markopy.BaseCLI.generate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_arguments()

def Python.Markopy.AbstractTrainingModelCLI.add_arguments (   self)
inherited

Reimplemented from Python.Markopy.BaseCLI.

Reimplemented in Python.Markopy.MarkopyCLI.

Definition at line 282 of file base.py.

282  def add_arguments(self):
283  "Add command line arguements to the parser"
284  self.parser.add_argument("-o", "--output", help="Output model file. This model will be exported when done. Will be ignored for generation mode.")
285  self.parser.add_argument("-d", "--dataset", help="Dataset file to read input from for training. Will be ignored for generation mode.")
286  self.parser.add_argument("-s", "--seperator", help="Seperator character to use with training data.(character between occurrence and value)")
287  super().add_arguments()

References Python.Markopy.BaseCLI.parser.

Referenced by Python.Markopy.BaseCLI.parse(), and Python.Markopy.MarkopyCLI.parse().

Here is the caller graph for this function:

◆ AdjustEdge()

void Markov::Model< char >::AdjustEdge ( const NodeStorageType payload,
long int  occurrence 
)
inherited

Adjust the model with a single string.

Start from the starter node, and for each character, AdjustEdge the edge EdgeWeight from current node to the next, until NULL character is reached.

Then, update the edge EdgeWeight from current node, to the terminator node.

This function is used for training purposes, as it can be used for adjusting the model with each line of the corpus file.

Example Use: Create an empty model and train it with string: "testdata"

char test[] = "testdata";
model.AdjustEdge(test, 15);
Parameters
string- String that is passed from the training, and will be used to AdjustEdge the model with
occurrence- Occurrence of this string.

Definition at line 109 of file model.h.

337  {
338  NodeStorageType p = payload[0];
341  int i = 0;
342 
343  if (p == 0) return;
344  while (p != 0) {
345  e = curnode->FindEdge(p);
346  if (e == NULL) return;
347  e->AdjustEdge(occurrence);
348  curnode = e->RightNode();
349  p = payload[++i];
350  }
351 
352  e = curnode->FindEdge('\xff');
353  e->AdjustEdge(occurrence);
354  return;
355 }
Edge class used to link nodes in the model together.
Definition: edge.h:23
Node< NodeStorageType > * RightNode()
return edge's RightNode
Definition: edge.h:170
void AdjustEdge(long int offset)
Adjust the edge EdgeWeight with offset. Adds the offset parameter to the edge EdgeWeight.
Definition: edge.h:137
Node< char > * starterNode
Starter Node of this model.
Definition: model.h:198
Edge< storageType > * FindEdge(storageType repr)
Find an edge with its character representation.
Definition: node.h:260

◆ Buff()

void Markov::API::MarkovPasswords::Buff ( const char *  str,
double  multiplier,
bool  bDontAdjustSelfLoops = true,
bool  bDontAdjustExtendedLoops = false 
)
inherited

Buff expression of some characters in the model.

Parameters
strA string containing all the characters to be buffed
multiplierA constant value to buff the nodes with.
bDontAdjustSelfEdgesDo not adjust weights if target node is same as source node
bDontAdjustExtendedLoopsDo not adjust if both source and target nodes are in first parameter

Definition at line 153 of file markovPasswords.cpp.

153  {
154  std::string buffstr(str);
155  std::map< char, Node< char > * > *nodes;
156  std::map< char, Edge< char > * > *edges;
157  nodes = this->Nodes();
158  int i=0;
159  for (auto const& [repr, node] : *nodes){
160  edges = node->Edges();
161  for (auto const& [targetrepr, edge] : *edges){
162  if(buffstr.find(targetrepr)!= std::string::npos){
163  if(bDontAdjustSelfLoops && repr==targetrepr) continue;
164  if(bDontAdjustExtendedLoops){
165  if(buffstr.find(repr)!= std::string::npos){
166  continue;
167  }
168  }
169  long int weight = edge->EdgeWeight();
170  weight = weight*multiplier;
171  edge->AdjustEdge(weight);
172  }
173 
174  }
175  i++;
176  }
177 
178  this->OptimizeEdgeOrder();
179 }
std::vector< Edge< char > * > edges
A list of all edges in this model.
Definition: model.h:204
std::map< char, Node< char > * > * Nodes()
Return starter Node.
Definition: model.h:181
std::map< char, Node< char > * > nodes
Map LeftNode is the Nodes NodeValue Map RightNode is the node pointer.
Definition: model.h:193
void OptimizeEdgeOrder()
Sort edges of all nodes in the model ordered by edge weights.
Definition: model.h:265

References Markov::Edge< NodeStorageType >::AdjustEdge(), Markov::Node< storageType >::Edges(), Markov::Edge< NodeStorageType >::EdgeWeight(), Markov::Model< NodeStorageType >::Nodes(), and Markov::Model< NodeStorageType >::OptimizeEdgeOrder().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_corpus_path() [1/2]

def Python.Markopy.BaseCLI.check_corpus_path ( str  filename)
staticinherited

check import path for validity

Parameters
filenamefilename to check

Definition at line 181 of file base.py.

181  def check_corpus_path(filename : str):
182  """!
183  @brief check import path for validity
184  @param filename filename to check
185  """
186 
187  if(not os.path.isfile(filename)):
188  return False
189  return True
190 

Referenced by Python.Markopy.BaseCLI.train().

Here is the caller graph for this function:

◆ check_corpus_path() [2/2]

def Python.Markopy.BaseCLI.check_corpus_path ( str  filename)
staticinherited

check import path for validity

Parameters
filenamefilename to check

Definition at line 181 of file base.py.

181  def check_corpus_path(filename : str):
182  """!
183  @brief check import path for validity
184  @param filename filename to check
185  """
186 
187  if(not os.path.isfile(filename)):
188  return False
189  return True
190 

Referenced by Python.Markopy.BaseCLI.train().

Here is the caller graph for this function:

◆ check_export_path() [1/2]

def Python.Markopy.BaseCLI.check_export_path ( str  filename)
staticinherited

check import path for validity

Parameters
filenamefilename to check

Definition at line 192 of file base.py.

192  def check_export_path(filename : str):
193  """!
194  @brief check import path for validity
195  @param filename filename to check
196  """
197 
198  if(filename and os.path.isfile(filename)):
199  return True
200  return True
201 

Referenced by Python.Markopy.BaseCLI.train().

Here is the caller graph for this function:

◆ check_export_path() [2/2]

def Python.Markopy.BaseCLI.check_export_path ( str  filename)
staticinherited

check import path for validity

Parameters
filenamefilename to check

Definition at line 192 of file base.py.

192  def check_export_path(filename : str):
193  """!
194  @brief check import path for validity
195  @param filename filename to check
196  """
197 
198  if(filename and os.path.isfile(filename)):
199  return True
200  return True
201 

Referenced by Python.Markopy.BaseCLI.train().

Here is the caller graph for this function:

◆ check_import_path() [1/2]

def Python.Markopy.BaseCLI.check_import_path ( str  filename)
staticinherited

check import path for validity

Parameters
filenamefilename to check

Definition at line 169 of file base.py.

169  def check_import_path(filename : str):
170  """!
171  @brief check import path for validity
172  @param filename filename to check
173  """
174 
175  if(not os.path.isfile(filename)):
176  return False
177  else:
178  return True
179 

Referenced by Python.Markopy.BaseCLI.import_model().

Here is the caller graph for this function:

◆ check_import_path() [2/2]

def Python.Markopy.BaseCLI.check_import_path ( str  filename)
staticinherited

check import path for validity

Parameters
filenamefilename to check

Definition at line 169 of file base.py.

169  def check_import_path(filename : str):
170  """!
171  @brief check import path for validity
172  @param filename filename to check
173  """
174 
175  if(not os.path.isfile(filename)):
176  return False
177  else:
178  return True
179 

Referenced by Python.Markopy.BaseCLI.import_model().

Here is the caller graph for this function:

◆ Edges()

std::vector<Edge<char >*>* Markov::Model< char >::Edges ( )
inlineinherited

Return a vector of all the edges in the model.

Returns
vector of edges

Definition at line 176 of file model.h.

176 { return &edges;}

◆ Export() [1/3]

bool Markov::Model< char >::Export ( const char *  filename)
inherited

Open a file to export with filename, and call bool Model::Export with std::ofstream.

Returns
True if successful, False for incomplete models or corrupt file formats

Example Use: Export file to filename

model.Export("test.mdl");

Definition at line 166 of file model.h.

300  {
301  std::ofstream exportfile;
302  exportfile.open(filename);
303  return this->Export(&exportfile);
304 }
bool Export(std::ofstream *)
Export a file of the model.
Definition: model.h:288

◆ export() [1/2]

def Python.Markopy.BaseCLI.export (   self,
str  filename 
)
inherited

Export model to a file.

Parameters
filenamefilename to export to

Definition at line 138 of file base.py.

138  def export(self, filename : str):
139  """!
140  @brief Export model to a file
141  @param filename filename to export to
142  """
143  self.model.Export(filename)
144 

References Python.CudaMarkopy.CudaModelMatrixCLI.model, Python.Markopy.BaseCLI.model, Python.Markopy.ModelMatrixCLI.model, Python.Markopy.MarkovPasswordsCLI.model, and Markov::GUI::MarkovPasswordsGUI.model().

Referenced by Python.Markopy.BaseCLI.train().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ export() [2/2]

def Python.Markopy.BaseCLI.export (   self,
str  filename 
)
inherited

Export model to a file.

Parameters
filenamefilename to export to

Definition at line 138 of file base.py.

138  def export(self, filename : str):
139  """!
140  @brief Export model to a file
141  @param filename filename to export to
142  """
143  self.model.Export(filename)
144 

References Python.CudaMarkopy.CudaModelMatrixCLI.model, Python.Markopy.BaseCLI.model, Python.Markopy.ModelMatrixCLI.model, Python.Markopy.MarkovPasswordsCLI.model, and Markov::GUI::MarkovPasswordsGUI.model().

Referenced by Python.Markopy.BaseCLI.train().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Export() [2/3]

bool Markov::Model< char >::Export ( std::ofstream *  f)
inherited

Export a file of the model.

File contains a list of edges. Format is: Left_repr;EdgeWeight;right_repr. For more information on the format, check out the project wiki or github readme.

Iterate over this vertices, and their edges, and write them to file.

Returns
True if successful, False for incomplete models.

Example Use: Export file to ofstream

std::ofstream file("test.mdl");
model.Export(&file);

Definition at line 155 of file model.h.

288  {
290  for (std::vector<int>::size_type i = 0; i != this->edges.size(); i++) {
291  e = this->edges[i];
292  //std::cout << e->LeftNode()->NodeValue() << "," << e->EdgeWeight() << "," << e->RightNode()->NodeValue() << "\n";
293  *f << e->LeftNode()->NodeValue() << "," << e->EdgeWeight() << "," << e->RightNode()->NodeValue() << "\n";
294  }
295 
296  return true;
297 }
uint64_t EdgeWeight()
return edge's EdgeWeight.
Definition: edge.h:160
Node< NodeStorageType > * LeftNode()
return edge's LeftNode
Definition: edge.h:165
unsigned char NodeValue()
Return character representation of this node.
Definition: node.h:215
f
output file handle
Definition: model_2gram.py:16

◆ Export() [3/3]

def Python.Markopy.MarkovModel.Export ( str  filename)
inherited

Definition at line 26 of file mm.py.

26  def Export(filename : str):
27  pass
28 

◆ Generate() [1/2]

def Python.Markopy.MarkovModel.Generate ( int  count,
str  wordlist,
int  minlen,
int  maxlen,
int  threads 
)
inherited

Definition at line 34 of file mm.py.

34  def Generate(count : int, wordlist : str, minlen : int, maxlen: int, threads : int):
35  pass
36 
37 

Referenced by Python.Markopy.MarkovPasswordsCLI._generate().

Here is the caller graph for this function:

◆ generate() [1/2]

def Python.Markopy.BaseCLI.generate (   self,
str  wordlist,
bool  bulk = False 
)
inherited

Generate strings from the model.

Parameters
modelmodel instance
wordlistwordlist filename
bulkmarks bulk operation with directories

Definition at line 145 of file base.py.

145  def generate(self, wordlist : str, bulk : bool=False):
146  """!
147  @brief Generate strings from the model
148  @param model: model instance
149  @param wordlist wordlist filename
150  @param bulk marks bulk operation with directories
151  """
152  if not (wordlist or self.args.count):
153  logging.pprint("Generation mode requires -w/--wordlist and -n/--count parameters. Exiting.")
154  return False
155 
156  if(bulk and os.path.isfile(wordlist)):
157  logging.pprint(f"{wordlist} exists and will be overwritten.", 1)
158  self._generate(wordlist)
159 

References Python.CudaMarkopy.CudaModelMatrixCLI._generate(), Python.Markopy.BaseCLI._generate(), Python.Markopy.ModelMatrixCLI._generate(), Python.Markopy.MarkovPasswordsCLI._generate(), Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, and Python.Markopy.MarkopyCLI.args.

Referenced by Python.Markopy.BaseCLI.process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generate() [2/2]

def Python.Markopy.BaseCLI.generate (   self,
str  wordlist,
bool  bulk = False 
)
inherited

Generate strings from the model.

Parameters
modelmodel instance
wordlistwordlist filename
bulkmarks bulk operation with directories

Definition at line 145 of file base.py.

145  def generate(self, wordlist : str, bulk : bool=False):
146  """!
147  @brief Generate strings from the model
148  @param model: model instance
149  @param wordlist wordlist filename
150  @param bulk marks bulk operation with directories
151  """
152  if not (wordlist or self.args.count):
153  logging.pprint("Generation mode requires -w/--wordlist and -n/--count parameters. Exiting.")
154  return False
155 
156  if(bulk and os.path.isfile(wordlist)):
157  logging.pprint(f"{wordlist} exists and will be overwritten.", 1)
158  self._generate(wordlist)
159 

References Python.CudaMarkopy.CudaModelMatrixCLI._generate(), Python.Markopy.BaseCLI._generate(), Python.Markopy.ModelMatrixCLI._generate(), Python.Markopy.MarkovPasswordsCLI._generate(), Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, and Python.Markopy.MarkopyCLI.args.

Referenced by Python.Markopy.BaseCLI.process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Generate() [2/2]

void Markov::API::MarkovPasswords::Generate ( unsigned long int  n,
const char *  wordlistFileName,
int  minLen = 6,
int  maxLen = 12,
int  threads = 20 
)
inherited

Call Markov::Model::RandomWalk n times, and collect output.

Generate from model and write results to a file. a much more performance-optimized method. FastRandomWalk will reduce the runtime by %96.5 on average.

Deprecated:
See Markov::API::MatrixModel::FastRandomWalk for more information.
Parameters
n- Number of passwords to generate.
wordlistFileName- Filename to write to
minLen- Minimum password length to generate
maxLen- Maximum password length to generate
threads- number of OS threads to spawn

Definition at line 118 of file markovPasswords.cpp.

118  {
119  char* res;
120  char print[100];
121  std::ofstream wordlist;
122  wordlist.open(wordlistFileName);
123  std::mutex mlock;
124  int iterationsPerThread = n/threads;
125  int iterationsCarryOver = n%threads;
126  std::vector<std::thread*> threadsV;
127  for(int i=0;i<threads;i++){
128  threadsV.push_back(new std::thread(&Markov::API::MarkovPasswords::GenerateThread, this, &mlock, iterationsPerThread, &wordlist, minLen, maxLen));
129  }
130 
131  for(int i=0;i<threads;i++){
132  threadsV[i]->join();
133  delete threadsV[i];
134  }
135 
136  this->GenerateThread(&mlock, iterationsCarryOver, &wordlist, minLen, maxLen);
137 
138 }
void GenerateThread(std::mutex *outputLock, unsigned long int n, std::ofstream *wordlist, int minLen, int maxLen)
A single thread invoked by the Generate function.

References Markov::API::MarkovPasswords::GenerateThread().

Referenced by Markov::Markopy::BOOST_PYTHON_MODULE(), and Markov::GUI::Generate::generation().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GenerateThread()

void Markov::API::MarkovPasswords::GenerateThread ( std::mutex *  outputLock,
unsigned long int  n,
std::ofstream *  wordlist,
int  minLen,
int  maxLen 
)
privateinherited

A single thread invoked by the Generate function.

DEPRECATED: See Markov::API::MatrixModel::FastRandomWalkThread for more information. This has been replaced with a much more performance-optimized method. FastRandomWalk will reduce the runtime by %96.5 on average.

Parameters
outputLock- shared mutex lock to lock during output operation. Prevents race condition on write.
nnumber of lines to be generated by this thread
wordlistwordlistfile
minLen- Minimum password length to generate
maxLen- Maximum password length to generate

Definition at line 140 of file markovPasswords.cpp.

140  {
141  char* res = new char[maxLen+5];
142  if(n==0) return;
143 
144  Markov::Random::Marsaglia MarsagliaRandomEngine;
145  for (int i = 0; i < n; i++) {
146  this->RandomWalk(&MarsagliaRandomEngine, minLen, maxLen, res);
147  outputLock->lock();
148  *wordlist << res << "\n";
149  outputLock->unlock();
150  }
151 }
char * RandomWalk(Markov::Random::RandomEngine *randomEngine, int minSetting, int maxSetting, char *buffer)
Do a random walk on this model.
Definition: model.h:307
Implementation of Marsaglia Random Engine.
Definition: random.h:125

References Markov::Model< NodeStorageType >::RandomWalk().

Referenced by Markov::API::MarkovPasswords::Generate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ help() [1/2]

def Python.Markopy.BaseCLI.help (   self)
inherited

Reimplemented in Python.Markopy.MarkopyCLI, and Python.CudaMarkopy.CudaMarkopyCLI.

Definition at line 51 of file base.py.

51  def help(self):
52  "! @brief Handle help strings. Defaults to argparse's help"
53  self.print_help()
54 

References Python.Markopy.BaseCLI.print_help.

Referenced by Python.Markopy.MarkopyCLI.add_arguments().

Here is the caller graph for this function:

◆ help() [2/2]

def Python.Markopy.BaseCLI.help (   self)
inherited

Reimplemented in Python.Markopy.MarkopyCLI, and Python.CudaMarkopy.CudaMarkopyCLI.

Definition at line 51 of file base.py.

51  def help(self):
52  "! @brief Handle help strings. Defaults to argparse's help"
53  self.print_help()
54 

References Python.Markopy.BaseCLI.print_help.

Referenced by Python.Markopy.MarkopyCLI.add_arguments().

Here is the caller graph for this function:

◆ Import() [1/3]

bool Markov::Model< char >::Import ( const char *  filename)
inherited

Open a file to import with filename, and call bool Model::Import with std::ifstream.

Returns
True if successful, False for incomplete models or corrupt file formats

Example Use: Import a file with filename

model.Import("test.mdl");

Definition at line 137 of file model.h.

280  {
281  std::ifstream importfile;
282  importfile.open(filename);
283  return this->Import(&importfile);
284 
285 }
bool Import(std::ifstream *)
Import a file to construct the model.
Definition: model.h:216

◆ Import() [2/3]

bool Markov::Model< char >::Import ( std::ifstream *  f)
inherited

Import a file to construct the model.

File contains a list of edges. For more info on the file format, check out the wiki and github readme pages. Format is: Left_repr;EdgeWeight;right_repr

Iterate over this list, and construct nodes and edges accordingly.

Returns
True if successful, False for incomplete models or corrupt file formats

Example Use: Import a file from ifstream

std::ifstream file("test.mdl");
model.Import(&file);

Definition at line 126 of file model.h.

216  {
217  std::string cell;
218 
219  char src;
220  char target;
221  long int oc;
222 
223  while (std::getline(*f, cell)) {
224  //std::cout << "cell: " << cell << std::endl;
225  src = cell[0];
226  target = cell[cell.length() - 1];
227  char* j;
228  oc = std::strtol(cell.substr(2, cell.length() - 2).c_str(),&j,10);
229  //std::cout << oc << "\n";
233  if (this->nodes.find(src) == this->nodes.end()) {
234  srcN = new Markov::Node<NodeStorageType>(src);
235  this->nodes.insert(std::pair<char, Markov::Node<NodeStorageType>*>(src, srcN));
236  //std::cout << "Creating new node at start.\n";
237  }
238  else {
239  srcN = this->nodes.find(src)->second;
240  }
241 
242  if (this->nodes.find(target) == this->nodes.end()) {
243  targetN = new Markov::Node<NodeStorageType>(target);
244  this->nodes.insert(std::pair<char, Markov::Node<NodeStorageType>*>(target, targetN));
245  //std::cout << "Creating new node at end.\n";
246  }
247  else {
248  targetN = this->nodes.find(target)->second;
249  }
250  e = srcN->Link(targetN);
251  e->AdjustEdge(oc);
252  this->edges.push_back(e);
253 
254  //std::cout << int(srcN->NodeValue()) << " --" << e->EdgeWeight() << "--> " << int(targetN->NodeValue()) << "\n";
255 
256 
257  }
258 
259  this->OptimizeEdgeOrder();
260 
261  return true;
262 }
Edge< storageType > * Link(Node< storageType > *)
Link this node with another, with this node as its source.
Definition: node.h:220

◆ Import() [3/3]

def Python.Markopy.MarkovModel.Import ( str  filename)
inherited

Definition at line 22 of file mm.py.

22  def Import(filename : str):
23  pass
24 

◆ import_model() [1/2]

def Python.Markopy.BaseCLI.import_model (   self,
str  filename 
)
inherited

Import a model file.

Parameters
filenamefilename to import

Definition at line 77 of file base.py.

77  def import_model(self, filename : str):
78  """!
79  @brief Import a model file
80  @param filename filename to import
81  """
82  logging.pprint("Importing model file.", 1)
83 
84  if not self.check_import_path(filename):
85  logging.pprint(f"Model file at {filename} not found. Check the file path, or working directory")
86  return False
87 
88  self.model.Import(filename)
89  logging.pprint("Model imported successfully.", 2)
90  return True
91 
92 
93 

References Python.Markopy.BaseCLI.check_import_path(), Python.CudaMarkopy.CudaModelMatrixCLI.model, Python.Markopy.BaseCLI.model, Python.Markopy.ModelMatrixCLI.model, Python.Markopy.MarkovPasswordsCLI.model, and Markov::GUI::MarkovPasswordsGUI.model().

Referenced by Python.Markopy.BaseCLI.process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ import_model() [2/2]

def Python.Markopy.BaseCLI.import_model (   self,
str  filename 
)
inherited

Import a model file.

Parameters
filenamefilename to import

Definition at line 77 of file base.py.

77  def import_model(self, filename : str):
78  """!
79  @brief Import a model file
80  @param filename filename to import
81  """
82  logging.pprint("Importing model file.", 1)
83 
84  if not self.check_import_path(filename):
85  logging.pprint(f"Model file at {filename} not found. Check the file path, or working directory")
86  return False
87 
88  self.model.Import(filename)
89  logging.pprint("Model imported successfully.", 2)
90  return True
91 
92 
93 

References Python.Markopy.BaseCLI.check_import_path(), Python.CudaMarkopy.CudaModelMatrixCLI.model, Python.Markopy.BaseCLI.model, Python.Markopy.ModelMatrixCLI.model, Python.Markopy.MarkovPasswordsCLI.model, and Markov::GUI::MarkovPasswordsGUI.model().

Referenced by Python.Markopy.BaseCLI.process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_post_arguments() [1/2]

def Python.Markopy.BaseCLI.init_post_arguments (   self)
inherited

Reimplemented in Python.Markopy.ModelMatrixCLI, Python.Markopy.MarkopyCLI, Python.CudaMarkopy.CudaModelMatrixCLI, and Python.Markopy.MarkopyCLI.

Definition at line 62 of file base.py.

62  def init_post_arguments(self):
63  "! @brief set up stuff that is collected from command line arguements"
64  logging.VERBOSITY = 0
65  try:
66  if self.args.verbosity:
67  logging.VERBOSITY = self.args.verbosity
68  logging.pprint(f"Verbosity set to {self.args.verbosity}.", 2)
69  except:
70  pass
71 

References Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, and Python.Markopy.MarkopyCLI.args.

Referenced by Python.Markopy.BaseCLI.parse(), and Python.Markopy.MarkopyCLI.parse().

Here is the caller graph for this function:

◆ init_post_arguments() [2/2]

def Python.Markopy.BaseCLI.init_post_arguments (   self)
inherited

Reimplemented in Python.Markopy.ModelMatrixCLI, Python.Markopy.MarkopyCLI, Python.CudaMarkopy.CudaModelMatrixCLI, and Python.Markopy.MarkopyCLI.

Definition at line 62 of file base.py.

62  def init_post_arguments(self):
63  "! @brief set up stuff that is collected from command line arguements"
64  logging.VERBOSITY = 0
65  try:
66  if self.args.verbosity:
67  logging.VERBOSITY = self.args.verbosity
68  logging.pprint(f"Verbosity set to {self.args.verbosity}.", 2)
69  except:
70  pass
71 

References Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, and Python.Markopy.MarkopyCLI.args.

Referenced by Python.Markopy.BaseCLI.parse(), and Python.Markopy.MarkopyCLI.parse().

Here is the caller graph for this function:

◆ Nodes()

std::map<char , Node<char >*>* Markov::Model< char >::Nodes ( )
inlineinherited

Return starter Node.

Returns
starter node with 00 NodeValue

Definition at line 181 of file model.h.

181 { return &nodes;}

◆ OpenDatasetFile()

std::ifstream * Markov::API::MarkovPasswords::OpenDatasetFile ( const char *  filename)
inherited

Open dataset file and return the ifstream pointer.

Parameters
filename- Filename to open
Returns
ifstream* to the the dataset file

Definition at line 51 of file markovPasswords.cpp.

51  {
52 
53  std::ifstream* datasetFile;
54 
55  std::ifstream newFile(filename);
56 
57  datasetFile = &newFile;
58 
59  this->Import(datasetFile);
60  return datasetFile;
61 }

References Markov::Model< NodeStorageType >::Import().

Here is the call graph for this function:

◆ OptimizeEdgeOrder()

void Markov::Model< char >::OptimizeEdgeOrder
inherited

Sort edges of all nodes in the model ordered by edge weights.

Definition at line 186 of file model.h.

265  {
266  for (std::pair<unsigned char, Markov::Node<NodeStorageType>*> const& x : this->nodes) {
267  //std::cout << "Total edges in EdgesV: " << x.second->edgesV.size() << "\n";
268  std::sort (x.second->edgesV.begin(), x.second->edgesV.end(), [](Edge<NodeStorageType> *lhs, Edge<NodeStorageType> *rhs)->bool{
269  return lhs->EdgeWeight() > rhs->EdgeWeight();
270  });
271  //for(int i=0;i<x.second->edgesV.size();i++)
272  // std::cout << x.second->edgesV[i]->EdgeWeight() << ", ";
273  //std::cout << "\n";
274  }
275  //std::cout << "Total number of nodes: " << this->nodes.size() << std::endl;
276  //std::cout << "Total number of edges: " << this->edges.size() << std::endl;
277 }

◆ parse() [1/2]

def Python.Markopy.BaseCLI.parse (   self)
inherited

◆ parse() [2/2]

def Python.Markopy.BaseCLI.parse (   self)
inherited

◆ parse_arguments() [1/2]

def Python.Markopy.BaseCLI.parse_arguments (   self)
inherited

Definition at line 73 of file base.py.

73  def parse_arguments(self):
74  "! @brief trigger parser"
75  self.args = self.parser.parse_known_args()[0]
76 

Referenced by Python.Markopy.BaseCLI.parse(), and Python.Markopy.MarkopyCLI.parse().

Here is the caller graph for this function:

◆ parse_arguments() [2/2]

def Python.Markopy.BaseCLI.parse_arguments (   self)
inherited

Definition at line 73 of file base.py.

73  def parse_arguments(self):
74  "! @brief trigger parser"
75  self.args = self.parser.parse_known_args()[0]
76 

Referenced by Python.Markopy.BaseCLI.parse(), and Python.Markopy.MarkopyCLI.parse().

Here is the caller graph for this function:

◆ process() [1/2]

def Python.Markopy.BaseCLI.process (   self)
inherited

Process parameters for operation.

Reimplemented in Python.Markopy.MarkopyCLI.

Definition at line 202 of file base.py.

202  def process(self):
203  """!
204  @brief Process parameters for operation
205  """
206  if(self.args.bulk):
207  logging.pprint(f"Bulk mode operation chosen.", 4)
208  if (self.args.mode.lower() == "train"):
209  if (os.path.isdir(self.args.output) and not os.path.isfile(self.args.output)) and (os.path.isdir(self.args.dataset) and not os.path.isfile(self.args.dataset)):
210  corpus_list = os.listdir(self.args.dataset)
211  for corpus in corpus_list:
212  self.import_model(self.args.input)
213  logging.pprint(f"Training {self.args.input} with {corpus}", 2)
214  output_file_name = corpus
215  model_extension = ""
216  if "." in self.args.input:
217  model_extension = self.args.input.split(".")[-1]
218  self.train(f"{self.args.dataset}/{corpus}", self.args.seperator, f"{self.args.output}/{corpus}.{model_extension}", output_forced=True, bulk=True)
219  else:
220  logging.pprint("In bulk training, output and dataset should be a directory.")
221  exit(1)
222 
223  elif (self.args.mode.lower() == "generate"):
224  if (os.path.isdir(self.args.wordlist) and not os.path.isfile(self.args.wordlist)) and (os.path.isdir(self.args.input) and not os.path.isfile(self.args.input)):
225  model_list = os.listdir(self.args.input)
226  print(model_list)
227  for input in model_list:
228  logging.pprint(f"Generating from {self.args.input}/{input} to {self.args.wordlist}/{input}.txt", 2)
229  self.import_model(f"{self.args.input}/{input}")
230  model_base = input
231  if "." in self.args.input:
232  model_base = input.split(".")[1]
233  self.generate(f"{self.args.wordlist}/{model_base}.txt", bulk=True)
234  else:
235  logging.pprint("In bulk generation, input and wordlist should be directory.")
236 
237  else:
238  self.import_model(self.args.input)
239  if (self.args.mode.lower() == "generate"):
240  self.generate(self.args.wordlist)
241 
242 
243  elif (self.args.mode.lower() == "train"):
244  self.train(self.args.dataset, self.args.seperator, self.args.output, output_forced=True)
245 
246 
247  elif(self.args.mode.lower() == "combine"):
248  self.train(self.args.dataset, self.args.seperator, self.args.output)
249  self.generate(self.args.wordlist)
250 
251 
252  else:
253  logging.pprint("Invalid mode arguement given.")
254  logging.pprint("Accepted modes: 'Generate', 'Train', 'Combine'")
255  exit(5)
256 

References Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, Python.Markopy.MarkopyCLI.args, Python.Markopy.BaseCLI.generate(), Python.Markopy.BaseCLI.import_model(), Markov::GUI::Generate.train(), Markov::GUI::Train.train(), and Python.Markopy.BaseCLI.train().

Here is the call graph for this function:

◆ process() [2/2]

def Python.Markopy.BaseCLI.process (   self)
inherited

Process parameters for operation.

Reimplemented in Python.Markopy.MarkopyCLI.

Definition at line 202 of file base.py.

202  def process(self):
203  """!
204  @brief Process parameters for operation
205  """
206  if(self.args.bulk):
207  logging.pprint(f"Bulk mode operation chosen.", 4)
208  if (self.args.mode.lower() == "train"):
209  if (os.path.isdir(self.args.output) and not os.path.isfile(self.args.output)) and (os.path.isdir(self.args.dataset) and not os.path.isfile(self.args.dataset)):
210  corpus_list = os.listdir(self.args.dataset)
211  for corpus in corpus_list:
212  self.import_model(self.args.input)
213  logging.pprint(f"Training {self.args.input} with {corpus}", 2)
214  output_file_name = corpus
215  model_extension = ""
216  if "." in self.args.input:
217  model_extension = self.args.input.split(".")[-1]
218  self.train(f"{self.args.dataset}/{corpus}", self.args.seperator, f"{self.args.output}/{corpus}.{model_extension}", output_forced=True, bulk=True)
219  else:
220  logging.pprint("In bulk training, output and dataset should be a directory.")
221  exit(1)
222 
223  elif (self.args.mode.lower() == "generate"):
224  if (os.path.isdir(self.args.wordlist) and not os.path.isfile(self.args.wordlist)) and (os.path.isdir(self.args.input) and not os.path.isfile(self.args.input)):
225  model_list = os.listdir(self.args.input)
226  print(model_list)
227  for input in model_list:
228  logging.pprint(f"Generating from {self.args.input}/{input} to {self.args.wordlist}/{input}.txt", 2)
229  self.import_model(f"{self.args.input}/{input}")
230  model_base = input
231  if "." in self.args.input:
232  model_base = input.split(".")[1]
233  self.generate(f"{self.args.wordlist}/{model_base}.txt", bulk=True)
234  else:
235  logging.pprint("In bulk generation, input and wordlist should be directory.")
236 
237  else:
238  self.import_model(self.args.input)
239  if (self.args.mode.lower() == "generate"):
240  self.generate(self.args.wordlist)
241 
242 
243  elif (self.args.mode.lower() == "train"):
244  self.train(self.args.dataset, self.args.seperator, self.args.output, output_forced=True)
245 
246 
247  elif(self.args.mode.lower() == "combine"):
248  self.train(self.args.dataset, self.args.seperator, self.args.output)
249  self.generate(self.args.wordlist)
250 
251 
252  else:
253  logging.pprint("Invalid mode arguement given.")
254  logging.pprint("Accepted modes: 'Generate', 'Train', 'Combine'")
255  exit(5)
256 

References Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, Python.Markopy.MarkopyCLI.args, Python.Markopy.BaseCLI.generate(), Python.Markopy.BaseCLI.import_model(), Markov::GUI::Generate.train(), Markov::GUI::Train.train(), and Python.Markopy.BaseCLI.train().

Here is the call graph for this function:

◆ RandomWalk()

char * Markov::Model< char >::RandomWalk ( Markov::Random::RandomEngine randomEngine,
int  minSetting,
int  maxSetting,
NodeStorageType buffer 
)
inherited

Do a random walk on this model.

Start from the starter node, on each node, invoke RandomNext using the random engine on current node, until terminator node is reached. If terminator node is reached before minimum length criateria is reached, ignore the last selection and re-invoke randomNext

If maximum length criteria is reached but final node is not, cut off the generation and proceed to the final node. This function takes Markov::Random::RandomEngine as a parameter to generate pseudo random numbers from

This library is shipped with two random engines, Marsaglia and Mersenne. While mersenne output is higher in entropy, most use cases don't really need super high entropy output, so Markov::Random::Marsaglia is preferable for better performance.

This function WILL NOT reallocate buffer. Make sure no out of bound writes are happening via maximum length criteria.

Example Use: Generate 10 lines, with 5 to 10 characters, and print the output. Use Marsaglia

Model.import("model.mdl");
char* res = new char[11];
Markov::Random::Marsaglia MarsagliaRandomEngine;
for (int i = 0; i < 10; i++) {
this->RandomWalk(&MarsagliaRandomEngine, 5, 10, res);
std::cout << res << "\n";
}
Model()
Initialize a model with only start and end nodes.
Definition: model.h:210
Parameters
randomEngineRandom Engine to use for the random walks. For examples, see Markov::Random::Mersenne and Markov::Random::Marsaglia
minSettingMinimum number of characters to generate
maxSettingMaximum number of character to generate
bufferbuffer to write the result to
Returns
Null terminated string that was generated.

Definition at line 86 of file model.h.

307  {
309  int len = 0;
311  while (true) {
312  temp_node = n->RandomNext(randomEngine);
313  if (len >= maxSetting) {
314  break;
315  }
316  else if ((temp_node == NULL) && (len < minSetting)) {
317  continue;
318  }
319 
320  else if (temp_node == NULL){
321  break;
322  }
323 
324  n = temp_node;
325 
326  buffer[len++] = n->NodeValue();
327  }
328 
329  //null terminate the string
330  buffer[len] = 0x00;
331 
332  //do something with the generated string
333  return buffer; //for now
334 }
Node< storageType > * RandomNext(Markov::Random::RandomEngine *randomEngine)
Chose a random node from the list of edges, with regards to its EdgeWeight, and TraverseNode to that.
Definition: node.h:234

◆ Save()

std::ofstream * Markov::API::MarkovPasswords::Save ( const char *  filename)
inherited

Export model to file.

Parameters
filename- Export filename.
Returns
std::ofstream* of the exported file.

Definition at line 106 of file markovPasswords.cpp.

106  {
107  std::ofstream* exportFile;
108 
109  std::ofstream newFile(filename);
110 
111  exportFile = &newFile;
112 
113  this->Export(exportFile);
114  return exportFile;
115 }

References Markov::Model< NodeStorageType >::Export().

Here is the call graph for this function:

◆ StarterNode()

Node<char >* Markov::Model< char >::StarterNode ( )
inlineinherited

Return starter Node.

Returns
starter node with 00 NodeValue

Definition at line 171 of file model.h.

171 { return starterNode;}

◆ Train() [1/2]

void Markov::API::MarkovPasswords::Train ( const char *  datasetFileName,
char  delimiter,
int  threads 
)
inherited

Train the model with the dataset file.

Parameters
datasetFileName- Ifstream* to the dataset. If null, use class member
delimiter- a character, same as the delimiter in dataset content
threads- number of OS threads to spawn
mp.Import("models/2gram.mdl");
mp.Train("password.corpus");
Markov::Model with char represented nodes.
Definition: mp.py:1

Definition at line 65 of file markovPasswords.cpp.

65  {
66  signal(SIGINT, intHandler);
67  Markov::API::Concurrency::ThreadSharedListHandler listhandler(datasetFileName);
68  auto start = std::chrono::high_resolution_clock::now();
69 
70  std::vector<std::thread*> threadsV;
71  for(int i=0;i<threads;i++){
72  threadsV.push_back(new std::thread(&Markov::API::MarkovPasswords::TrainThread, this, &listhandler, delimiter));
73  }
74 
75  for(int i=0;i<threads;i++){
76  threadsV[i]->join();
77  delete threadsV[i];
78  }
79  auto finish = std::chrono::high_resolution_clock::now();
80  std::chrono::duration<double> elapsed = finish - start;
81  std::cout << "Elapsed time: " << elapsed.count() << " s\n";
82 
83 }
Simple class for managing shared access to file.
void TrainThread(Markov::API::Concurrency::ThreadSharedListHandler *listhandler, char delimiter)
A single thread invoked by the Train function.
void intHandler(int dummy)

References intHandler(), Markov::API::Concurrency::ThreadSharedListHandler::ThreadSharedListHandler(), and Markov::API::MarkovPasswords::TrainThread().

Referenced by Markov::Markopy::BOOST_PYTHON_MODULE(), Markov::GUI::Generate::train(), Markov::GUI::Train::train(), and Markov::API::ModelMatrix::Train().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ train() [1/2]

def Python.Markopy.BaseCLI.train (   self,
str  dataset,
str  seperator,
str  output,
bool  output_forced = False,
bool  bulk = False 
)
inherited

Train a model via CLI parameters.

Parameters
modelModel instance
datasetfilename for the dataset
seperatorseperator used with the dataset
outputoutput filename
output_forcedforce overwrite
bulkmarks bulk operation with directories

Definition at line 94 of file base.py.

94  def train(self, dataset : str, seperator : str, output : str, output_forced : bool=False, bulk : bool=False):
95  """!
96  @brief Train a model via CLI parameters
97  @param model Model instance
98  @param dataset filename for the dataset
99  @param seperator seperator used with the dataset
100  @param output output filename
101  @param output_forced force overwrite
102  @param bulk marks bulk operation with directories
103  """
104  logging.pprint("Training.")
105 
106  if not (dataset and seperator and (output or not output_forced)):
107  logging.pprint(f"Training mode requires -d/--dataset{', -o/--output' if output_forced else''} and -s/--seperator parameters. Exiting.")
108  return False
109 
110  if not bulk and not self.check_corpus_path(dataset):
111  logging.pprint(f"{dataset} doesn't exists. Check the file path, or working directory")
112  return False
113 
114  if not self.check_export_path(output):
115  logging.pprint(f"Cannot create output at {output}")
116  return False
117 
118  if(seperator == '\\t'):
119  logging.pprint("Escaping seperator.", 3)
120  seperator = '\t'
121 
122  if(len(seperator)!=1):
123  logging.pprint(f'Delimiter must be a single character, and "{seperator}" is not accepted.')
124  exit(4)
125 
126  logging.pprint(f'Starting training.', 3)
127  self.model.Train(dataset,seperator, int(self.args.threads))
128  logging.pprint(f'Training completed.', 2)
129 
130  if(output):
131  logging.pprint(f'Exporting model to {output}', 2)
132  self.export(output)
133  else:
134  logging.pprint(f'Model will not be exported.', 1)
135 
136  return True
137 

References Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, Python.Markopy.MarkopyCLI.args, Python.Markopy.BaseCLI.check_corpus_path(), Python.Markopy.BaseCLI.check_export_path(), Python.Markopy.BaseCLI.export(), Python.CudaMarkopy.CudaModelMatrixCLI.model, Python.Markopy.BaseCLI.model, Python.Markopy.ModelMatrixCLI.model, Python.Markopy.MarkovPasswordsCLI.model, and Markov::GUI::MarkovPasswordsGUI.model().

Referenced by Python.Markopy.BaseCLI.process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ train() [2/2]

def Python.Markopy.BaseCLI.train (   self,
str  dataset,
str  seperator,
str  output,
bool  output_forced = False,
bool  bulk = False 
)
inherited

Train a model via CLI parameters.

Parameters
modelModel instance
datasetfilename for the dataset
seperatorseperator used with the dataset
outputoutput filename
output_forcedforce overwrite
bulkmarks bulk operation with directories

Definition at line 94 of file base.py.

94  def train(self, dataset : str, seperator : str, output : str, output_forced : bool=False, bulk : bool=False):
95  """!
96  @brief Train a model via CLI parameters
97  @param model Model instance
98  @param dataset filename for the dataset
99  @param seperator seperator used with the dataset
100  @param output output filename
101  @param output_forced force overwrite
102  @param bulk marks bulk operation with directories
103  """
104  logging.pprint("Training.")
105 
106  if not (dataset and seperator and (output or not output_forced)):
107  logging.pprint(f"Training mode requires -d/--dataset{', -o/--output' if output_forced else''} and -s/--seperator parameters. Exiting.")
108  return False
109 
110  if not bulk and not self.check_corpus_path(dataset):
111  logging.pprint(f"{dataset} doesn't exists. Check the file path, or working directory")
112  return False
113 
114  if not self.check_export_path(output):
115  logging.pprint(f"Cannot create output at {output}")
116  return False
117 
118  if(seperator == '\\t'):
119  logging.pprint("Escaping seperator.", 3)
120  seperator = '\t'
121 
122  if(len(seperator)!=1):
123  logging.pprint(f'Delimiter must be a single character, and "{seperator}" is not accepted.')
124  exit(4)
125 
126  logging.pprint(f'Starting training.', 3)
127  self.model.Train(dataset,seperator, int(self.args.threads))
128  logging.pprint(f'Training completed.', 2)
129 
130  if(output):
131  logging.pprint(f'Exporting model to {output}', 2)
132  self.export(output)
133  else:
134  logging.pprint(f'Model will not be exported.', 1)
135 
136  return True
137 

References Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, Python.Markopy.MarkopyCLI.args, Python.Markopy.BaseCLI.check_corpus_path(), Python.Markopy.BaseCLI.check_export_path(), Python.Markopy.BaseCLI.export(), Python.CudaMarkopy.CudaModelMatrixCLI.model, Python.Markopy.BaseCLI.model, Python.Markopy.ModelMatrixCLI.model, Python.Markopy.MarkovPasswordsCLI.model, and Markov::GUI::MarkovPasswordsGUI.model().

Referenced by Python.Markopy.BaseCLI.process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Train() [2/2]

def Python.Markopy.MarkovModel.Train ( str  dataset,
str  seperator,
int  threads 
)
inherited

Definition at line 30 of file mm.py.

30  def Train(dataset: str, seperator : str, threads : int):
31  pass
32 

◆ TrainThread()

void Markov::API::MarkovPasswords::TrainThread ( Markov::API::Concurrency::ThreadSharedListHandler listhandler,
char  delimiter 
)
privateinherited

A single thread invoked by the Train function.

Parameters
listhandler- Listhandler class to read corpus from
delimiter- a character, same as the delimiter in dataset content

Definition at line 85 of file markovPasswords.cpp.

85  {
86  char format_str[] ="%ld,%s";
87  format_str[3]=delimiter;
88  std::string line;
89  while (listhandler->next(&line) && keepRunning) {
90  long int oc;
91  if (line.size() > 100) {
92  line = line.substr(0, 100);
93  }
94  char* linebuf = new char[line.length()+5];
95 #ifdef _WIN32
96  sscanf_s(line.c_str(), "%ld,%s", &oc, linebuf, line.length()+5); //<== changed format_str to-> "%ld,%s"
97 #else
98  sscanf(line.c_str(), format_str, &oc, linebuf);
99 #endif
100  this->AdjustEdge((const char*)linebuf, oc);
101  delete linebuf;
102  }
103 }
bool next(std::string *line)
Read the next line from the file.
void AdjustEdge(const char *payload, long int occurrence)
Adjust the model with a single string.
Definition: model.h:337
static volatile int keepRunning

References Markov::Model< NodeStorageType >::AdjustEdge(), keepRunning, and Markov::API::Concurrency::ThreadSharedListHandler::next().

Referenced by Markov::API::MarkovPasswords::Train().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ args [1/2]

◆ args [2/2]

◆ datasetFile

std::ifstream* Markov::API::MarkovPasswords::datasetFile
privateinherited

Definition at line 123 of file markovPasswords.h.

◆ edges

std::vector<Edge<char >*> Markov::Model< char >::edges
privateinherited

A list of all edges in this model.

Definition at line 204 of file model.h.

◆ model

◆ modelSavefile

std::ofstream* Markov::API::MarkovPasswords::modelSavefile
privateinherited

Dataset file input of our system

Definition at line 124 of file markovPasswords.h.

◆ nodes

std::map<char , Node<char >*> Markov::Model< char >::nodes
privateinherited

Map LeftNode is the Nodes NodeValue Map RightNode is the node pointer.

Definition at line 193 of file model.h.

◆ outputFile

std::ofstream* Markov::API::MarkovPasswords::outputFile
privateinherited

File to save model of our system

Definition at line 125 of file markovPasswords.h.

◆ parser [1/2]

◆ parser [2/2]

◆ print_help [1/2]

Python.Markopy.BaseCLI.print_help
inherited

Definition at line 39 of file base.py.

Referenced by Python.Markopy.BaseCLI.help(), and Python.Markopy.MarkopyCLI.help().

◆ print_help [2/2]

Python.Markopy.BaseCLI.print_help
inherited

Definition at line 39 of file base.py.

Referenced by Python.Markopy.BaseCLI.help(), and Python.Markopy.MarkopyCLI.help().

◆ starterNode

Node<char >* Markov::Model< char >::starterNode
privateinherited

Starter Node of this model.

Definition at line 198 of file model.h.


The documentation for this class was generated from the following file: