Example #1
0
        string generateSentence(Noun previousSubject = null)
        {
            int nounId = rand.Next(0, Vocabulary.subjects.Count);
            int verbId = rand.Next(0, Vocabulary.verbs.Count);

            var subParticlePair = Enumerable.ToList(Vocabulary.subjects)[nounId];
            var nextSubject     = previousSubject ?? subParticlePair.Value;
            var subject         = (previousSubject == null) ? subParticlePair.Key : previousSubject.Text;
            var subjectParticle = (previousSubject == null) ? subParticlePair.Value.Particle : previousSubject.Particle;
            var verb            = Vocabulary.verbs[verbId];

            if (verb.Tags.Count > 0)
            {
                string tag   = verb.Tags[rand.Next(0, verb.Tags.Count)];
                var    nouns = Noun.GetByTag(Vocabulary.nouns, tag);
                if (nouns.Count > 0)
                {
                    int advId           = rand.Next(0, Vocabulary.adjectives.Count);
                    var advParticlePair = Enumerable.ToList(Vocabulary.adjectives)[advId];
                    var adv             = advParticlePair.Key;
                    var advParticle     = advParticlePair.Value;

                    var randomNoun = nouns[(rand.Next(0, nouns.Count))];
                    return(String.Format("{0} {1} {2} was {3} {4} {5}.", capitalize(advParticle), adv, subject, verb.Continuous, randomNoun.Particle, randomNoun.Text));
                }
            }

            bool isContinuous   = rand.NextDouble() < continuousProb;
            int  numberOfAdj    = previousSubject == null ? (rand.NextDouble() < adjProb ? (rand.NextDouble() < secAdjProb ? 2 : 1) : 0) : 0;
            bool useParticleThe = rand.NextDouble() < theProb;

            List <string> genWords = new List <string>();

            List <string> adjList  = new List <string>();
            string        particle = subjectParticle;

            for (int i = 0; i < numberOfAdj; i++)
            {
                int advId           = rand.Next(0, Vocabulary.adjectives.Count);
                var advParticlePair = Enumerable.ToList(Vocabulary.adjectives)[advId];

                particle = advParticlePair.Value;

                adjList.Add(advParticlePair.Key);
            }
            adjList.Reverse();

            if (previousSubject != null)
            {
                List <string> thenLikes = new List <string>()
                {
                    "then", "after that", "later", "suddenly"
                };
                genWords.Add(thenLikes[rand.Next(thenLikes.Count)]);
            }

            if (previousSubject == null)
            {
                if (rand.NextDouble() < someProb)
                {
                    genWords.Add("some");
                }
                else
                {
                    genWords.Add(particle);
                }
            }
            else if (useParticleThe)
            {
                genWords.Add("the");
            }
            else
            {
                List <string> theLikes = new List <string>()
                {
                    "that", "the same", "same"
                };
                genWords.Add(theLikes[rand.Next(theLikes.Count)]);
            }

            genWords.AddRange(adjList);
            genWords.Add(subject);
            if (isContinuous)
            {
                genWords.Add("was");
                genWords.Add(verb.Continuous);
            }
            else
            {
                genWords.Add(verb.Past);
            }

            genWords[0] = capitalize(genWords[0]);

            string nextSentence = GetRandomTruth(nextSentenceProb) ? " " + generateSentence(nextSubject) : "";

            return(string.Join(" ", genWords) + "." + nextSentence);
        }
        string MakePassive(string sentence)
        {
            int           verbIdx       = -1;
            Verb          verb          = null;
            List <string> splitSentence = new List <string>(sentence.Split(" "));

            for (int i = splitSentence.Count - 1; i >= 0; i--)
            {
                if (splitSentence[i].Length == 0 || splitSentence[i].Equals(" "))
                {
                    splitSentence.RemoveAt(i);
                }
            }
            sentence = string.Join(" ", splitSentence);

            for (int i = 0; i < splitSentence.Count; i++)
            {
                var verbOpt = Verb.ParseVerb(splitSentence[i]);
                if (verbOpt != null)
                {
                    verbIdx = i;
                    verb    = verbOpt;
                }
            }

            if (verb == null || !verb.IsPassive)
            {
                return(sentence);
            }

            int  n1Idx = -1;
            Noun n1    = null;

            for (int i = verbIdx - 1; i >= 0; i--)
            {
                var nounOpt = Noun.ParseNoun(splitSentence[i]);
                if (nounOpt != null)
                {
                    n1    = nounOpt;
                    n1Idx = i;
                    break;
                }
            }

            int  n2Idx = -1;
            Noun n2    = null;

            for (int i = verbIdx + 1; i < splitSentence.Count; i++)
            {
                var nounOpt = Noun.ParseNoun(splitSentence[i]);
                if (nounOpt != null)
                {
                    n2    = nounOpt;
                    n2Idx = i;
                    break;
                }
            }

            if (n1 == null || n2 == null)
            {
                return(sentence);
            }

            try
            {
                int p1Idx = -1;
                int p2Idx = -1;
                if (n1Idx >= 1 && IsParticle(splitSentence[n1Idx - 1]))
                {
                    p1Idx = n1Idx - 1;
                }
                else if (n1Idx >= 2 && IsParticle(splitSentence[n1Idx - 2]))
                {
                    p1Idx = n1Idx - 2;
                }

                if (n2Idx >= 1 && IsParticle(splitSentence[n2Idx - 1]))
                {
                    p2Idx = n2Idx - 1;
                }
                else if (n2Idx >= 2 && IsParticle(splitSentence[n2Idx - 2]))
                {
                    p2Idx = n2Idx - 2;
                }
                splitSentence[n1Idx]   = n2.Text;
                splitSentence[n2Idx]   = n1.Text;
                splitSentence[verbIdx] = "being " + verb.Text + "en by";

                if (p1Idx > -1 && p2Idx > -1)
                {
                    splitSentence[p1Idx] = n2.Particle;
                    splitSentence[p2Idx] = n1.Particle;

                    if (p1Idx == n1Idx - 2)
                    {
                        splitSentence[p2Idx]    += " " + splitSentence[n1Idx - 1];
                        splitSentence[n1Idx - 1] = "";
                    }

                    if (p2Idx == n2Idx - 2)
                    {
                        splitSentence[p1Idx]    += " " + splitSentence[n2Idx - 1];
                        splitSentence[n2Idx - 1] = "";
                    }
                }

                splitSentence[0] = Vocabulary.capitalize(splitSentence[0]);

                return(string.Join(" ", splitSentence));
            }
            catch (Exception)
            {
                return(sentence);
            }
        }