internal CaseWordVocabularySource(ApplicationModel model, WordVocabularySource source, HeadWordItem target)
            : base(model)
        {
            _source = source;
            _target = target;

            var substitutes = new List <string>();

            var tile       = TileData.FromTokenString(target.Content);
            var content    = tile.Content;
            var isPrefix   = tile.IsPrefix;
            var isSuffix   = tile.IsSuffix;
            var attributes = tile.Attributes;
            var included   = new HashSet <string> {
                target.Content
            };

            var map = WordCaseMap.Create(content);

            if (map.LetterCount != 0)
            {
                var position0 = map.Positions[0];

                CheckedAdd(model.HeadItems[0].Culture.TextInfo.ToTitleCase(content));
                CheckedAdd(content.ToLower());
                CheckedAdd(content.ToUpper());
                CheckedAdd(content.Substring(0, position0) + char.ToUpper(content[position0]) + content.Substring(position0 + 1));

                for (var i = 0; i < map.Positions.Length; i++)
                {
                    var position = map.Positions[i];
                    var ch       = map.Uppers[i] ? char.ToLower(content[position]) : char.ToUpper(content[position]);
                    var cased    = content.Substring(0, position) + ch + content.Substring(position + 1);
                    CheckedAdd(cased);
                }

                void CheckedAdd(string newContent)
                {
                    var newTile = TileData.Create(content: newContent, isSuffix: isPrefix, isPrefix: isSuffix, attributes: attributes);
                    var version = newTile.ToTokenString();

                    if (included.Add(version))
                    {
                        substitutes.Add(version);
                    }
                }
            }

            for (var i = 0; i < 4; i++)
            {
                var spacedTile    = TileData.Create(content: content, isSuffix: (i & 1) != 0, isPrefix: (i & 2) != 0, attributes: attributes);
                var spacedContent = spacedTile.ToTokenString();
                if (included.Add(spacedContent))
                {
                    substitutes.Add(spacedContent);
                }
            }

            _substitutes = substitutes.ToArray();
        }
Exemplo n.º 2
0
        internal static WordCaseMap Create(string word)
        {
            var lowerCount = 0;
            var upperCount = 0;
            var positions  = new List <int>();
            var uppers     = new List <bool>();

            for (var position = 0; position < word.Length; position++)
            {
                var ch = word[position];

                if (char.IsLetter(ch))
                {
                    var lower = char.ToLower(ch);
                    var upper = char.ToUpper(ch);
                    if (lower != upper)
                    {
                        if (ch == lower)
                        {
                            if (ch != upper)
                            {
                                lowerCount++;
                                positions.Add(position);
                                uppers.Add(false);
                            }
                        }
                        else if (ch == upper)
                        {
                            if (ch != lower)
                            {
                                upperCount++;
                                positions.Add(position);
                                uppers.Add(true);
                            }
                        }
                    }
                }
            }

            Debug.Assert(lowerCount + upperCount == positions.Count);
            Debug.Assert(positions.Count == uppers.Count);

            var map = new WordCaseMap(positions.ToArray(), uppers.ToArray(), lowerCount);

            return(map);
        }
Exemplo n.º 3
0
        internal int GetDistanceTo(WordCaseMap other)
        {
            var distance = 0;

            Debug.Assert(Positions.Length == other.Positions.Length);

            for (var i = 0; i < Positions.Length; i++)
            {
                Debug.Assert(Positions[i] == other.Positions[i]);

                if (Uppers[i] != other.Uppers[i])
                {
                    distance++;
                }
            }

            return(distance);
        }