public void SpellerSubstitution()
        {
            var mistakes = Speller.OptimalStringAlignmentDistance("синхрАфазАтрон", "синхрофазотрон");

            Assert.AreEqual(2, mistakes.Count);
            Assert.AreEqual(mistakes[0].Position, 5);
            Assert.AreEqual(mistakes[0].Type, CharMistakeType.Substitution);
            Assert.AreEqual(mistakes[1].Position, 9);
            Assert.AreEqual(mistakes[1].Type, CharMistakeType.Substitution);
        }
Exemplo n.º 2
0
        private void UpdateSpellerResult()
        {
            this.Invoke(new Action(() =>
            {
                try
                {
                    SpellerOptions options = 0;
                    if (cbIgnoreUppercase.Checked)
                    {
                        options |= SpellerOptions.IgnoreUppercase;
                    }
                    if (cbIgnoreDigits.Checked)
                    {
                        options |= SpellerOptions.IgnoreDigits;
                    }
                    if (cbIgnoreUrls.Checked)
                    {
                        options |= SpellerOptions.IgnoreUrls;
                    }
                    if (cbFindRepeatWords.Checked)
                    {
                        options |= SpellerOptions.FindRepeatWords;
                    }
                    if (cbIgnoreLatin.Checked)
                    {
                        options |= SpellerOptions.IgnoreLatin;
                    }
                    if (cbNoSuggest.Checked)
                    {
                        options |= SpellerOptions.NoSuggest;
                    }
                    if (cbFlagLatin.Checked)
                    {
                        options |= SpellerOptions.FlagLatin;
                    }
                    if (cbByWords.Checked)
                    {
                        options |= SpellerOptions.ByWords;
                    }
                    if (cbIgnoreCapitalization.Checked)
                    {
                        options |= SpellerOptions.IgnoreCapitalization;
                    }

                    List <Lang> langs = new List <Lang>();
                    if (cbSpellerRu.Checked)
                    {
                        langs.Add(Lang.Ru);
                    }
                    if (cbSpellerEn.Checked)
                    {
                        langs.Add(Lang.En);
                    }
                    if (cbSpellerUk.Checked)
                    {
                        langs.Add(Lang.Uk);
                    }

                    var response = Speller.CheckText(tbSpellerInput.Text, langs.ToArray(), options);
                    var errors   = response.Errors;

                    string input = tbSpellerInput.Text;

                    if (errors.Count != 0)
                    {
                        StringBuilder output   = new StringBuilder(input.Length);
                        int currentErrorNumber = 0;
                        int i                    = 0;
                        int lastInd              = 0;
                        Error currentError       = errors[0];
                        var highlightedCharPoses = new Dictionary <int, CharMistakeType>();
                        while (i < input.Length)
                        {
                            if (currentError.Column == i)
                            {
                                output.Append(input.Substring(lastInd, i - lastInd));
                                if (currentError.Steer != null)
                                {
                                    output.Append(currentError.Steer);
                                    var poses = Speller.OptimalStringAlignmentDistance(currentError.Word, currentError.Steer);
                                    foreach (var pos in poses)
                                    {
                                        highlightedCharPoses.Add(
                                            output.Length - currentError.Steer.Length + pos.Position,
                                            pos.Type);
                                    }
                                }
                                else if (cbIncludeErrorWords.Checked)
                                {
                                    output.Append(currentError.Word);
                                }

                                i += currentError.Length;
                                currentErrorNumber++;
                                if (currentErrorNumber < errors.Count)
                                {
                                    currentError = errors[currentErrorNumber];
                                }
                                lastInd = i;
                            }
                            else
                            {
                                i++;
                            }
                        }
                        output.Append(input.Substring(lastInd, i - lastInd));

                        rtbSpellerOutput.Text = output.ToString();
                        foreach (var pos in highlightedCharPoses)
                        {
                            if (pos.Value == CharMistakeType.Substitution)
                            {
                                rtbSpellerOutput.Select(pos.Key, 1);
                                rtbSpellerOutput.SelectionColor = Color.Red;
                            }
                            else if (pos.Value == CharMistakeType.Deletion)
                            {
                                if (pos.Key == 0)
                                {
                                    rtbSpellerOutput.Select(0, 1);
                                }
                                else
                                {
                                    rtbSpellerOutput.Select(pos.Key - 1, 2);
                                }
                                rtbSpellerOutput.SelectionBackColor = Color.Gold;
                            }
                            else if (pos.Value == CharMistakeType.Insertion)
                            {
                                rtbSpellerOutput.Select(pos.Key, 1);
                                rtbSpellerOutput.SelectionColor = Color.Blue;
                            }
                            else if (pos.Value == CharMistakeType.Transposition)
                            {
                                rtbSpellerOutput.Select(pos.Key, 2);
                                rtbSpellerOutput.SelectionBackColor = Color.Violet;
                            }
                        }
                    }
                    else
                    {
                        rtbSpellerOutput.Text = input;
                    }
                }
                catch (Exception ex)
                {
                    rtbSpellerOutput.Text = ex.ToString();
                }
            }));
        }