public string Generate(int len) { string result = ""; List <string> prefix = new List <string>(MaxChainLength); for (var i = 0; i < MaxChainLength; i++) { prefix.Add(null); } for (var i = 0; i < len; i++) { var cmpPrefix = new MarkovPrefix(prefix); if (Segments.ContainsKey(cmpPrefix)) { var match = Segments[cmpPrefix]; var str = match.GetRandomPostfix(); prefix.RemoveAt(0); prefix.Add(str); result += str + " "; if (RESET_ON.Contains(str)) { for (var reset = 0; reset < MaxChainLength; reset++) { prefix[reset] = (null); } } } else { Console.Out.WriteLine("NULL M:: " + string.Join(",", prefix.Select(prf => @"""" + prf + @""""))); } } return(result); }
public static IEnumerable <MarkovPrefix> Subprefixes(MarkovPrefix prfx) { for (var i = 1; i < prfx.Prefix.Length; i++) { yield return(new MarkovPrefix(prfx.Prefix.Skip(i))); } }
public static MarkovChain ReadFromStream(StreamReader reader) { MarkovChain chain = null; while (!reader.EndOfStream) { var prefixline = reader.ReadLine(); var prefixes = prefixline.Split(','); if (chain == null) { chain = new MarkovChain(prefixes.Length); } var prefix = new MarkovPrefix(prefixes.Select(x => x[0] == '"' ? x.Substring(1, x.Length - 2) : null)); List <string> Postfixes = new List <string>(); var nextLine = reader.ReadLine(); while (nextLine != "-") { Postfixes.Add(nextLine); nextLine = reader.ReadLine(); } chain.Segments.Add(prefix, MarkovChainSegment.GenerateSegment(Postfixes)); } return(chain); }
public void AddSegment(IEnumerable <string> prefix, string postfix) { MarkovPrefix prfx = new MarkovPrefix(prefix); if (Segments.ContainsKey(prfx)) { Segments[prfx].Add(postfix); } else { Segments.Add(prfx, new MarkovChainSegment(postfix)); } }
public bool PartialMatch(MarkovPrefix oprefix) { for (var i = 0; i < oprefix.Prefix.Length; i++) { string left = oprefix.Prefix[i]; string right = Prefix[i + (Prefix.Length - oprefix.Prefix.Length)]; if ((left == null || right == null) && !(left == null && right == null)) { return(false); } else if (!(left == null && right == null) && !left.Equals(right)) { return(false); } } return(true); }
public IEnumerable <MarkovChain> BuildSubChains() { var skip = 1; for (var length = this.MaxChainLength - 1; length >= 1; length--) { MarkovChain tempChain = new MarkovChain(length); var grp = this.Segments.GroupBy((kv) => new MarkovPrefix(kv.Key.Prefix.Skip(skip))); foreach (var SegmentGroup in grp) { var prefix = new MarkovPrefix(SegmentGroup.First().Key.Prefix.Skip(skip)); MarkovChainSegment newSegment = new MarkovChainSegment(""); newSegment.Postfixes.Clear(); foreach (var Segment in SegmentGroup) { newSegment = MarkovChainSegment.Merge(newSegment, Segment.Value); } tempChain.Segments.Add(prefix, newSegment); } yield return(tempChain); skip++; } }