Skip to content

Instantly share code, notes, and snippets.

@aarondandy
Created March 6, 2017 06:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aarondandy/aaa622afeeb0cb86b0d4efe697c23be5 to your computer and use it in GitHub Desktop.
Save aarondandy/aaa622afeeb0cb86b0d4efe697c23be5 to your computer and use it in GitHub Desktop.
Make words
using System.Collections.Generic;
using System.Linq;
using Hunspell;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
var dictionary = HunspellDictionary.FromFile(@"C:\projects\HunspellNetCore\samples\English (American).dic");
var words = GenerateAllWords(dictionary).Distinct().OrderBy(x => x, dictionary.Affix.StringComparer).ToList();
}
public static bool TryAppend(PrefixEntry prefix, string word, out string result)
{
if (prefix.Conditions.IsStartingMatch(word) && word.StartsWith(prefix.Strip))
{
result = prefix.Append + word.Substring(prefix.Strip.Length);
return true;
}
result = null;
return false;
}
public static bool TryAppend(SuffixEntry suffix, string word, out string result)
{
if (suffix.Conditions.IsEndingMatch(word) && word.EndsWith(suffix.Strip))
{
result = word.Substring(0, word.Length - suffix.Strip.Length) + suffix.Append;
return true;
}
result = null;
return false;
}
static IEnumerable<string> GenerateAllWords(HunspellDictionary dictionary)
{
string combined;
foreach (var wordEntry in dictionary.WordList.AllEntries)
{
yield return wordEntry.Word;
var allPrefixesForWord = dictionary.Affix.Prefixes.Where(p => wordEntry.ContainsFlag(p.AFlag));
foreach (var prefixEntry in allPrefixesForWord.SelectMany(p => p.Entries))
{
if (TryAppend(prefixEntry, wordEntry.Word, out combined))
{
yield return combined;
}
}
var allSuffixesForWord = dictionary.Affix.Suffixes.Where(s => wordEntry.ContainsFlag(s.AFlag));
foreach (var suffixEntry in allSuffixesForWord.SelectMany(s => s.Entries))
{
if (TryAppend(suffixEntry, wordEntry.Word, out combined))
{
yield return combined;
}
}
foreach (var prefixEntry in allPrefixesForWord.Where(p => p.AllowCross).SelectMany(p => p.Entries))
{
string withPrefix;
if (TryAppend(prefixEntry, wordEntry.Word, out withPrefix))
{
foreach (var suffixEntry in allSuffixesForWord.Where(s => s.AllowCross).SelectMany(s => s.Entries))
{
if (TryAppend(suffixEntry, withPrefix, out combined))
{
yield return combined;
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment