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 static MarkovChain Exclusion(MarkovChain source, MarkovChain exclude) { var work = source.Clone(); foreach (var seg in exclude.Segments) { if (work.Segments.ContainsKey(seg.Key)) { var workseg = work.Segments[seg.Key]; var excl = seg.Value; foreach (var postfix in excl.Postfixes) { if (workseg.Postfixes.ContainsKey(postfix.Key)) { workseg.Postfixes[postfix.Key] -= postfix.Value; if (workseg.Postfixes[postfix.Key] <= 0) { workseg.Postfixes.Remove(postfix.Key); } } } if (workseg.Postfixes.Count == 0) { work.Segments.Remove(seg.Key); } } } return(work); }
public MarkovChain Clone() { MarkovChain tmp = new MarkovChain(MaxChainLength); foreach (var seg in Segments) { tmp.Segments.Add(new MarkovPrefix(seg.Key.Prefix), seg.Value.Clone()); } return(tmp); }
public void Extend(MarkovChain otherChain) { foreach (var seg in otherChain.Segments) { if (Segments.ContainsKey(seg.Key)) { MarkovChainSegment.Merge(Segments[seg.Key], seg.Value); } else { Segments.Add(seg.Key, seg.Value); } } }
public static double PercentageMatch(MarkovChain test, MarkovChain source) { int totalSegments = test.Segments.Count; double perSegment = 1.0 / totalSegments; var total = 0.0; foreach (var segment in test.Segments) { var prefix = segment.Key; if (source.Segments.ContainsKey(prefix)) { var sourceSegment = source.Segments[prefix]; total += perSegment * MarkovChainSegment.ComparePercentage(sourceSegment, source.Segments[prefix]); } } return(total); }
public static void SaveToStream(StreamWriter writer, MarkovChain chain) { foreach (var segment in chain.Segments) { writer.WriteLine(string.Join(",", segment.Key.Prefix.Select(x => x == null ? "null" : "\"" + x + "\""))); foreach (var postfix in segment.Value.Postfixes) { if (postfix.Key.Contains("\n")) { writer.WriteLine(postfix.Value + " ."); } else { writer.WriteLine(postfix.Value + " " + postfix.Key); } } writer.WriteLine("-"); } }
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++; } }