private WordList ToImmutable(bool destructive) { var affix = Affix ?? new AffixConfig.Builder().MoveToImmutable(); var nGramRestrictedFlags = Dedup(FlagSet.Create( new[] { affix.ForbiddenWord, affix.NoSuggest, affix.NoNgramSuggest, affix.OnlyInCompound, SpecialFlags.OnlyUpcaseFlag } .Where(f => f.HasValue))); var result = new WordList(affix) { NGramRestrictedFlags = nGramRestrictedFlags, }; if (destructive) { result.EntriesByRoot = EntriesByRoot ?? new Dictionary <string, WordEntrySet>(); EntriesByRoot = null; } else { result.EntriesByRoot = EntriesByRoot == null ? new Dictionary <string, WordEntrySet>() : new Dictionary <string, WordEntrySet>(EntriesByRoot); } var nGramRestrictedEntries = new HashSet <WordEntry>(); foreach (var rootSet in result.EntriesByRoot) { foreach (var entry in rootSet.Value) { if (nGramRestrictedFlags.ContainsAny(entry.Flags)) { nGramRestrictedEntries.Add(entry); } } } result.NGramRestrictedEntries = nGramRestrictedEntries; return(result); }
internal static TResult Create <TResult>(List <AffixEntryGroup <TEntry> .Builder> builders, Constructor <TResult> constructor) where TResult : AffixCollection <TEntry> { var affixesByFlag = new Dictionary <FlagValue, AffixEntryGroup <TEntry> >(builders.Count); var affixesByIndexedByKeyBuilders = new Dictionary <char, Dictionary <FlagValue, AffixEntryGroup <TEntry> .Builder> >(); var affixesWithEmptyKeys = new List <AffixEntryGroup <TEntry> >(); var affixesWithDots = new List <AffixEntryGroup <TEntry> >(); var contClasses = new HashSet <FlagValue>(); if (builders != null) { foreach (var builder in builders) { var group = builder.ToGroup(); affixesByFlag.Add(group.AFlag, group); var entriesWithNoKey = new List <TEntry>(); var entriesWithDots = new List <TEntry>(); foreach (var entry in group.Entries) { var key = entry.Key; contClasses.UnionWith(entry.ContClass); if (string.IsNullOrEmpty(key)) { entriesWithNoKey.Add(entry); } else if (key.Contains('.')) { entriesWithDots.Add(entry); } else { var indexedKey = key[0]; if (!affixesByIndexedByKeyBuilders.TryGetValue(indexedKey, out var keyedAffixes)) { keyedAffixes = new Dictionary <FlagValue, AffixEntryGroup <TEntry> .Builder>(); affixesByIndexedByKeyBuilders.Add(indexedKey, keyedAffixes); } if (!keyedAffixes.TryGetValue(group.AFlag, out var groupBuilder)) { groupBuilder = new AffixEntryGroup <TEntry> .Builder { AFlag = group.AFlag, Options = group.Options, Entries = new List <TEntry>() }; keyedAffixes.Add(group.AFlag, groupBuilder); } groupBuilder.Entries.Add(entry); } } if (entriesWithNoKey.Count > 0) { affixesWithEmptyKeys.Add(new AffixEntryGroup <TEntry>(group.AFlag, group.Options, AffixEntryCollection <TEntry> .Create(entriesWithNoKey))); } if (entriesWithDots.Count > 0) { affixesWithDots.Add(new AffixEntryGroup <TEntry>(group.AFlag, group.Options, AffixEntryCollection <TEntry> .Create(entriesWithDots))); } } } var affixesByIndexedByKey = new Dictionary <char, AffixEntryGroupCollection <TEntry> >(affixesByIndexedByKeyBuilders.Count); foreach (var keyedBuilder in affixesByIndexedByKeyBuilders) { var indexedAffixGroup = new AffixEntryGroup <TEntry> [keyedBuilder.Value.Count]; var writeIndex = 0; foreach (var builderPair in keyedBuilder.Value) { indexedAffixGroup[writeIndex++] = builderPair.Value.ToGroup(); } affixesByIndexedByKey.Add(keyedBuilder.Key, AffixEntryGroupCollection <TEntry> .TakeArray(indexedAffixGroup)); } return(constructor ( affixesByFlag, affixesByIndexedByKey, AffixEntryGroupCollection <TEntry> .Create(affixesWithDots), AffixEntryGroupCollection <TEntry> .Create(affixesWithEmptyKeys), FlagSet.Create(contClasses) )); }
private WordList ToImmutable(bool destructive) { var affix = Affix ?? new AffixConfig.Builder().MoveToImmutable(); var nGramRestrictedFlags = Dedup(FlagSet.Create( new[] { affix.ForbiddenWord, affix.NoSuggest, affix.NoNgramSuggest, affix.OnlyInCompound, SpecialFlags.OnlyUpcaseFlag } .Where(f => f.HasValue))); var result = new WordList(affix) { NGramRestrictedFlags = nGramRestrictedFlags, }; if (EntryDetailsByRoot == null) { result.EntriesByRoot = new Dictionary <string, WordEntryDetail[]>(); } else { result.EntriesByRoot = new Dictionary <string, WordEntryDetail[]>(EntryDetailsByRoot.Count); foreach (var pair in EntryDetailsByRoot) { result.EntriesByRoot.Add(pair.Key, pair.Value.ToArray()); } if (destructive) { EntryDetailsByRoot = null; } } result.AllReplacements = affix.Replacements; if (PhoneticReplacements != null && PhoneticReplacements.Count != 0) { // store ph: field of a morphological description in reptable if (result.AllReplacements.IsEmpty) { result.AllReplacements = SingleReplacementSet.Create(PhoneticReplacements); } else { result.AllReplacements = SingleReplacementSet.Create(result.AllReplacements.Concat(PhoneticReplacements)); } } result.NGramRestrictedDetails = new Dictionary <string, WordEntryDetail[]>(); var details = new List <WordEntryDetail>(); foreach (var rootSet in result.EntriesByRoot) { details.Clear(); foreach (var entry in rootSet.Value) { if (nGramRestrictedFlags.ContainsAny(entry.Flags)) { details.Add(entry); } } if (details.Count != 0) { result.NGramRestrictedDetails.Add(rootSet.Key, details.ToArray()); } } return(result); }
public static AffixCollection <TEntry> Create(List <AffixEntryGroup.Builder <TEntry> > builders) { if (builders == null || builders.Count == 0) { return(Empty); } var affixesByFlag = new Dictionary <FlagValue, AffixEntryGroup <TEntry> >(builders.Count); var affixesByIndexedByKeyBuilders = new Dictionary <char, List <AffixEntryWithDetail <TEntry> > >(); var affixesWithDots = new List <AffixEntryWithDetail <TEntry> >(); var affixesWithEmptyKeys = new List <AffixEntryWithDetail <TEntry> >(); var contClasses = new HashSet <FlagValue>(); foreach (var builder in builders) { var group = builder.ToGroup(); affixesByFlag.Add(group.AFlag, group); foreach (var entry in group.Entries) { var key = entry.Key; contClasses.UnionWith(entry.ContClass); var entryWithDetail = new AffixEntryWithDetail <TEntry>(group, entry); if (string.IsNullOrEmpty(key)) { affixesWithEmptyKeys.Add(entryWithDetail); } else { if (key.Contains('.')) { affixesWithDots.Add(entryWithDetail); } else { var indexedKey = key[0]; if (!affixesByIndexedByKeyBuilders.TryGetValue(indexedKey, out List <AffixEntryWithDetail <TEntry> > keyedAffixes)) { keyedAffixes = new List <AffixEntryWithDetail <TEntry> >(); affixesByIndexedByKeyBuilders.Add(indexedKey, keyedAffixes); } keyedAffixes.Add(entryWithDetail); } } } } var affixesByIndexedByKey = new Dictionary <char, AffixEntryWithDetailCollection <TEntry> >( affixesByIndexedByKeyBuilders.Count); foreach (var keyedBuilder in affixesByIndexedByKeyBuilders) { affixesByIndexedByKey.Add(keyedBuilder.Key, AffixEntryWithDetailCollection <TEntry> .TakeList(keyedBuilder.Value)); } return(new AffixCollection <TEntry> ( affixesByFlag, affixesByIndexedByKey, AffixEntryWithDetailCollection <TEntry> .TakeList(affixesWithDots), AffixEntryWithDetailCollection <TEntry> .TakeList(affixesWithEmptyKeys), FlagSet.Create(contClasses) )); }
internal static FlagSet ParseNumberFlags(StringSlice text) => FlagSet.Create(ParseNumberFlagsInOrder(text));
public static FlagSet ParseNumberFlags(string text, int startIndex, int length) => FlagSet.Create(ParseNumberFlagsInOrder(text.Subslice(startIndex, length)));
private WordList ToImmutable(bool destructive) { var affix = Affix ?? new AffixConfig.Builder().MoveToImmutable(); var nGramRestrictedFlags = Dedup(FlagSet.Create( new[] { affix.ForbiddenWord, affix.NoSuggest, affix.NoNgramSuggest, affix.OnlyInCompound, SpecialFlags.OnlyUpcaseFlag } .Where(f => f.HasValue))); var result = new WordList(affix) { NGramRestrictedFlags = nGramRestrictedFlags, }; if (EntryDetailsByRoot == null) { result.EntriesByRoot = new Dictionary <string, WordEntryDetail[]>(0); } else { result.EntriesByRoot = new Dictionary <string, WordEntryDetail[]>(EntryDetailsByRoot.Count); foreach (var pair in EntryDetailsByRoot) { result.EntriesByRoot.Add(pair.Key, pair.Value.ToArray()); } if (destructive) { EntryDetailsByRoot = null; } } result.NGramRestrictedDetails = new Dictionary <string, WordEntryDetail[]>(); foreach (var rootSet in result.EntriesByRoot) { List <WordEntryDetail> details = null; foreach (var entry in rootSet.Value) { if (nGramRestrictedFlags.ContainsAny(entry.Flags)) { if (details == null) { details = new List <WordEntryDetail>(); } details.Add(entry); } } if (details != null) { result.NGramRestrictedDetails.Add(rootSet.Key, details.ToArray()); } } return(result); }