Exemplo n.º 1
0
        public void AddFromList(List<string> input)
        {
            Log.Debug (TAG, "Adding from list of words.");
            int wordCount = 0;
            for (int j = 0; j < input.Count; j++) {
                string[] words = ( SENTENCESTART + " " + CleanInput (input[j]) + SENTENCEEND  ).Split (' ');

                for (int i = 0; i < words.Length - 1; i++) {
                    string curWord = words [i].ToLower ();
                    string nextWord = words [i + 1].ToLower ();

                    // Debug, tracking how many words
                    wordCount++;

                    //Log.Debug (TAG, curWord);

                    if (!chain.ContainsKey (curWord)) {
                        MarkovWord link = new MarkovWord (curWord);
                        chain.Add (curWord, link);
                    }

                    if (!chain.ContainsKey (nextWord)) {
                        MarkovWord link = new MarkovWord (nextWord);
                        chain.Add (nextWord, link);
                    }

                    MarkovWord after = chain [nextWord];
                    if (!chain [curWord].afters.ContainsKey (chain [nextWord])) {
                        chain [curWord].afters.Add (after, 1);
                    } else {
                        chain [curWord].afters [after]++;
                    }
                }
            }
            Log.Debug(TAG, "Done importing list - " + wordCount + " words processed.");
        }
Exemplo n.º 2
0
        public void Load(MarkovFactory factory)
        {
            Log.Debug ( TAG, "Loading." );
            myDatabase = context.OpenOrCreateDatabase (DATABASE_NAME, FileCreationMode.WorldReadable, null);
            ICursor cursor = myDatabase.RawQuery ("SELECT * FROM 'words'", null);

            int firstWordIndex = cursor.GetColumnIndex("firstWord");
            int secondWordIndex = cursor.GetColumnIndex("secondWord");
            int instanceCountIndex = cursor.GetColumnIndex("instanceCount");

            string firstWord, secondWord;
            int instanceCount;

            cursor.MoveToFirst ();
            while (!cursor.IsAfterLast)
            {
                firstWord = cursor.GetString ( firstWordIndex );
                secondWord = cursor.GetString ( secondWordIndex );
                instanceCount = cursor.GetInt( instanceCountIndex );

                if (!factory.chain.ContainsKey( firstWord ) )
                {
                    MarkovWord link = new MarkovWord(firstWord);
                    factory.chain.Add( firstWord, link );
                }

                MarkovWord afterWord;

                if (!factory.chain.ContainsKey ( secondWord ) )
                {
                    afterWord = new MarkovWord ( secondWord );
                    factory.chain.Add( secondWord, afterWord );
                }

                afterWord = factory.chain[secondWord];

                factory.chain[ firstWord ].afters.Add( afterWord, instanceCount );

                cursor.MoveToNext();
            }
        }