Exemple #1
0
 public void TryAddAssociation(Word associatedWord)
 {
     if ( associations.Contains(associatedWord) == false )
     {
         associations.Add(associatedWord);
     }
 }
Exemple #2
0
        public string GenerateSentence(WordSet inputWords, WordSet relatedWords, Word startingWord, Random rand)
        {
            //If the starting word is moot, generate another one

            Word currentWord, lastWord;
            string ret = "";
            int wordCount = 1;
            int maxWordCount = Math.Min(Sentence.maxWords, wordbank.AverageSentenceLength);
            maxWordCount = Math.Max(maxWordCount, Sentence.minWords);
            lastWord = startingWord;
            ret += Word.ToSentenceCase(lastWord.name);
            WordSet appearedWords = new WordSet();

            while ( wordCount < maxWordCount )
            {
                if ( lastWord.followingWords.Count == 0 ) { break; }

                WeightedWordDict relatedFollowingWords = GetRelatedWords(lastWord.followingWords, relatedWords);

                //Tries to choose the current word.  If it cant choose a related followed word, choose any followed one
                if ( wordCount >= Sentence.minWords )
                {
                    currentWord = relatedFollowingWords.ChooseRandomEndingWord(rand);
                    if ( currentWord == null ) { currentWord = relatedFollowingWords.ChooseRandomWord(rand); }
                }
                else
                { currentWord = relatedFollowingWords.ChooseRandomWord(rand); }

                if ( appearedWords.Contains(currentWord) )
                {
                    relatedFollowingWords.Remove(currentWord);
                    currentWord = relatedFollowingWords.ChooseRandomWord(rand);
                }
                if ( currentWord == null ) { currentWord = lastWord.followingWords.ChooseRandomWord(rand); }
                if ( currentWord == null ) { DANI.Program.NullError("ChooseRandomWord returned null with non-empty list (last word "+lastWord+")."); break; }

                appearedWords.Add(currentWord);
                lastWord = currentWord;

                ret += " " + currentWord.GetName;
                wordCount++;

                if ( ( currentWord.endingWord ) && ( wordCount >= Sentence.minWords ) )
                { DANI.Program.DebugMsg("endingWord ended: " + currentWord.GetName); break; }
            }

            return ret;
        }
Exemple #3
0
 public bool TryGetWord(string name, out Word word)
 {
     return wordbank.wordlist.TryGetValue(name, out word);
 }
Exemple #4
0
        //Tries to add word to dictionary.  Returns if the word was not added (eg already present, or invalid)
        public bool TryAddWord(string word, out Word result)
        {
            if ( word == "" ) { result = null; return false; }

            word = word.ToLower();
            if ( wordlist.TryGetValue(word, out result) )
            {
                result.frequency++;
                return false;
            }
            else
            {
                result = new Word(word);
                wordlist.Add(word, result);
                return true;
            }
        }
Exemple #5
0
        public void TryAddFollowingWord(Word followingWord)
        {
            if ( followingWord == null ) { Program.NullError("TryAddFollowingWord given a null word"); return; }

            followingWords.AddOrIncrement(followingWord);
        }
Exemple #6
0
 public static string WordPropertiesString(Word word)
 {
     return (
             word.name +"."
             +word.startingWord +"."
             +word.endingWord +"."
             +word.capitalisedWordWeight +"."
             +word.frequency
             +" " );
 }
Exemple #7
0
 //generates string of associated word for file output
 public static string WordAssoiationString(int srcIndex, Word association, Dictionary<Word, int> map)
 {
     return ( srcIndex +":" +map[association] +" " );
 }