Пример #1
0
        public void TestPunHelper(string theme, PunCategory category)
        {
            var synSets = WordNetEngine.GetSynSets(theme).ToList();

            var puns = PunHelper.GetPuns(
                category,
                theme,
                synSets,
                WordNetEngine,
                PronunciationEngine,
                SpellingEngine,
                PunStrategyFactory.AllFactories
                )
                       .ToList();

            puns.Should().HaveCountGreaterThan(2);

            TestOutputHelper.WriteLine(puns.Count + " puns");

            foreach (var(newPhrase, oldPhrase) in puns.Select(x => (x.NewPhrase, x.OldPhrase))
                     .Distinct())
            {
                TestOutputHelper.WriteLine($"{newPhrase} ({oldPhrase})");
            }
        }
Пример #2
0
 public PunState(
     string initialTheme,
     PunCategory initialCategory,
     Action stateHasChanged,
     ISyncLocalStorageService storage)
 {
     WordNetEngine       = new WordNetEngine();
     PronunciationEngine = new PronunciationEngine();
     SpellingEngine      = new SpellingEngine();
     Theme           = initialTheme;
     StateHasChanged = stateHasChanged;
     Storage         = storage;
     PunCategory     = initialCategory;
     FavoritePuns    = GetFavoritePuns(storage);
 }
Пример #3
0
        public static IEnumerable <Pun> GetPuns(
            PunCategory category,
            string theme,
            IReadOnlyCollection <SynSet> synSets,
            WordNetEngine wordNetEngine,
            PronunciationEngine pronunciationEngine,
            SpellingEngine spellingEngine,
            IReadOnlyList <PunStrategyFactory> strategies)
        {
            var sw = Stopwatch.StartNew();

#if Debug
            Console.WriteLine(@"Getting Puns");
#endif

            var resultCount = 0;

            var phrases = GetPhrases(category);

            var themeWords =
                synSets.SelectMany(
                    synSet => GetRelatedWords(theme, synSet, wordNetEngine)
                    .Select(x => x.Word)
                    )
                .Where(x => !x.Contains('_'))
                .Prepend(theme)
                .Distinct(StringComparer.OrdinalIgnoreCase)
                .Except(CommonWords.Value, StringComparer.OrdinalIgnoreCase)
                .Where(x => x.Length > 1)
                .Select(pronunciationEngine.GetPhoneticsWord)
                .Where(x => x is not null)
                .Cast <PhoneticsWord>()
                .Where(x => x.Syllables.Count > 1 || x.Syllables[0].Symbols.Count > 1)
                .Distinct(WordPronunciationComparer.Instance)
                .ToList();
#if Debug
            Console.WriteLine($@"Got Theme Words ({sw.Elapsed}");
#endif

            var cache = new Dictionary <PhoneticsWord, PunReplacement>();

            var punStrategies =
                strategies.Select(x => x.GetStrategy(spellingEngine, themeWords)).ToList();

        #if Debug
            Console.WriteLine($@"Built Strategies ({sw.Elapsed}");
#endif

            //TODO run in parallel
            foreach (var phrase in phrases)
            {
                var words = phrase
                            .Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

                var wordList         = new List <string>();
                var punWords         = new HashSet <string>();
                var containsOriginal = false;
                var containsPun      = false;

                foreach (var word in words)
                {
                    var bestReplacement = BestReplacement(
                        word,
                        pronunciationEngine,
                        cache,
                        punStrategies
                        );

                    if (bestReplacement != null)
                    {
                        var casing    = DetectCasing(word);
                        var newString = ToCase(bestReplacement.Value.ReplacementString, casing);
                        wordList.Add(newString);
                        containsOriginal |= bestReplacement.Value.IsAmalgam;
                        containsPun       = true;
                        punWords.Add(bestReplacement.Value.PunWord);
                    }
                    else
                    {
                        wordList.Add(word);
                        containsOriginal = true;
                    }
                }

                if (containsPun && (words.Length > 1 || containsOriginal))
                {
                    var pun = new Pun(wordList.ToDelimitedString(" "), phrase, punWords);

                #if Debug
                    if (resultCount == 0)
                    {
                        Console.WriteLine($@"{pun.NewPhrase} ({sw.Elapsed})");
                    }
                #endif

                    yield return(pun);

                    resultCount++;
                }
            }


        #if Debug
            Console.WriteLine($@"{resultCount} Puns Got ({sw.Elapsed})");