Ekaterina
Ekaterina

Reputation: 534

How to get a sequence of letters between two letters

This is how I can get the sequence of English letters between two letters, but it works only for English. Somebody know how can I do the same thing, but for Russian alphabet. Should I somehow use Unicode representations? If you did something similar, please, let me know here.u

        public static int aMatrixDim = 10;
        public static byte aFirstChar = (byte) 'a';
        public static byte aLastChar = (byte) 'z';
        public static int aCharsCount = aLastChar - aFirstChar + 1;

        public PatternsCollection CreateTrainingPatterns(Font font)
        {
            var result = new PatternsCollection(aCharsCount, aMatrixDim*aMatrixDim, aCharsCount);
            for (var i = 0; i < aCharsCount; i++)
            {
                var aBitMatrix = CharToBitArray(Convert.ToChar(aFirstChar + i), font, aMatrixDim, 0);
                for (var j = 0; j < aMatrixDim*aMatrixDim; j++)
                    result[i].Input[j] = aBitMatrix[j];
                result[i].Output[i] = 1;
            }
            return result;
        }

Upvotes: 0

Views: 139

Answers (2)

Dmitry Egorov
Dmitry Egorov

Reputation: 9650

Unicode defines 32 out of 33 Russian alphabet letters as consecutive ranges from 0x0410 to 0x042F (for capital letters) and from 0x0430 to 0x044F (for small letters). The missing letter Ё/ё has the codes 0x0401/0x0451.

So to build a list of Russian letters you may iterate through that ranges and add the missing Ё/ё. Additional sort operation is required if you need the letters to be ordered alphabetically:

var russianSmall = Enumerable.Range(0x0430, 32)
    .Concat(new[] { 0x0451 })
    .Select(i => Convert.ToChar(i))
    .ToList();

var russianSmallOrdered = russianSmall
    .OrderBy(c => c.ToString(), StringComparer.Create(new CultureInfo("ru-RU"), false))
    .ToList();

var russianCapital = Enumerable.Range(0x410, 32)
    .Concat(new[] { 0x0401 })
    .Select(i => Convert.ToChar(i))
    .ToList();

var russianCapitalOrdered = russianCapital
    .OrderBy(c => c.ToString(), StringComparer.Create(new CultureInfo("ru-RU"), false))
    .ToList();

Demo: https://dotnetfiddle.net/NrcAUy

Upvotes: 1

degant
degant

Reputation: 4981

To fetch Cryllic capital characters (Range 0410 to 042F) in a List<char>:

char CRYLLIC_CAPITAL_START = '\x0410';
char CRYLLIC_CAPITAL_END = '\x042F';

List<char> cryllicCapitalCharacters = new List<char>();
for (char c = CRYLLIC_CAPITAL_START; c <= CRYLLIC_CAPITAL_END; c++)
{
    cryllicCapitalCharacters.Add(c);
}

Or alternatively using Linq:

cryllicCapitalCharacters = Enumerable.Range('\x0410', '\x042F' - '\x0410' + 1)
                                     .Select(x => (char)x).ToList();

To do the same for small letters, use 0430 to 044F

Russian Unicode Source: https://en.wikipedia.org/wiki/Cyrillic_script_in_Unicode

Upvotes: 2

Related Questions