Esempio n. 1
0
        private static string Serialize(MarkovChain <char> chain)
        {
            var states = chain.GetStates().ToDictionary(
                s => string.Concat(s),
                s =>
            {
                var next   = chain.GetNextStates(s) ?? Enumerable.Empty <KeyValuePair <char, int> >();
                var result = next.ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value);

                var terminal = chain.GetTerminalWeight(s);
                if (terminal > 0)
                {
                    result[string.Empty] = terminal;
                }

                return(result);
            });

            return(JsonConvert.SerializeObject(states).Replace('\"', '\''));
        }
Esempio n. 2
0
        // We want the average options per state to be between 1.5 to 2.0 ideally.
        // If it's less than that then we're likely directly quoting the input text.
        static void EvaluateOrders()
        {
            for (int order = 1; order < 5; order++)
            {
                MarkovChain <string> chain = GetChain(order);

                var states     = chain.GetStates();
                int numStates  = 0;
                int numOptions = 0;
                foreach (ChainState <string> state in states)
                {
                    var nextStates     = chain.GetNextStates(state);
                    int terminalWeight = chain.GetTerminalWeight(state);
                    numStates++;
                    numOptions += (nextStates != null ? nextStates.Count : 0);
                    numOptions += (terminalWeight > 0 ? 1 : 0); // If this is a possible termination of the chain, that's one option
                }

                Console.WriteLine("Order: " + order + " NumStates: " + numStates + " NumOptionsTotal: " + numOptions + " AvgOptionsPerState " + ((float)numOptions / (float)numStates));
            }
        }