Created
October 5, 2012 13:03
-
-
Save iodiot/3839708 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
namespace MarkVShaney | |
{ | |
class MarkVShaney | |
{ | |
private Dictionary<string, List<string>> table; | |
private string firstWord; | |
private string secondWord; | |
private Random random; | |
public MarkVShaney(string fileName) | |
{ | |
random = new Random(Guid.NewGuid().GetHashCode()); | |
table = new Dictionary<string, List<string>>(); | |
string text = File.ReadAllText(fileName); | |
string[] words = text.Split(' '); | |
int overloaded = 0; | |
int i = 0; | |
firstWord = words[i++]; | |
secondWord = words[i++]; | |
string first = firstWord; | |
string second = secondWord; | |
while (i < words.Length) | |
{ | |
string third = words[i++]; | |
string key = first + second; | |
if (!table.ContainsKey(key)) | |
table.Add(key, new List<string>()); | |
else | |
++overloaded; | |
table[key].Add(third); | |
first = second; | |
second = third; | |
} | |
System.Console.WriteLine("Words count: " + words.Length.ToString()); | |
System.Console.WriteLine("Keys count: " + table.Keys.Count.ToString()); | |
System.Console.WriteLine("Overloaded keys count: " + overloaded.ToString() + '\n'); | |
} | |
public string GenerateSomeText(int wordsCount) | |
{ | |
if (wordsCount < 3) | |
return String.Empty; | |
StringBuilder sb = new StringBuilder(); | |
sb.Append(firstWord + ' '); | |
sb.Append(secondWord + ' '); | |
wordsCount -= 2; | |
string first = firstWord; | |
string second = secondWord; | |
while (wordsCount > 0) | |
{ | |
string key = first + second; | |
if (!table.ContainsKey(key)) | |
break; | |
string third = table[key].ToArray()[random.Next(table[key].Count)]; | |
sb.Append(third + ' '); | |
first = second; | |
second = third; | |
--wordsCount; | |
} | |
return sb.ToString(); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
MarkVShaney shaney = new MarkVShaney("text.txt"); | |
System.Console.WriteLine(shaney.GenerateSomeText(1000)); | |
System.Console.Read(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment