private void ChangeEncoding(string encodingName)
        {
            var newEncoding = EncodingEx.GetEncodingByName(encodingName);

            if (newEncoding == null || ReferenceEquals(newEncoding, encoding) || encoding.Equals(newEncoding))
            {
                return;
            }

            decoder  = newEncoding.GetDecoder();
            encoding = newEncoding;
        }
Ejemplo n.º 2
0
        private bool TryHandleParameterizedCommand(string name, string parameters)
        {
            var commandName = name.ToUpperInvariant();

            switch (commandName)
            {
            case "FLAG":     // parse in the try string
                return(TrySetFlagMode(parameters));

            case "KEY":     // parse in the keyboard string
                Builder.KeyString = Builder.Dedup(parameters);
                return(true);

            case "TRY":     // parse in the try string
                Builder.TryString = Builder.Dedup(parameters);
                return(true);

            case "SET":     // parse in the name of the character set used by the .dict and .aff
                var encoding = EncodingEx.GetEncodingByName(parameters);
                if (encoding == null)
                {
                    Builder.LogWarning("Failed to get encoding: " + parameters);
                    return(false);
                }

                Builder.Encoding = encoding;
                return(true);

            case "LANG":     // parse in the language for language specific codes
                Builder.Language = Builder.Dedup(parameters.Trim());
                Builder.Culture  = GetCultureFromLanguage(Builder.Language);
                return(true);

            case "SYLLABLENUM":     // parse in the flag used by compound_check() method
                Builder.CompoundSyllableNum = Builder.Dedup(parameters);
                return(true);

            case "WORDCHARS":     // parse in the extra word characters
                Builder.WordChars = CharacterSet.Create(parameters);
                return(true);

            case "IGNORE":     // parse in the ignored characters (for example, Arabic optional diacretics characters)
                Builder.IgnoredChars = CharacterSet.Create(parameters);
                return(true);

            case "COMPOUNDFLAG":     // parse in the flag used by the controlled compound words
                return(TryParseFlag(parameters, out Builder.CompoundFlag));

            case "COMPOUNDMIDDLE":     // parse in the flag used by compound words
                return(TryParseFlag(parameters, out Builder.CompoundMiddle));

            case "COMPOUNDBEGIN":     // parse in the flag used by compound words
                return(EnumEx.HasFlag(Builder.Options, AffixConfigOptions.ComplexPrefixes)
                        ? TryParseFlag(parameters, out Builder.CompoundEnd)
                        : TryParseFlag(parameters, out Builder.CompoundBegin));

            case "COMPOUNDEND":     // parse in the flag used by compound words
                return(EnumEx.HasFlag(Builder.Options, AffixConfigOptions.ComplexPrefixes)
                        ? TryParseFlag(parameters, out Builder.CompoundBegin)
                        : TryParseFlag(parameters, out Builder.CompoundEnd));

            case "COMPOUNDWORDMAX":     // parse in the data used by compound_check() method
                Builder.CompoundWordMax = IntEx.TryParseInvariant(parameters);
                return(Builder.CompoundWordMax.HasValue);

            case "COMPOUNDMIN":     // parse in the minimal length for words in compounds
                Builder.CompoundMin = IntEx.TryParseInvariant(parameters);
                if (!Builder.CompoundMin.HasValue)
                {
                    Builder.LogWarning("Failed to parse CompoundMin: " + parameters);
                    return(false);
                }

                if (Builder.CompoundMin.GetValueOrDefault() < 1)
                {
                    Builder.CompoundMin = 1;
                }

                return(true);

            case "COMPOUNDROOT":     // parse in the flag sign compounds in dictionary
                return(TryParseFlag(parameters, out Builder.CompoundRoot));

            case "COMPOUNDPERMITFLAG":     // parse in the flag used by compound_check() method
                return(TryParseFlag(parameters, out Builder.CompoundPermitFlag));

            case "COMPOUNDFORBIDFLAG":     // parse in the flag used by compound_check() method
                return(TryParseFlag(parameters, out Builder.CompoundForbidFlag));

            case "COMPOUNDSYLLABLE":     // parse in the max. words and syllables in compounds
                return(TryParseCompoundSyllable(parameters));

            case "NOSUGGEST":
                return(TryParseFlag(parameters, out Builder.NoSuggest));

            case "NONGRAMSUGGEST":
                return(TryParseFlag(parameters, out Builder.NoNgramSuggest));

            case "FORBIDDENWORD":     // parse in the flag used by forbidden words
                Builder.ForbiddenWord = TryParseFlag(parameters);
                return(Builder.ForbiddenWord.HasValue);

            case "LEMMA_PRESENT":     // parse in the flag used by forbidden words
                return(TryParseFlag(parameters, out Builder.LemmaPresent));

            case "CIRCUMFIX":     // parse in the flag used by circumfixes
                return(TryParseFlag(parameters, out Builder.Circumfix));

            case "ONLYINCOMPOUND":     // parse in the flag used by fogemorphemes
                return(TryParseFlag(parameters, out Builder.OnlyInCompound));

            case "PSEUDOROOT":    // parse in the flag used by `needaffixs'
            case "NEEDAFFIX":     // parse in the flag used by `needaffixs'
                return(TryParseFlag(parameters, out Builder.NeedAffix));

            case "REP":     // parse in the typical fault correcting table
                return(TryParseStandardListItem(EntryListType.Replacements, parameters, ref Builder.Replacements, TryParseReplacements));

            case "ICONV":     // parse in the input conversion table
                return(TryParseConv(parameters, EntryListType.Iconv, ref Builder.InputConversions));

            case "OCONV":     // parse in the output conversion table
                return(TryParseConv(parameters, EntryListType.Oconv, ref Builder.OutputConversions));

            case "PHONE":     // parse in the phonetic conversion table
                return(TryParseStandardListItem(EntryListType.Phone, parameters, ref Builder.Phone, TryParsePhone));

            case "CHECKCOMPOUNDPATTERN":     // parse in the checkcompoundpattern table
                return(TryParseStandardListItem(EntryListType.CompoundPatterns, parameters, ref Builder.CompoundPatterns, TryParseCheckCompoundPatternIntoCompoundPatterns));

            case "COMPOUNDRULE":     // parse in the defcompound table
                return(TryParseStandardListItem(EntryListType.CompoundRules, parameters, ref Builder.CompoundRules, TryParseCompoundRuleIntoList));

            case "MAP":     // parse in the related character map table
                return(TryParseStandardListItem(EntryListType.Map, parameters, ref Builder.RelatedCharacterMap, TryParseMapEntry));

            case "BREAK":     // parse in the word breakpoints table
                return(TryParseStandardListItem(EntryListType.Break, parameters, ref Builder.BreakPoints, TryParseBreak));

            case "VERSION":
                Builder.Version = parameters;
                return(true);

            case "MAXNGRAMSUGS":
                Builder.MaxNgramSuggestions = IntEx.TryParseInvariant(parameters);
                return(Builder.MaxNgramSuggestions.HasValue);

            case "MAXDIFF":
                Builder.MaxDifferency = IntEx.TryParseInvariant(parameters);
                return(Builder.MaxDifferency.HasValue);

            case "MAXCPDSUGS":
                Builder.MaxCompoundSuggestions = IntEx.TryParseInvariant(parameters);
                return(Builder.MaxCompoundSuggestions.HasValue);

            case "KEEPCASE":     // parse in the flag used by forbidden words
                return(TryParseFlag(parameters, out Builder.KeepCase));

            case "FORCEUCASE":
                return(TryParseFlag(parameters, out Builder.ForceUpperCase));

            case "WARN":
                return(TryParseFlag(parameters, out Builder.Warn));

            case "SUBSTANDARD":
                return(TryParseFlag(parameters, out Builder.SubStandard));

            case "PFX":
            case "SFX":
                var parseAsPrefix = "PFX" == commandName;
                if (EnumEx.HasFlag(Builder.Options, AffixConfigOptions.ComplexPrefixes))
                {
                    parseAsPrefix = !parseAsPrefix;
                }

                return(parseAsPrefix
                        ? TryParseAffixIntoList(parameters, ref Builder.Prefixes)
                        : TryParseAffixIntoList(parameters, ref Builder.Suffixes));

            case "AF":
                return(TryParseStandardListItem(EntryListType.AliasF, parameters, ref Builder.AliasF, TryParseAliasF));

            case "AM":
                return(TryParseStandardListItem(EntryListType.AliasM, parameters, ref Builder.AliasM, TryParseAliasM));

            default:
                Builder.LogWarning($"Unknown command {commandName} with params: {parameters}");
                return(false);
            }
        }
        private void ChangeEncoding(ReadOnlySpan <char> encodingName)
        {
            var newEncoding = EncodingEx.GetEncodingByName(encodingName);

            ChangeEncoding(newEncoding);
        }
Ejemplo n.º 4
0
        private bool TryHandleParameterizedCommand(string commandName, string parameters)
        {
            if (commandName == null || !CommandMap.TryGetValue(commandName, out AffixReaderCommandKind command))
            {
                Builder.LogWarning($"Unknown command {commandName} with params: {parameters}");
                return(false);
            }

            switch (command)
            {
            case AffixReaderCommandKind.Flag:
                return(TrySetFlagMode(parameters));

            case AffixReaderCommandKind.KeyString:
                Builder.KeyString = Builder.Dedup(parameters);
                return(true);

            case AffixReaderCommandKind.TryString:
                Builder.TryString = Builder.Dedup(parameters);
                return(true);

            case AffixReaderCommandKind.SetEncoding:
                var encoding = EncodingEx.GetEncodingByName(parameters);
                if (encoding == null)
                {
                    Builder.LogWarning("Failed to get encoding: " + parameters);
                    return(false);
                }

                Builder.Encoding = encoding;
                return(true);

            case AffixReaderCommandKind.Language:
                Builder.Language = Builder.Dedup(parameters.Trim());
                Builder.Culture  = GetCultureFromLanguage(Builder.Language);
                return(true);

            case AffixReaderCommandKind.CompoundSyllableNum:
                Builder.CompoundSyllableNum = Builder.Dedup(parameters);
                return(true);

            case AffixReaderCommandKind.WordChars:
                Builder.WordChars = CharacterSet.Create(parameters);
                return(true);

            case AffixReaderCommandKind.Ignore:
                Builder.IgnoredChars = CharacterSet.Create(parameters);
                return(true);

            case AffixReaderCommandKind.CompoundFlag:
                return(TryParseFlag(parameters, out Builder.CompoundFlag));

            case AffixReaderCommandKind.CompoundMiddle:
                return(TryParseFlag(parameters, out Builder.CompoundMiddle));

            case AffixReaderCommandKind.CompoundBegin:
                return(EnumEx.HasFlag(Builder.Options, AffixConfigOptions.ComplexPrefixes)
                        ? TryParseFlag(parameters, out Builder.CompoundEnd)
                        : TryParseFlag(parameters, out Builder.CompoundBegin));

            case AffixReaderCommandKind.CompoundEnd:
                return(EnumEx.HasFlag(Builder.Options, AffixConfigOptions.ComplexPrefixes)
                        ? TryParseFlag(parameters, out Builder.CompoundBegin)
                        : TryParseFlag(parameters, out Builder.CompoundEnd));

            case AffixReaderCommandKind.CompoundWordMax:
                Builder.CompoundWordMax = IntEx.TryParseInvariant(parameters);
                return(Builder.CompoundWordMax.HasValue);

            case AffixReaderCommandKind.CompoundMin:
                Builder.CompoundMin = IntEx.TryParseInvariant(parameters);
                if (!Builder.CompoundMin.HasValue)
                {
                    Builder.LogWarning("Failed to parse CompoundMin: " + parameters);
                    return(false);
                }

                if (Builder.CompoundMin.GetValueOrDefault() < 1)
                {
                    Builder.CompoundMin = 1;
                }

                return(true);

            case AffixReaderCommandKind.CompoundRoot:
                return(TryParseFlag(parameters, out Builder.CompoundRoot));

            case AffixReaderCommandKind.CompoundPermitFlag:
                return(TryParseFlag(parameters, out Builder.CompoundPermitFlag));

            case AffixReaderCommandKind.CompoundForbidFlag:
                return(TryParseFlag(parameters, out Builder.CompoundForbidFlag));

            case AffixReaderCommandKind.CompoundSyllable:
                return(TryParseCompoundSyllable(parameters));

            case AffixReaderCommandKind.NoSuggest:
                return(TryParseFlag(parameters, out Builder.NoSuggest));

            case AffixReaderCommandKind.NoNGramSuggest:
                return(TryParseFlag(parameters, out Builder.NoNgramSuggest));

            case AffixReaderCommandKind.ForbiddenWord:
                Builder.ForbiddenWord = TryParseFlag(parameters);
                return(Builder.ForbiddenWord.HasValue);

            case AffixReaderCommandKind.LemmaPresent:
                return(TryParseFlag(parameters, out Builder.LemmaPresent));

            case AffixReaderCommandKind.Circumfix:
                return(TryParseFlag(parameters, out Builder.Circumfix));

            case AffixReaderCommandKind.OnlyInCompound:
                return(TryParseFlag(parameters, out Builder.OnlyInCompound));

            case AffixReaderCommandKind.NeedAffix:
                return(TryParseFlag(parameters, out Builder.NeedAffix));

            case AffixReaderCommandKind.Replacement:
                return(TryParseStandardListItem(EntryListType.Replacements, parameters, ref Builder.Replacements, TryParseReplacements));

            case AffixReaderCommandKind.InputConversions:
                return(TryParseConv(parameters, EntryListType.Iconv, ref Builder.InputConversions));

            case AffixReaderCommandKind.OutputConversions:
                return(TryParseConv(parameters, EntryListType.Oconv, ref Builder.OutputConversions));

            case AffixReaderCommandKind.Phone:
                return(TryParseStandardListItem(EntryListType.Phone, parameters, ref Builder.Phone, TryParsePhone));

            case AffixReaderCommandKind.CheckCompoundPattern:
                return(TryParseStandardListItem(EntryListType.CompoundPatterns, parameters, ref Builder.CompoundPatterns, TryParseCheckCompoundPatternIntoCompoundPatterns));

            case AffixReaderCommandKind.CompoundRule:
                return(TryParseStandardListItem(EntryListType.CompoundRules, parameters, ref Builder.CompoundRules, TryParseCompoundRuleIntoList));

            case AffixReaderCommandKind.Map:
                return(TryParseStandardListItem(EntryListType.Map, parameters, ref Builder.RelatedCharacterMap, TryParseMapEntry));

            case AffixReaderCommandKind.Break:
                return(TryParseStandardListItem(EntryListType.Break, parameters, ref Builder.BreakPoints, TryParseBreak));

            case AffixReaderCommandKind.Version:
                Builder.Version = parameters;
                return(true);

            case AffixReaderCommandKind.MaxNgramSuggestions:
                Builder.MaxNgramSuggestions = IntEx.TryParseInvariant(parameters);
                return(Builder.MaxNgramSuggestions.HasValue);

            case AffixReaderCommandKind.MaxDifferency:
                Builder.MaxDifferency = IntEx.TryParseInvariant(parameters);
                return(Builder.MaxDifferency.HasValue);

            case AffixReaderCommandKind.MaxCompoundSuggestions:
                Builder.MaxCompoundSuggestions = IntEx.TryParseInvariant(parameters);
                return(Builder.MaxCompoundSuggestions.HasValue);

            case AffixReaderCommandKind.KeepCase:
                return(TryParseFlag(parameters, out Builder.KeepCase));

            case AffixReaderCommandKind.ForceUpperCase:
                return(TryParseFlag(parameters, out Builder.ForceUpperCase));

            case AffixReaderCommandKind.Warn:
                return(TryParseFlag(parameters, out Builder.Warn));

            case AffixReaderCommandKind.SubStandard:
                return(TryParseFlag(parameters, out Builder.SubStandard));

            case AffixReaderCommandKind.Prefix:
            case AffixReaderCommandKind.Suffix:
                var parseAsPrefix = AffixReaderCommandKind.Prefix == command;
                if (EnumEx.HasFlag(Builder.Options, AffixConfigOptions.ComplexPrefixes))
                {
                    parseAsPrefix = !parseAsPrefix;
                }

                return(parseAsPrefix
                        ? TryParseAffixIntoList(parameters, ref Builder.Prefixes)
                        : TryParseAffixIntoList(parameters, ref Builder.Suffixes));

            case AffixReaderCommandKind.AliasF:
                return(TryParseStandardListItem(EntryListType.AliasF, parameters, ref Builder.AliasF, TryParseAliasF));

            case AffixReaderCommandKind.AliasM:
                return(TryParseStandardListItem(EntryListType.AliasM, parameters, ref Builder.AliasM, TryParseAliasM));

            default:
                Builder.LogWarning($"Unknown parsed command {command}");
                return(false);
            }
        }
        private void ChangeEncoding(StringSlice encodingName)
        {
            var newEncoding = EncodingEx.GetEncodingByName(encodingName);

            ChangeEncoding(newEncoding);
        }