2017-04-26 20:35:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2017-04-26 20:43:45 +00:00
|
|
|
|
using System.Linq;
|
2017-04-26 20:35:00 +00:00
|
|
|
|
|
|
|
|
|
namespace MarkovGrams
|
|
|
|
|
{
|
|
|
|
|
class MainClass
|
|
|
|
|
{
|
2017-05-18 11:58:20 +00:00
|
|
|
|
public static int Main(string[] args)
|
2017-04-26 20:35:00 +00:00
|
|
|
|
{
|
2017-05-18 11:58:20 +00:00
|
|
|
|
if(args.Length < 1)
|
2017-04-26 20:35:00 +00:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Usage:");
|
2017-05-18 11:58:20 +00:00
|
|
|
|
Console.WriteLine(" ./MarkovGrams.exe <command>");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("Available commands:");
|
|
|
|
|
Console.WriteLine(" markov:");
|
|
|
|
|
Console.WriteLine(" Generate new words using a markov chain.");
|
|
|
|
|
Console.WriteLine(" ngrams:");
|
|
|
|
|
Console.WriteLine(" Generate raw unique n-grams");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("Type just ./MarovGrams.exe <command> to see command-specific help.");
|
|
|
|
|
return 1;
|
2017-04-26 20:35:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-18 11:58:20 +00:00
|
|
|
|
string mode = args[0];
|
|
|
|
|
string wordlistFilename;
|
|
|
|
|
int order;
|
|
|
|
|
IEnumerable<string> words, ngrams;
|
2017-04-26 20:35:00 +00:00
|
|
|
|
|
2017-05-18 11:58:20 +00:00
|
|
|
|
switch(mode)
|
|
|
|
|
{
|
|
|
|
|
case "markov":
|
|
|
|
|
if(args.Length != 5)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("markov command usage:");
|
|
|
|
|
Console.WriteLine(" ./MarkovGrams.exe markov <wordlist.txt> <order> <length> <count>");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("<wordlist.txt> The path to the wordlist to read from.");
|
|
|
|
|
Console.WriteLine("<order> The order of the n-grams to use.");
|
|
|
|
|
Console.WriteLine("<length> The length of word to generate.");
|
|
|
|
|
Console.WriteLine("<count> The number of words to generate.");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
wordlistFilename = args[1];
|
|
|
|
|
order = int.Parse(args[2]);
|
|
|
|
|
int desiredStringLength = int.Parse(args[3]);
|
|
|
|
|
int count = int.Parse(args[4]);
|
|
|
|
|
|
|
|
|
|
words = File.ReadLines(wordlistFilename).SelectMany(word => word.Trim().Split(' '));
|
|
|
|
|
ngrams = NGrams.GenerateFlat(words, order);
|
|
|
|
|
|
|
|
|
|
UnweightedMarkovChain chain = new UnweightedMarkovChain(ngrams);
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < count; i++)
|
|
|
|
|
Console.WriteLine(chain.Generate(desiredStringLength));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "ngrams":
|
|
|
|
|
if(args.Length != 3)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("ngrams command usage:");
|
|
|
|
|
Console.WriteLine(" ./MarkovGrams.exe <wordlist.txt> <order>");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("<wordlist.txt> The path to the wordlist to read from.");
|
|
|
|
|
Console.WriteLine("<order> The order of n-grams to generate.");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2017-04-26 20:35:00 +00:00
|
|
|
|
|
2017-05-18 11:58:20 +00:00
|
|
|
|
wordlistFilename = args[1];
|
|
|
|
|
order = int.Parse(args[2]);
|
|
|
|
|
words = File.ReadLines(wordlistFilename).SelectMany(word => word.Trim().Split(' '));
|
|
|
|
|
ngrams = NGrams.GenerateFlat(words, order);
|
|
|
|
|
|
|
|
|
|
foreach(string ngram in ngrams)
|
|
|
|
|
Console.WriteLine(ngram);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Console.WriteLine("Unknown command {0}.");
|
|
|
|
|
Console.WriteLine("Available commands:");
|
|
|
|
|
Console.WriteLine(" markov Generate words with a markov chain");
|
|
|
|
|
Console.WriteLine(" ngrams Generate unique ngrams from wordlists");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2017-04-26 20:35:00 +00:00
|
|
|
|
|
2017-05-18 11:58:20 +00:00
|
|
|
|
return 0;
|
2017-04-26 20:35:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|