예제 #1
0
            private AffixConfig ToImmutable(bool destructive)
            {
                var culture = CultureInfo.ReadOnly(Culture ?? CultureInfo.InvariantCulture);

                var config = new AffixConfig
                {
                    Options                = Options,
                    FlagMode               = FlagMode,
                    KeyString              = Dedup(KeyString ?? DefaultKeyString),
                    TryString              = Dedup(TryString ?? string.Empty),
                    Language               = Dedup(Language ?? string.Empty),
                    Culture                = culture,
                    IsHungarian            = string.Equals(culture?.TwoLetterISOLanguageName, "HU", StringComparison.OrdinalIgnoreCase),
                    StringComparer         = new CulturedStringComparer(culture),
                    CompoundFlag           = CompoundFlag,
                    CompoundBegin          = CompoundBegin,
                    CompoundEnd            = CompoundEnd,
                    CompoundMiddle         = CompoundMiddle,
                    CompoundWordMax        = CompoundWordMax,
                    CompoundMin            = CompoundMin ?? DefaultCompoundMinLength,
                    CompoundRoot           = CompoundRoot,
                    CompoundPermitFlag     = CompoundPermitFlag,
                    CompoundForbidFlag     = CompoundForbidFlag,
                    NoSuggest              = NoSuggest,
                    NoNgramSuggest         = NoNgramSuggest,
                    ForbiddenWord          = ForbiddenWord ?? SpecialFlags.ForbiddenWord,
                    LemmaPresent           = LemmaPresent,
                    Circumfix              = Circumfix,
                    OnlyInCompound         = OnlyInCompound,
                    NeedAffix              = NeedAffix,
                    MaxNgramSuggestions    = MaxNgramSuggestions ?? DefaultMaxNgramSuggestions,
                    MaxDifferency          = MaxDifferency,
                    MaxCompoundSuggestions = MaxCompoundSuggestions ?? DefaultMaxCompoundSuggestions,
                    KeepCase               = KeepCase,
                    ForceUpperCase         = ForceUpperCase,
                    Warn                = Warn,
                    SubStandard         = SubStandard,
                    CompoundSyllableNum = CompoundSyllableNum,
                    Encoding            = Encoding,
                    CompoundMaxSyllable = CompoundMaxSyllable,
                    CompoundVowels      = CompoundVowels ?? CharacterSet.Empty,
                    WordChars           = WordChars ?? CharacterSet.Empty,
                    IgnoredChars        = IgnoredChars ?? CharacterSet.Empty,
                    Version             = Dedup(Version),
                    BreakPoints         = BreakSet.Create(BreakPoints)
                };

                if (destructive)
                {
                    config.Replacements        = SingleReplacementSet.TakeList(ReferenceHelpers.Steal(ref Replacements));
                    config.CompoundRules       = CompoundRuleSet.TakeList(ReferenceHelpers.Steal(ref CompoundRules));
                    config.CompoundPatterns    = PatternSet.TakeList(ReferenceHelpers.Steal(ref CompoundPatterns));
                    config.RelatedCharacterMap = MapTable.TakeList(ReferenceHelpers.Steal(ref RelatedCharacterMap));
                    config.Phone             = PhoneTable.TakeList(ReferenceHelpers.Steal(ref Phone));
                    config.InputConversions  = MultiReplacementTable.TakeDictionary(ReferenceHelpers.Steal(ref InputConversions));
                    config.OutputConversions = MultiReplacementTable.TakeDictionary(ReferenceHelpers.Steal(ref OutputConversions));
                    config.Warnings          = WarningList.TakeList(ReferenceHelpers.Steal(ref Warnings));

                    config.aliasF = AliasF ?? new List <FlagSet>(0);
                    AliasF        = null;
                    config.aliasM = AliasM ?? new List <MorphSet>(0);
                    AliasM        = null;
                }
                else
                {
                    config.Replacements        = SingleReplacementSet.Create(Replacements);
                    config.CompoundRules       = CompoundRuleSet.Create(CompoundRules);
                    config.CompoundPatterns    = PatternSet.Create(CompoundPatterns);
                    config.RelatedCharacterMap = MapTable.Create(RelatedCharacterMap);
                    config.Phone             = PhoneTable.Create(Phone);
                    config.InputConversions  = MultiReplacementTable.Create(InputConversions);
                    config.OutputConversions = MultiReplacementTable.Create(OutputConversions);
                    config.Warnings          = WarningList.Create(Warnings);

                    config.aliasF = AliasF == null ? new List <FlagSet>(0) : AliasF.ToList();
                    config.aliasM = AliasM == null ? new List <MorphSet>(0) : AliasM.ToList();
                }

                config.Prefixes = AffixCollection <PrefixEntry> .Create(Prefixes);

                config.Suffixes = AffixCollection <SuffixEntry> .Create(Suffixes);

                config.ContClasses = FlagSet.Union(config.Prefixes.ContClasses, config.Suffixes.ContClasses);

                return(config);
            }
예제 #2
0
            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.AllReplacements = affix.Replacements;
                if (PhoneticReplacements != null && PhoneticReplacements.Count != 0)
                {
                    // store ph: field of a morphological description in reptable
                    if (result.AllReplacements.IsEmpty)
                    {
                        result.AllReplacements = destructive
                            ? SingleReplacementSet.TakeList(ReferenceHelpers.Steal(ref PhoneticReplacements))
                            : SingleReplacementSet.Create(PhoneticReplacements);
                    }
                    else
                    {
                        result.AllReplacements = SingleReplacementSet.Create(result.AllReplacements.Concat(PhoneticReplacements));
                    }
                }

                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);
            }