protected PunStrategy(SpellingEngine spellingEngine, IEnumerable <PhoneticsWord> themeWords) { SpellingEngine = spellingEngine; ThemeWordLookup = themeWords.SelectMany( word => GetThemeWordSyllables(word).Select(cluster => (word, cluster)) ) .OrderBy(x => x.word.Text.Length) .ToLookup(x => x.cluster, x => x.word, ListComparer <Syllable> .Instance); }
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); }
protected string GetSpelling(IEnumerable <Syllable> syllables) { var sb = new StringBuilder(); foreach (var syllable in syllables) { var spelling = SpellingEngine.GetSpelling(syllable); if (spelling != null) { sb.Append(spelling.Text); } else { sb.Append(new string(syllable.ToString().Where(char.IsLetter).ToArray())); } } return(sb.ToString()); }
public WordFixture() { SpellingEngine = new SpellingEngine(); WordNetEngine = new WordNetEngine(); PronunciationEngine = new PronunciationEngine(); }
/// <inheritdoc /> public SharedPrefixPunStrategy( SpellingEngine spellingEngine, IEnumerable <PhoneticsWord> themeWords) : base(spellingEngine, themeWords) { }
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})");
/// <inheritdoc /> public HomophonePunStrategy( SpellingEngine spellingEngine, IEnumerable <PhoneticsWord> themeWords) : base(spellingEngine, themeWords) { }
/// <inheritdoc /> public SameConsonantsPunStrategy( SpellingEngine spellingEngine, IEnumerable <PhoneticsWord> themeWords) : base(spellingEngine, themeWords) { }
public PerfectRhymePunStrategy( SpellingEngine spellingEngine, IEnumerable <PhoneticsWord> themeWords) : base(spellingEngine, themeWords) { }