示例#1
0
        /*public Rhyme EvaluateRhyme(Phrase word) {
         *  var consecutive = 0;
         *  var count = 0;
         *  var index = word.Words.SelectMany(each => each.Syllables).Count() - 1;
         *  var isConsecutive = true;
         *
         *  foreach (var syllable in word.Words.SelectMany(each => each.Syllables).Reverse()) {
         *      if (RhymeFinder.IsRhyme(syllable.Phonemes, Syllables[index].Phonemes)) {
         *          if (isConsecutive) {
         *              consecutive += 1;
         *          }
         *
         *          count += 1;
         *      }
         *      else {
         *          isConsecutive = false;
         *      }
         *
         *      index -= 1;
         *  }
         *
         *  return new Rhyme {
         *      Phrase = word,
         *      Consecutive = consecutive,
         *      Count = count
         *  };
         * }*/

        private IEnumerable <Phrase> InternalGetRhymes(bool strict = true)
        {
            var index   = 1;
            var results = new Dictionary <int, List <Word> >();
            var ngrams  = new Dictionary <string, NGram>();

            foreach (var syllable in Syllables)
            {
                var rhymes = RhymeFinder.GetRhymes(syllable, index).ToList();

                var validWords = new List <Word>();

                foreach (var rhyme in rhymes.Where(each => index - each.Syllables.Count + 1 == 1).ToList())
                {
                    validWords.Add(rhyme);

                    if (strict)
                    {
                        foreach (var ngram in rhyme.NGrams.Where(each => each.Index == 1))
                        {
                            if (!ngrams.ContainsKey(ngram.NGram.Text))
                            {
                                ngrams.Add(ngram.NGram.Text, ngram.NGram);
                            }
                        }
                    }
                }

                foreach (var rhyme in rhymes.Where(each => index - each.Syllables.Count + 1 != 1).ToList())
                {
                    if (strict)
                    {
                        var requiredSyllables = index - rhyme.Syllables.Count;

                        foreach (var ngram in rhyme.NGrams.OrderByDescending(each => each.NGram.Frequency))
                        {
                            if (ngrams.ContainsKey(ngram.NGram.Text))
                            {
                                var priorSyllables = ngram.NGram.Words.Where(each => each.Index < ngram.Index).Sum(each => each.Word.Syllables.Count);

                                if (priorSyllables == requiredSyllables)
                                {
                                    validWords.Add(rhyme);

                                    // Todo:  Should filter all valid NGrams for all permutations
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        validWords.Add(rhyme);
                    }
                }

                var bestResults = validWords.OrderByDescending(each => each.NGrams.Sum(ngram => ngram.NGram.Frequency)) /*.Take(100)*/.ToList();

                results.Add(index, bestResults);

                index += 1;
            }

            var final = FindMultiSyllables(results, Syllables.Count);

            if (strict)
            {
                final = final.Where(each => each.GetNGramScore() > 0);
            }

            return(final);
        }
示例#2
0
        public IEnumerable <Phrase> GetRhymes(bool strict = true)
        {
            if (Syllables.Count > 10)
            {
                throw new Exception("Too many syllables.");
            }

            var rhymes = InternalGetRhymes(strict).Where(rhyme => {
                var index = 0;

                foreach (var word in Words)
                {
                    if (word.Text == rhyme.Words[index].Text && RhymeFinder.IsRhyme(word, rhyme.Words[index]))
                    {
                        return(false);
                    }

                    if (rhyme.Words.Count > index + 1)
                    {
                        index += 1;
                    }
                }

                return(true);
            })
                         .Select(rhyme => {
                var consecutive   = 0;
                var count         = 0;
                var index         = rhyme.Words.SelectMany(each => each.Syllables).Count() - 1;
                var isConsecutive = true;

                foreach (var syllable in rhyme.Words.SelectMany(each => each.Syllables).Reverse())
                {
                    if (RhymeFinder.IsRhyme(syllable, Syllables[index]))
                    {
                        if (isConsecutive)
                        {
                            consecutive += 1;
                        }

                        count += 1;
                    }
                    else
                    {
                        isConsecutive = false;
                    }

                    index -= 1;
                }

                return(new {
                    Rhyme = rhyme,
                    Consecutive = consecutive,
                    Count = count
                });
            })
                         //.Where(each => each.Consecutive == each.Rhyme.Words.Sum(w => w.Syllables.Count))
                         .OrderByDescending(each => each.Count)
                         .ThenByDescending(each => each.Consecutive)
                         .ThenByDescending(each => each.Rhyme.GetNGramScore())
                         .Take(500)
                         .Select(each => each.Rhyme)
                         .ToList();

            return(rhymes);
        }
示例#3
0
        static English()
        {
            Vocabulary = new Dictionary <string, Word>();

            //var pronounce = EnglishResources.pronounce.Replace("\t", " ").ToLower();
            //File.WriteAllText(@"C:\Users\Josh\Downloads\pronounce.txt", pronounce);

            var words = EnglishResources.pronounce.Replace("\t", " ").ToLower().ReadLines();

            /*var punctuated = new List<string>();
             *
             * foreach (var word in words) {
             *  if (word.Contains("'") && !word.Contains("'s"))) {
             *      punctuated.Add(word);
             *  }
             * }*/

            var tokens = IllegalTokens;

            var results = words.ForEachParallel(line => {
                if (tokens.Any(line.Contains))
                {
                    return(null);
                }

                var segments   = line.Replace("'", "").Replace("-", "").Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                var word       = segments.First().Trim();
                var hasNumbers = word.HasNumbers();

                if (!word[0].IsAlphabetical() || (hasNumbers && !word.Contains("(")) || !word.HasVowel())
                {
                    return(null);
                }

                if (hasNumbers)
                {
                    word = word.Split('(')[0];
                }

                var phonemes = segments.Skip(1).Select(each => new Phoneme(each)).ToList();

                var resolver = new PhonemeResolver(word);
                resolver.BindPhonemes(phonemes.ToArray());

                return(new Word(word, phonemes));
            });

            foreach (var word in results)
            {
                if (!Vocabulary.ContainsKey(word.Text))
                {
                    Vocabulary.Add(word.Text, word);
                }
                else
                {
                    Vocabulary[word.Text].Homographs.Add(word);
                }
            }

            RhymeFinder.BuildRhymes();
        }