public bool Equals(NGram <T> other) { if (this.hashCode != other.hashCode) { return(false); } if (other.N != this.N) { return(false); } for (int i = 0; i < other.N; i++) { if (null == other.Grams[i]) { if (this.Grams[i] == null) { continue; } return(false); } if (!other.Grams[i].Equals(this.Grams[i])) { return(false); } } return(true); }
private NGram <T> GetNextState(NGram <T> curState, NextStateType searchType) { var edges = this.graph.Edges(curState); if (null == edges || edges.Count == 0) { switch (searchType) { case NextStateType.PossibleHeterogenous: var pos = this.graph.GetLowerConstruct(curState); if (pos.HasValue) { curState = pos.Value; } else { return(NGram <T> .Empty); } break; case NextStateType.Homogenous: return(NGram <T> .Empty); } } while (true) { foreach (var item in edges) { if (this.Random.NextDouble() < item.Probability) { return(item.Edge); } } } }
public NGramGraphMarkovChain <T> GetSubGraphFromNGram(NGram <T> gram, int depth) { this.ValidNode(gram).AssertTrue(); var set = this.RetrieveAppearedNGramsToSomeDepth(gram, depth); Dictionary <NGram <T>, List <NonRecursiveNGramProbabilisticEdge <T> > > dictionary = new Dictionary <NGram <T>, List <NonRecursiveNGramProbabilisticEdge <T> > >(); foreach (var item in set) { dictionary.Add(item, new List <NonRecursiveNGramProbabilisticEdge <T> >()); foreach (var sub in this.graph[item]) { if (set.Contains(sub.Edge)) { dictionary[item].Add(sub); } else { dictionary[item].Add(sub); if (!dictionary.ContainsKey(sub.Edge)) { dictionary.Add(sub.Edge, new List <NonRecursiveNGramProbabilisticEdge <T> >()); } } } } return(new NGramGraphMarkovChain <T>(dictionary, this.IsHeterogenous, this.Distrubution.Filter(new Predicate <KeyValuePair <NGram <T>, int> >(x => dictionary.ContainsKey(x.Key))))); }
public NonRecursiveNGramProbabilisticEdge(float prob, NGram <T> edge) { (prob <= 1F && prob >= 0).AssertTrue(); this.Probability = prob; this.Edge = edge; }
public int RetrieveCount(NGram <T> t) { if (this.distrubution.ContainsKey(t)) { return(this.distrubution[t]); } return(0); }
public NGramRandomGraphMarkovChainWalker(NGramGraphMarkovChain <T> graph, NGram <T> startingNode, Random rand) { graph.NullCheck(); graph.ValidNode(startingNode).AssertTrue(); rand.NullCheck(); this.graph = graph; this.startingNode = startingNode; this.Random = rand; }
/* * public bool Save(string filePath) * { * try * { * using (Stream stream = File.Open(filePath, FileMode.OpenOrCreate)) * { * BinaryFormatter formatter = new BinaryFormatter(); * formatter.Serialize(stream, this); * } * } * catch (Exception e) * { * return false; * } * return true; * } * * public static NGramGraphMarkovChain<T> Load(string filePath) * { * using (Stream stream = File.Open(filePath, FileMode.Open)) * { * BinaryFormatter formatter = new BinaryFormatter(); * var obj = (NGramGraphMarkovChain<T>)formatter.Deserialize(stream); * return obj; * } * } */ private HashSet <NGram <T> > RetrieveAppearedNGramsToSomeDepth(NGram <T> gram, int depth, int curDepth = 0) { if (curDepth >= depth) { return(new HashSet <NGram <T> >()); } HashSet <NGram <T> > set = new HashSet <NGram <T> >(); set.Add(gram); foreach (var item in this.graph[gram]) { foreach (var sub in RetrieveAppearedNGramsToSomeDepth(item.Edge, depth, ++curDepth)) { set.Add(sub); } } return(set); }
public NGram <T>?GetLowerConstruct(NGram <T> curState) { if (curState.N > 1) { var lowerConstructData = curState .AsEnumerable() .Skip(1) .ToArray(); NGram <T> lowerConstruct = new NGram <T>(lowerConstructData); if (this.ValidNode(lowerConstruct)) { return(lowerConstruct); } return(null); } return(null); }
public NGram <T>[] WalkMarkovChain(int depth, NextStateType searchType = NextStateType.PossibleHeterogenous) { List <NGram <T> > path = new List <NGram <T> >(depth); (depth > 0).AssertTrue(); NGram <T> curState = this.startingNode; path.Add(curState); for (int i = 1; i < depth; i++) { curState = this.GetNextState(curState, searchType); if (curState == NGram <T> .Empty) { return(path.ToArray()); } path.Add(curState); } return(path.ToArray()); }
public static HomogenousNGrams <T> BuildFromSingleNGrams(NGram <T> a) { return(new HomogenousNGrams <T>(a.AsEnumerableObject(), a.N)); }
public static HeterogenousNGrams <T> BuildSingle(NGram <T> nGram) { return(new HeterogenousNGrams <T>(HomogenousNGrams <T> .BuildFromSingleNGrams(nGram).AsEnumerableObject().ToList(), nGram.N, nGram.N + 1)); }
private static HashSet <NGram <T> > BuildSingleBlock(List <NGram <T>[]> grams, NGram <T> node, int min, int max) { HashSet <NGram <T> > blockedGram = new HashSet <NGram <T> >(); List <int> indices = new List <int>(20); var first = grams[node.N - min]; var n = node.N; for (int i = n; i < first.Length; i++) { if (first[i - n] == node) { indices.Add(i); blockedGram.Add(first[i]); } } for (int i = min; i < max; i++) { if (i == n) { continue; } var gram = grams[i - min]; foreach (var item in indices) { if (item < gram.Length) { blockedGram.Add(gram[item]); } } } return(blockedGram); }
public IReadOnlyList <NonRecursiveNGramProbabilisticEdge <T> > Edges(NGram <T> gram) { this.ValidNode(gram).AssertTrue(); return(this.graph[gram].AsReadOnly()); }
public bool ValidNode(NGram <T> node) { return(this.graph.ContainsKey(node)); }
public double RetrieveProbability(NGram <T> t) { return((double)this.RetrieveCount(t) / (double)this.Size); }