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); }
static PunReplacement?BestReplacement( string word, PronunciationEngine pronunciationEngine, IDictionary <PhoneticsWord, PunReplacement> cache, IEnumerable <PunStrategy> punStrategies) { if (CommonWords.Value.Contains(word)) { return(null); } var cmuWord = pronunciationEngine.GetPhoneticsWord(word); if (cmuWord is null) { return(null); } if (cmuWord.Syllables.Count < 2 && cmuWord.Syllables[0].Symbols.Count < 3) { return(null); } if (!cache.TryGetValue(cmuWord, out var bestReplacement)) { bestReplacement = punStrategies .SelectMany(x => x.GetPossibleReplacements(cmuWord)) .FirstOrDefault() !; cache.Add(cmuWord, bestReplacement); } if (string.IsNullOrWhiteSpace(bestReplacement.ReplacementString)) { return(null); } return(bestReplacement); }
public WordFixture() { SpellingEngine = new SpellingEngine(); WordNetEngine = new WordNetEngine(); PronunciationEngine = new PronunciationEngine(); }
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})");
public WordFixture() { WordNetEngine = new WordNetEngine(); PronunciationEngine = new PronunciationEngine(); }