MarkovGrams/MarkovGrams/Program.cs

95 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MarkovGrams
{
class MainClass
{
public static int Main(string[] args)
{
if(args.Length < 1)
{
Console.WriteLine("Usage:");
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;
}
string mode = args[0];
string wordlistFilename;
int order;
IEnumerable<string> words, ngrams;
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;
}
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;
}
return 0;
}
}
}