Beispiel #1
0
 private ChainMap InitSimpleChainMapp()
 {
     ChainMap cm = new ChainMap() ;
     cm.AddToChain(new ChainKey(new string[]{null, "word1"}), "valnull") ;
     cm.AddToChain(new ChainKey(new string[]{"word1", "word2"}), "val1") ;
     cm.AddToChain(new ChainKey(new string[]{"word2", "word3"}), "val2") ;
     cm.AddToChain(new ChainKey(new string[]{"word1", "word2"}), "val3") ;
     return cm ;
 }
        /// <summary>
        /// Generate markov chains using input text
        /// </summary>
        public void GenerateChains()
        {
            //Split text
            string[]      splits         = _text.Split();
            List <string> advancedSplits = new List <string>();

            foreach (string baseSplit in splits)
            {
                advancedSplits.AddRange(StringUtils.SplitAndKeep(baseSplit.ToLower(), _delims));
            }


            Chains = new ChainMap();
            if (advancedSplits.Count < _chainSize * 2)
            {
                throw new Exceptions.InvalidArguments(string.Format("Chain size: {0} to small relative to text split {1}",
                                                                    _chainSize, advancedSplits.Count));
            }

            //Insert null elements in the beginning so chains are created for the first _chainSize elements
            for (int i = 0; i < _chainSize; i++)
            {
                advancedSplits.Insert(0, null);
            }

            List <string> .Enumerator listEnum = advancedSplits.GetEnumerator();

            string[] chainWords = new string[_chainSize];

            for (int i = 0; i < _chainSize; i++)
            {
                listEnum.MoveNext();
                chainWords[i] = listEnum.Current;
            }

            while (listEnum.MoveNext())
            {
                string   current = listEnum.Current;
                ChainKey currKey = new ChainKey(chainWords);
                Chains.AddToChain(currKey, current);

                for (int i = 0; i < _chainSize - 1; i++)
                {
                    chainWords[i] = chainWords[i + 1];
                }
                chainWords[_chainSize - 1] = current;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Get subchain of chainkeys and words that satisfies the passed in condition
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        internal ChainMap GetSubChain(ChainCondition condition)
        {
            //If conditions won't be applied return original chains
            if (condition == null || condition == _alwaysTrueCondition ||
                (condition.WordCondition == null && condition.KeyCondition == null))
            {
                return(_chains);
            }

            ChainMap newMap      = new ChainMap();
            bool     valuesFound = false;

            foreach (ChainKey key in _chains.GetAllKeys())
            {
                if (condition.KeyCondition != null)
                {
                    if (condition.KeyCondition.Invoke(key))
                    {
                        foreach (string word in _chains.GetValues(key))
                        {
                            if (condition.WordCondition != null)
                            {
                                if (condition.WordCondition.Invoke(word))
                                {
                                    newMap.AddToChain(key, word);
                                    valuesFound = true;
                                }
                            }
                        }
                    }
                }
            }

            if (!valuesFound)
            {
                throw new Exceptions.NoPossibleElements("Chain map filter filtered everything out");
            }

            return(newMap);
        }
Beispiel #4
0
        /// <summary>
        /// Generate markov chains using input text
        /// </summary>
        public void GenerateChains()
        {
            //Split text
            string[] splits = _text.Split() ;
            List<string> advancedSplits = new List<string>() ;
            foreach(string baseSplit in splits)
            {
                advancedSplits.AddRange(StringUtils.SplitAndKeep(baseSplit.ToLower(), _delims)) ;
            }

            Chains = new ChainMap() ;
            if(advancedSplits.Count < _chainSize * 2)
            {
                throw new Exceptions.InvalidArguments(string.Format("Chain size: {0} to small relative to text split {1}",
                                                         _chainSize, advancedSplits.Count)) ;
            }

            //Insert null elements in the beginning so chains are created for the first _chainSize elements
            for(int i = 0 ; i < _chainSize ; i++)
            {
                advancedSplits.Insert(0, null) ;
            }

            List<string>.Enumerator listEnum = advancedSplits.GetEnumerator() ;

            string[] chainWords = new string[_chainSize] ;

            for(int i = 0 ; i < _chainSize ; i++)
            {
                listEnum.MoveNext();
                chainWords[i] = listEnum.Current ;
            }

            while(listEnum.MoveNext())
            {
                string current = listEnum.Current;
                ChainKey currKey = new ChainKey(chainWords) ;
                Chains.AddToChain(currKey, current) ;

                for(int i = 0 ; i < _chainSize - 1 ; i++)
                {
                    chainWords[i] = chainWords[i+1] ;
                }
                chainWords[_chainSize - 1] = current ;
            }
        }
        public void T_GetSubChain()
        {
            WordGenerator gen = InitSimpleWordGen() ;

            //Should give me both "hello world one" and "hello world two"
            ChainMap tempMap = gen.GetSubChain(new ChainCondition(cw => cw.Words[0] == "hello", w => w.Length == 3)) ;
            ChainMap correctMap = new ChainMap() ;
            correctMap.AddToChain(new ChainKey(new string[] {"hello", "world"}), "one") ;
            correctMap.AddToChain(new ChainKey(new string[] {"hello", "world"}), "two") ;
            TestUtils.CompareChainTables(correctMap, tempMap) ;

            //Should give me just "hello world one"
            tempMap = gen.GetSubChain(new ChainCondition(cw => cw.Words[0] == "hello", w => w.StartsWith("o"))) ;
            correctMap = new ChainMap() ;
            correctMap.AddToChain(new ChainKey(new string[] {"hello", "world"}), "one") ;
            TestUtils.CompareChainTables(correctMap, tempMap) ;

            //If passed in condition is null nothing should change
            tempMap = gen.GetSubChain(new ChainCondition(null, null)) ;
            TestUtils.CompareChainTables(gen.Chains, tempMap) ;

            //If impossible condition is passed in exception should be thrown
            Assert.Throws(typeof(Exceptions.NoPossibleElements), delegate
                          { gen.GetSubChain(new ChainCondition(null, w => w == "Does not exist")) ;}) ;
        }
        public void T_GetRandomKey_WithParams()
        {
            WordGenerator gen = InitSimpleWordGen() ;
            ChainMap subMap = new ChainMap() ;
            ChainKey key1 = new ChainKey(new string[]{"key1", "key2", "key3"}) ;
            ChainKey key2 = new ChainKey(new string[]{"key1", "key2", "key4"}) ;
            subMap.AddToChain(key1, "val1") ;
            subMap.AddToChain(key2, "val2") ;
            bool key1Found, key2Found ;
            key1Found = key2Found = false ;

            for(int i = 0 ; i < 10 ; i++)
            {
                ChainKey currKey = gen.GetRandomKey(subMap);
                if(key1.Equals(currKey))
                {
                    key1Found = true ;
                }
                else if(key2.Equals(currKey))
                {
                    key2Found = true ;
                }
                else
                {
                    Assert.Fail("Invalid key returned") ;
                }
            }
            if(!(key1Found && key2Found))
            {
                Assert.Fail("GetRandomKey is not random") ;
            }
        }
Beispiel #7
0
        /// <summary>
        /// Get subchain of chainkeys and words that satisfies the passed in condition
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        internal ChainMap GetSubChain(ChainCondition condition)
        {
            //If conditions won't be applied return original chains
            if(condition == null || condition == _alwaysTrueCondition ||
               (condition.WordCondition == null && condition.KeyCondition == null))
            {
                return _chains ;
            }

            ChainMap newMap = new ChainMap() ;
            bool valuesFound = false ;
            foreach(ChainKey key in _chains.GetAllKeys())
            {
                if(condition.KeyCondition != null)
                {
                    if(condition.KeyCondition.Invoke(key))
                    {
                        foreach(string word in _chains.GetValues(key))
                        {
                            if(condition.WordCondition != null)
                            {
                                if(condition.WordCondition.Invoke(word))
                                {
                                    newMap.AddToChain(key, word) ;
                                    valuesFound = true ;
                                }
                            }
                        }
                    }
                }
            }

            if(!valuesFound)
            {
                throw new Exceptions.NoPossibleElements("Chain map filter filtered everything out") ;
            }

            return newMap ;
        }