Example #1
0
        public static WordList CreateFromWords(IEnumerable <string> words, AffixConfig affix)
        {
            if (words == null)
            {
                words = Enumerable.Empty <string>();
            }

            var wordListBuilder = new Builder(affix ?? new AffixConfig.Builder().MoveToImmutable());

            if (words is IList <string> wordsAsList)
            {
                wordListBuilder.InitializeEntriesByRoot(wordsAsList.Count);
            }
            else
            {
                wordListBuilder.InitializeEntriesByRoot(-1);
            }

            var entryDetail = WordEntryDetail.Default;

            foreach (var word in words)
            {
                wordListBuilder.Add(word, entryDetail);
            }

            return(wordListBuilder.MoveToImmutable());
        }
Example #2
0
 public static WordList ReadFile(string dictionaryFilePath, AffixConfig affix, WordList.Builder builder = null)
 {
     using (var stream = FileStreamEx.OpenReadFileStream(dictionaryFilePath))
     {
         return(Read(stream, affix, builder));
     }
 }
        public static WordList CreateFromWords(IEnumerable <string> words, AffixConfig affix)
        {
            var wordListBuilder = new Builder(affix ?? new AffixConfig.Builder().MoveToImmutable());

            if (words is IList <string> wordsAsList)
            {
                wordListBuilder.InitializeEntriesByRoot(wordsAsList.Count);
            }
            else
            {
                wordListBuilder.InitializeEntriesByRoot(0);
            }

            foreach (var word in words)
            {
                var wordEntry = new WordEntry(word, FlagSet.Empty, MorphSet.Empty, WordEntryOptions.None);

                WordEntrySet entryList = !wordListBuilder.EntriesByRoot.TryGetValue(word, out entryList)
                    ? WordEntrySet.Create(wordEntry)
                    : WordEntrySet.CopyWithItemAdded(entryList, wordEntry);

                wordListBuilder.EntriesByRoot.Add(word, entryList);
            }

            return(wordListBuilder.MoveToImmutable());
        }
Example #4
0
 public static async Task <WordList> ReadFileAsync(string dictionaryFilePath, AffixConfig affix, WordList.Builder builder = null)
 {
     using (var stream = FileStreamEx.OpenAsyncReadFileStream(dictionaryFilePath))
     {
         return(await ReadAsync(stream, affix, builder).ConfigureAwait(false));
     }
 }
Example #5
0
 public static WordList Read(Stream dictionaryStream, AffixConfig affix, WordList.Builder builder = null)
 {
     using (var reader = new StaticEncodingLineReader(dictionaryStream, affix.Encoding))
     {
         return(Read(reader, affix, builder));
     }
 }
Example #6
0
 public static async Task <WordList> ReadAsync(Stream dictionaryStream, AffixConfig affix, WordList.Builder builder = null)
 {
     using (var reader = new StaticEncodingLineReader(dictionaryStream, affix.Encoding))
     {
         return(await ReadAsync(reader, affix, builder).ConfigureAwait(false));
     }
 }
Example #7
0
 internal Builder(AffixConfig affix, Deduper <FlagSet> flagSetDeduper, Deduper <MorphSet> morphSet, StringDeduper stringDeduper)
 {
     Affix          = affix;
     FlagSetDeduper = flagSetDeduper ?? new Deduper <FlagSet>(FlagSet.DefaultComparer);
     FlagSetDeduper.Add(FlagSet.Empty);
     StringDeduper = stringDeduper ?? new StringDeduper();
     StringDeduper.Add(MorphologicalTags.Phon);
     MorphSetDeduper = morphSet ?? new Deduper <MorphSet>(MorphSet.DefaultComparer);
     MorphSetDeduper.Add(MorphSet.Empty);
 }
 internal Builder(AffixConfig affix, Deduper <FlagSet> flagSetDeduper, Deduper <MorphSet> morphSet)
 {
     Affix          = affix;
     FlagSetDeduper = flagSetDeduper ?? new Deduper <FlagSet>(FlagSet.DefaultComparer);
     FlagSetDeduper.Add(FlagSet.Empty);
     MorphSetDeduper = morphSet ?? new Deduper <MorphSet>(MorphSet.DefaultComparer);
     MorphSetDeduper.Add(MorphSet.Empty);
     WordEntryDetailDeduper = new Deduper <WordEntryDetail>(EqualityComparer <WordEntryDetail> .Default);
     WordEntryDetailDeduper.Add(WordEntryDetail.Default);
 }
Example #9
0
        public static WordList Read(IHunspellLineReader dictionaryReader, AffixConfig affix, WordList.Builder builder = null)
        {
            var readerInstance = new WordListReader(builder, affix);

            string line;

            while ((line = dictionaryReader.ReadLine()) != null)
            {
                readerInstance.ParseLine(line);
            }

            return(readerInstance.Builder.MoveToImmutable());
        }
Example #10
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);
            }
Example #11
0
 public Builder(AffixConfig affix)
     : this(affix, null, null, null)
 {
 }
 private WordListReader(WordList.Builder builder, AffixConfig affix)
 {
     Builder = builder ?? new WordList.Builder(affix);
     Affix   = affix;
 }
Example #13
0
        public static async Task <WordList> ReadAsync(IHunspellLineReader dictionaryReader, AffixConfig affix, WordList.Builder builder = null)
        {
            var readerInstance = new WordListReader(builder, affix);

            string line;

            while ((line = await dictionaryReader.ReadLineAsync().ConfigureAwait(false)) != null)
            {
                readerInstance.ParseLine(line);
            }

            return(readerInstance.Builder.MoveToImmutable());
        }
 private WordList(AffixConfig affix) =>
Example #15
0
        internal static CapitalizationType GetCapitalizationType(StringSlice word, AffixConfig affix)
        {
            if (word.IsNullOrEmpty)
            {
                return(CapitalizationType.None);
            }

            var hasFoundMoreCaps = false;
            var firstIsUpper     = false;
            var hasLower         = false;
            var c = word.Text[word.Offset];

            if (char.IsUpper(c))
            {
                firstIsUpper = true;
            }
            else if (!CharIsNeutral(c, affix))
            {
                hasLower = true;
            }

            var wordIndexLimit = word.Length + word.Offset;

            for (int i = word.Offset + 1; i < wordIndexLimit; i++)
            {
                c = word.Text[i];

                if (char.IsUpper(c))
                {
                    hasFoundMoreCaps = true;
                }
                else if (!CharIsNeutral(c, affix))
                {
                    hasLower = true;
                }

                if (hasLower && hasFoundMoreCaps)
                {
                    break;
                }
            }

            if (firstIsUpper)
            {
                if (!hasFoundMoreCaps)
                {
                    return(CapitalizationType.Init);
                }
                if (!hasLower)
                {
                    return(CapitalizationType.All);
                }

                return(CapitalizationType.HuhInit);
            }
            else
            {
                if (!hasFoundMoreCaps)
                {
                    return(CapitalizationType.None);
                }
                if (!hasLower)
                {
                    return(CapitalizationType.All);
                }

                return(CapitalizationType.Huh);
            }
        }
Example #16
0
 public static CapitalizationType GetCapitalizationType(string word, AffixConfig affix) =>
 GetCapitalizationType(StringSlice.Create(word), affix);
Example #17
0
 private static bool CharIsNeutral(char c, AffixConfig affix) =>
 !char.IsLower(c) || (c > 127 && affix.Culture.TextInfo.ToUpper(c) == c);
Example #18
0
 private WordList(AffixConfig affix)
 {
     Affix = affix;
 }