Esempio n. 1
0
        public void Initialize(string languageName, SpellCheckWord word, List <string> suggestions, string paragraph, string progress)
        {
            _originalWord = word.Text;
            _suggestions  = suggestions;
            groupBoxWordNotFound.Visible  = true;
            groupBoxEditWholeText.Visible = false;
            buttonEditWholeText.Text      = Configuration.Settings.Language.SpellCheck.EditWholeText;
            Text                  = Configuration.Settings.Language.SpellCheck.Title + " [" + languageName + "] - " + progress;
            textBoxWord.Text      = word.Text;
            textBoxWholeText.Text = paragraph;
            listBoxSuggestions.Items.Clear();
            foreach (string suggestion in suggestions)
            {
                listBoxSuggestions.Items.Add(suggestion);
            }
            if (listBoxSuggestions.Items.Count > 0)
            {
                listBoxSuggestions.SelectedIndex = 0;
            }

            richTextBoxParagraph.Text = paragraph;

            FillSpellCheckDictionaries(languageName);
            ShowActiveWordWithColor(word);
            _action      = SpellCheckAction.Skip;
            DialogResult = DialogResult.None;
        }
Esempio n. 2
0
        private void ShowActiveWordWithColor(SpellCheckWord word)
        {
            richTextBoxParagraph.SelectAll();
            richTextBoxParagraph.SelectionColor  = Color.Black;
            richTextBoxParagraph.SelectionLength = 0;

            for (int i = 0; i < 10; i++)
            {
                int idx = word.Index - i;
                if (idx >= 0 && idx < richTextBoxParagraph.Text.Length && richTextBoxParagraph.Text.Substring(idx).StartsWith(word.Text))
                {
                    richTextBoxParagraph.SelectionStart  = idx;
                    richTextBoxParagraph.SelectionLength = word.Text.Length;
                    richTextBoxParagraph.SelectionColor  = Color.Red;
                    break;
                }
                idx = word.Index + i;
                if (idx >= 0 && idx < richTextBoxParagraph.Text.Length && richTextBoxParagraph.Text.Substring(idx).StartsWith(word.Text))
                {
                    richTextBoxParagraph.SelectionStart  = idx;
                    richTextBoxParagraph.SelectionLength = word.Text.Length;
                    richTextBoxParagraph.SelectionColor  = Color.Red;
                    break;
                }
            }
        }
Esempio n. 3
0
        public void ShowUnknownWord(SpellCheckWord currentSpellCheckWord, Paragraph currentParagraph)
        {
            string text = HtmlUtil.RemoveHtmlTags(currentParagraph.Text, true);

            _textViewFullText.Editable = true;
            _textViewFullText.TextStorage.SetString(new NSAttributedString(""));
            _textViewFullText.InsertText(new NSString(text));
            int idx = text.IndexOf(currentSpellCheckWord.Text);

            while (idx >= 0)
            {
                bool startOk = idx == 0 || text.Substring(idx - 1, 1).ToLower() == text.Substring(idx - 1, 1).ToUpper();
                if (startOk)
                {
                    int  endIdx = idx + currentSpellCheckWord.Text.Length;
                    bool endOk  = endIdx >= text.Length || text.Substring(endIdx, 1).ToLower() == text.Substring(endIdx, 1).ToUpper();
                    if (endOk)
                    {
                        _textViewFullText.SetTextColor(NSColor.Red, new NSRange(idx, currentSpellCheckWord.Text.Length));
                    }
                }
                if (idx < text.Length - 1)
                {
                    idx = text.IndexOf(currentSpellCheckWord.Text, idx + 1);
                }
            }
            _textViewFullText.Editable = false;

            _textWordNotFound.StringValue = currentSpellCheckWord.Text;
        }
Esempio n. 4
0
 private void UiTextBox_MouseDown(object sender, MouseEventArgs e)
 {
     if (_wrongWords?.Count > 0 && e.Clicks == 1 && e.Button == MouseButtons.Right)
     {
         int positionToSearch = GetCharIndexFromPosition(new Point(e.X, e.Y));
         var wrongWord        = _wrongWords.Where(word => positionToSearch > GetIndexWithLineBreak(word.Index) && positionToSearch < GetIndexWithLineBreak(word.Index) + word.Length).FirstOrDefault();
         if (wrongWord != null)
         {
             IsWrongWord  = true;
             _currentWord = wrongWord;
         }
         else
         {
             IsWrongWord = false;
         }
     }
 }
Esempio n. 5
0
 private void UiTextBox_KeyDown(object sender, KeyEventArgs e)
 {
     if (IsLiveSpellCheckEnabled && e.KeyCode == Keys.Apps && _wrongWords?.Count > 0)
     {
         var cursorPos = SelectionStart;
         var wrongWord = _wrongWords.Where(word => cursorPos > GetIndexWithLineBreak(word.Index) && cursorPos < GetIndexWithLineBreak(word.Index) + word.Length).FirstOrDefault();
         if (wrongWord != null)
         {
             IsWrongWord  = true;
             _currentWord = wrongWord;
         }
         else
         {
             IsWrongWord = false;
         }
     }
 }
Esempio n. 6
0
        public void DisposeHunspellAndDictionaries()
        {
            if (IsSpellCheckerInitialized)
            {
                _skipAllList         = null;
                _skipOnceList        = null;
                _spellCheckWordLists = null;
                _words             = null;
                _wrongWords        = null;
                _currentWord       = null;
                _currentDictionary = null;
                CurrentLanguage    = null;
                _hunspell?.Dispose();
                _hunspell   = null;
                IsWrongWord = false;
                IsSpellCheckerInitialized = false;

                if (Configuration.Settings.General.SubtitleTextBoxSyntaxColor)
                {
                    IsSpellCheckRequested = true;
                    TextChangedHighlight(this, EventArgs.Empty);
                }
            }
        }
Esempio n. 7
0
        private void PrepareNextWord()
        {
            while (true)
            {
                if (_wordsIndex + 1 < _words.Count)
                {
                    _wordsIndex++;
                    _currentWord = _words[_wordsIndex].Text;
                    _currentSpellCheckWord = _words[_wordsIndex];
                }
                else
                {
                    if (_currentIndex + 1 < _subtitle.Paragraphs.Count)
                    {
                        _currentIndex++;
                        _currentParagraph = _subtitle.Paragraphs[_currentIndex];
                        SetWords(_currentParagraph.Text);
                        _wordsIndex = 0;
                        if (_words.Count == 0)
                        {
                            _currentWord = string.Empty;
                        }
                        else
                        {
                            _currentWord = _words[_wordsIndex].Text;
                            _currentSpellCheckWord = _words[_wordsIndex];
                        }
                    }
                    else
                    {
                        ShowEndStatusMessage(Configuration.Settings.Language.SpellCheck.SpellCheckCompleted);
                        DialogResult = DialogResult.OK;
                        return;
                    }
                }

                int minLength = 2;
                if (Configuration.Settings.Tools.SpellCheckOneLetterWords)
                    minLength = 1;

                if (_currentWord.Trim().Length >= minLength &&
                    !_currentWord.Contains(new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '%', '&', '@', '$', '*', '=', '£', '#', '_', '½', '^' }))
                {
                    _prefix = string.Empty;
                    _postfix = string.Empty;
                    if (_currentWord.Length > 1)
                    {
                        if (_currentWord.StartsWith('\''))
                        {
                            _prefix = "'";
                            _currentWord = _currentWord.Substring(1);
                        }
                        if (_currentWord.StartsWith('`'))
                        {
                            _prefix = "`";
                            _currentWord = _currentWord.Substring(1);
                        }
                    }
                    if (_namesEtcList.Contains(_currentWord)
                        || (_currentWord.StartsWith('\'') || _currentWord.EndsWith('\'')) && _namesEtcList.Contains(_currentWord.Trim('\'')))
                    {
                        _noOfNamesEtc++;
                    }
                    else if (_skipAllList.Contains(_currentWord.ToUpper())
                        || (_currentWord.StartsWith('\'') || _currentWord.EndsWith('\'')) && _skipAllList.Contains(_currentWord.Trim('\'').ToUpper()))
                    {
                        _noOfSkippedWords++;
                    }
                    else if (_userWordList.Contains(_currentWord.ToLower())
                        || (_currentWord.StartsWith('\'') || _currentWord.EndsWith('\'')) && _userWordList.Contains(_currentWord.Trim('\'').ToLower()))
                    {
                        _noOfCorrectWords++;
                    }
                    else if (_changeAllDictionary.ContainsKey(_currentWord))
                    {
                        _noOfChangedWords++;
                        _mainWindow.CorrectWord(_changeAllDictionary[_currentWord], _currentParagraph, _currentWord, ref _firstChange);
                    }
                    else if (_changeAllDictionary.ContainsKey(_currentWord.Trim('\'')))
                    {
                        _noOfChangedWords++;
                        _mainWindow.CorrectWord(_changeAllDictionary[_currentWord], _currentParagraph, _currentWord.Trim('\''), ref _firstChange);
                    }
                    else if (_namesEtcListUppercase.Contains(_currentWord)
                        || _namesEtcListWithApostrophe.Contains(_currentWord)
                        || _namesList.IsInNamesEtcMultiWordList(_currentParagraph.Text, _currentWord)) // TODO: Verify this!
                    {
                        _noOfNamesEtc++;
                    }
                    else if (IsWordInUserPhrases(_userPhraseList, _wordsIndex, _words))
                    {
                        _noOfCorrectWords++;
                    }
                    else
                    {
                        bool correct;

                        if (_prefix == "'" && _currentWord.Length >= 1 && (DoSpell(_prefix + _currentWord) || _userWordList.Contains(_prefix + _currentWord)))
                        {
                            correct = true;
                        }
                        else if (_currentWord.Length > 1)
                        {
                            correct = DoSpell(_currentWord);
                            if (!correct && "`'".Contains(_currentWord[_currentWord.Length - 1]))
                                correct = DoSpell(_currentWord.TrimEnd('\'').TrimEnd('`'));
                            if (!correct && _currentWord.EndsWith("'s", StringComparison.Ordinal) && _currentWord.Length > 4)
                                correct = DoSpell(_currentWord.TrimEnd('s').TrimEnd('\''));
                            if (!correct && _currentWord.EndsWith('\'') && DoSpell(_currentWord.TrimEnd('\'')))
                            {
                                _currentWord = _currentWord.TrimEnd('\'');
                                correct = true;
                            }
                        }
                        else
                        {
                            correct = false;
                            if (_currentWord == "'")
                                correct = true;
                            else if (_languageName.StartsWith("en_", StringComparison.Ordinal) && (_currentWord.Equals("a", StringComparison.OrdinalIgnoreCase) || _currentWord == "I"))
                                correct = true;
                            else if (_languageName.StartsWith("da_", StringComparison.Ordinal) && _currentWord.Equals("i", StringComparison.OrdinalIgnoreCase))
                                correct = true;
                        }

                        if (!correct && Configuration.Settings.Tools.SpellCheckEnglishAllowInQuoteAsIng &&
                            _languageName.StartsWith("en_", StringComparison.Ordinal) && _currentWord.EndsWith("in'", StringComparison.OrdinalIgnoreCase))
                        {
                            correct = DoSpell(_currentWord.TrimEnd('\'') + "g");
                        }

                        if (correct)
                        {
                            _noOfCorrectWords++;
                        }
                        else
                        {
                            _mainWindow.FocusParagraph(_currentIndex);

                            List<string> suggestions = new List<string>();

                            if ((_currentWord == "Lt's" || _currentWord == "Lt'S") && _languageName.StartsWith("en_"))
                            {
                                suggestions.Add("It's");
                            }
                            else
                            {
                                if (_currentWord.ToUpper() != "LT'S" && _currentWord.ToUpper() != "SOX'S" && !_currentWord.ToUpper().StartsWith("HTTP", StringComparison.Ordinal)) // TODO: Get fixed nhunspell
                                    suggestions = DoSuggest(_currentWord); // TODO: 0.9.6 fails on "Lt'S"
                                if (_languageName.StartsWith("fr_", StringComparison.Ordinal) && (_currentWord.StartsWith("I'", StringComparison.Ordinal) || _currentWord.StartsWith("I’", StringComparison.Ordinal)))
                                {
                                    if (_currentWord.Length > 3 && Utilities.LowercaseLetters.Contains(_currentWord[2]) && _currentSpellCheckWord.Index > 3)
                                    {
                                        string ending = _currentParagraph.Text.Substring(0, _currentSpellCheckWord.Index - 1).Trim();
                                        if (ending.Length > 1 && !".!?".Contains(ending[ending.Length - 1]))
                                        {
                                            for (int i = 0; i < suggestions.Count; i++)
                                            {
                                                if (suggestions[i].StartsWith("L'") || suggestions[i].StartsWith("L’"))
                                                    suggestions[i] = @"l" + suggestions[i].Substring(1);
                                            }
                                        }
                                    }
                                }
                            }

                            suggestions.Remove(_currentWord);
                            if (_currentWord.Length == 1)
                            {
                                if ((_currentWord == "L") && _languageName.StartsWith("en_", StringComparison.Ordinal))
                                {
                                    suggestions.Remove("I");
                                    suggestions.Insert(0, "I");
                                }
                            }

                            if (AutoFixNames && _currentWord.Length > 1 && suggestions.Contains(char.ToUpper(_currentWord[0]) + _currentWord.Substring(1)))
                            {
                                ChangeWord = char.ToUpper(_currentWord[0]) + _currentWord.Substring(1);
                                DoAction(SpellCheckAction.ChangeAll);
                                return;
                            }
                            if (AutoFixNames && _currentWord.Length > 3 && suggestions.Contains(_currentWord.ToUpper()))
                            { // does not work well with two letter words like "da" and "de" which get auto-corrected to "DA" and "DE"
                                ChangeWord = _currentWord.ToUpper();
                                DoAction(SpellCheckAction.ChangeAll);
                                return;
                            }
                            if (AutoFixNames && _currentWord.Length > 1 && _namesEtcList.Contains(char.ToUpper(_currentWord[0]) + _currentWord.Substring(1)))
                            {
                                ChangeWord = char.ToUpper(_currentWord[0]) + _currentWord.Substring(1);
                                DoAction(SpellCheckAction.ChangeAll);
                                return;
                            }
                            if (_prefix != null && _prefix == "''" && _currentWord.EndsWith("''"))
                            {
                                _prefix = string.Empty;
                                _currentSpellCheckWord.Index += 2;
                                _currentWord = _currentWord.Trim('\'');
                            }
                            if (_prefix != null && _prefix == "'" && _currentWord.EndsWith('\''))
                            {
                                _prefix = string.Empty;
                                _currentSpellCheckWord.Index++;
                                _currentWord = _currentWord.Trim('\'');
                            }

                            if (_postfix != null && _postfix == "'")
                            {
                                _currentSpellCheckWord.Text = _currentWord + _postfix;
                                Initialize(_languageName, _currentSpellCheckWord, suggestions, _currentParagraph.Text, string.Format(Configuration.Settings.Language.Main.LineXOfY, (_currentIndex + 1), _subtitle.Paragraphs.Count));
                            }
                            else
                            {
                                _currentSpellCheckWord.Text = _currentWord;
                                Initialize(_languageName, _currentSpellCheckWord, suggestions, _currentParagraph.Text, string.Format(Configuration.Settings.Language.Main.LineXOfY, (_currentIndex + 1), _subtitle.Paragraphs.Count));
                            }
                            if (!Visible)
                                ShowDialog(_mainWindow);
                            return; // wait for user input
                        }

                    }
                }
            }
        }
Esempio n. 8
0
        public void Initialize(string languageName, SpellCheckWord word, List<string> suggestions, string paragraph, string progress)
        {
            _originalWord = word.Text;
            _suggestions = suggestions;
            groupBoxWordNotFound.Visible = true;
            groupBoxEditWholeText.Visible = false;
            buttonEditWholeText.Text = Configuration.Settings.Language.SpellCheck.EditWholeText;
            Text = Configuration.Settings.Language.SpellCheck.Title + " [" + languageName + "] - " + progress;
            textBoxWord.Text = word.Text;
            textBoxWholeText.Text = paragraph;
            listBoxSuggestions.Items.Clear();
            foreach (string suggestion in suggestions)
            {
                listBoxSuggestions.Items.Add(suggestion);
            }
            if (listBoxSuggestions.Items.Count > 0)
                listBoxSuggestions.SelectedIndex = 0;

            richTextBoxParagraph.Text = paragraph;

            FillSpellCheckDictionaries(languageName);
            ShowActiveWordWithColor(word);
            _action = SpellCheckAction.Skip;
            DialogResult = DialogResult.None;
        }
Esempio n. 9
0
        private void ShowActiveWordWithColor(SpellCheckWord word)
        {
            richTextBoxParagraph.SelectAll();
            richTextBoxParagraph.SelectionColor = Color.Black;
            richTextBoxParagraph.SelectionLength = 0;

            for (int i = 0; i < 10; i++)
            {
                int idx = word.Index - i;
                if (idx >= 0 && idx < richTextBoxParagraph.Text.Length && richTextBoxParagraph.Text.Substring(idx).StartsWith(word.Text))
                {
                    richTextBoxParagraph.SelectionStart = idx;
                    richTextBoxParagraph.SelectionLength = word.Text.Length;
                    richTextBoxParagraph.SelectionColor = Color.Red;
                    break;
                }
                idx = word.Index + i;
                if (idx >= 0 && idx < richTextBoxParagraph.Text.Length && richTextBoxParagraph.Text.Substring(idx).StartsWith(word.Text))
                {
                    richTextBoxParagraph.SelectionStart = idx;
                    richTextBoxParagraph.SelectionLength = word.Text.Length;
                    richTextBoxParagraph.SelectionColor = Color.Red;
                    break;
                }
            }
        }
Esempio n. 10
0
        private void PrepareNextWord()
        {
            while (true)
            {
                if (_wordsIndex + 1 < _words.Count)
                {
                    _wordsIndex++;
                    _currentWord           = _words[_wordsIndex].Text;
                    _currentSpellCheckWord = _words[_wordsIndex];
                }
                else
                {
                    if (_currentIndex + 1 < _subtitle.Paragraphs.Count)
                    {
                        _currentIndex++;
                        _currentParagraph = _subtitle.Paragraphs[_currentIndex];
                        SetWords(_currentParagraph.Text);
                        _wordsIndex = 0;
                        if (_words.Count == 0)
                        {
                            _currentWord = string.Empty;
                        }
                        else
                        {
                            _currentWord           = _words[_wordsIndex].Text;
                            _currentSpellCheckWord = _words[_wordsIndex];
                        }
                    }
                    else
                    {
                        ShowEndStatusMessage(Configuration.Settings.Language.SpellCheck.SpellCheckCompleted);
                        DialogResult = DialogResult.OK;
                        return;
                    }
                }

                int minLength = 2;
                if (Configuration.Settings.Tools.SpellCheckOneLetterWords)
                {
                    minLength = 1;
                }

                if (_currentWord.Trim().Length >= minLength &&
                    !_currentWord.Contains(ExpectedChars))
                {
                    _prefix  = string.Empty;
                    _postfix = string.Empty;
                    if (_currentWord.Length > 1)
                    {
                        if (_currentWord.StartsWith('\''))
                        {
                            _prefix      = "'";
                            _currentWord = _currentWord.Substring(1);
                        }
                        if (_currentWord.StartsWith('`'))
                        {
                            _prefix      = "`";
                            _currentWord = _currentWord.Substring(1);
                        }
                    }
                    if (_spellCheckWordLists.HasName(_currentWord))
                    {
                        _noOfNamesEtc++;
                    }
                    else if (_skipAllList.Contains(_currentWord.ToUpper()) ||
                             (_currentWord.StartsWith('\'') || _currentWord.EndsWith('\'')) && _skipAllList.Contains(_currentWord.Trim('\'').ToUpper()))
                    {
                        _noOfSkippedWords++;
                    }
                    else if (_spellCheckWordLists.HasUserWord(_currentWord))
                    {
                        _noOfCorrectWords++;
                    }
                    else if (_changeAllDictionary.ContainsKey(_currentWord))
                    {
                        _noOfChangedWords++;
                        _mainWindow.CorrectWord(_changeAllDictionary[_currentWord], _currentParagraph, _currentWord, ref _firstChange, -1);
                    }
                    else if (_changeAllDictionary.ContainsKey(_currentWord.Trim('\'')))
                    {
                        _noOfChangedWords++;
                        _mainWindow.CorrectWord(_changeAllDictionary[_currentWord.Trim('\'')], _currentParagraph, _currentWord.Trim('\''), ref _firstChange, -1);
                    }
                    else if (_spellCheckWordLists.HasNameExtended(_currentWord, _currentParagraph.Text)) // TODO: Verify this!
                    {
                        _noOfNamesEtc++;
                    }
                    else if (_spellCheckWordLists.IsWordInUserPhrases(_wordsIndex, _words))
                    {
                        _noOfCorrectWords++;
                    }
                    else
                    {
                        bool correct;

                        if (_prefix == "'" && _currentWord.Length >= 1 && (DoSpell(_prefix + _currentWord) || _spellCheckWordLists.HasUserWord(_prefix + _currentWord)))
                        {
                            correct = true;
                        }
                        else if (_currentWord.Length > 1)
                        {
                            correct = DoSpell(_currentWord);
                            if (!correct && "`'".Contains(_currentWord[_currentWord.Length - 1]))
                            {
                                correct = DoSpell(_currentWord.TrimEnd('\'').TrimEnd('`'));
                            }
                            if (!correct && _currentWord.EndsWith("'s", StringComparison.Ordinal) && _currentWord.Length > 4)
                            {
                                correct = DoSpell(_currentWord.TrimEnd('s').TrimEnd('\''));
                            }
                            if (!correct && _currentWord.EndsWith('\'') && DoSpell(_currentWord.TrimEnd('\'')))
                            {
                                _currentWord = _currentWord.TrimEnd('\'');
                                correct      = true;
                            }
                            if (!correct)
                            {
                                string removeUnicode = _currentWord.Replace(Char.ConvertFromUtf32(0x200b), string.Empty); // zero width space
                                removeUnicode = removeUnicode.Replace(Char.ConvertFromUtf32(0x2060), string.Empty);       // word joiner
                                removeUnicode = removeUnicode.Replace(Char.ConvertFromUtf32(0xfeff), string.Empty);       // zero width no-break space
                                correct       = DoSpell(removeUnicode);
                            }
                        }
                        else
                        {
                            correct = false;
                            if (_currentWord == "'")
                            {
                                correct = true;
                            }
                            else if (_languageName.StartsWith("en_", StringComparison.Ordinal) && (_currentWord.Equals("a", StringComparison.OrdinalIgnoreCase) || _currentWord == "I"))
                            {
                                correct = true;
                            }
                            else if (_languageName.StartsWith("da_", StringComparison.Ordinal) && _currentWord.Equals("i", StringComparison.OrdinalIgnoreCase))
                            {
                                correct = true;
                            }
                        }

                        if (!correct && Configuration.Settings.Tools.SpellCheckEnglishAllowInQuoteAsIng &&
                            _languageName.StartsWith("en_", StringComparison.Ordinal) && _currentWord.EndsWith("in'", StringComparison.OrdinalIgnoreCase))
                        {
                            correct = DoSpell(_currentWord.TrimEnd('\'') + "g");
                        }

                        if (correct)
                        {
                            _noOfCorrectWords++;
                        }
                        else
                        {
                            _mainWindow.FocusParagraph(_currentIndex);

                            var suggestions = new List <string>();

                            if ((_currentWord == "Lt's" || _currentWord == "Lt'S") && _languageName.StartsWith("en_"))
                            {
                                suggestions.Add("It's");
                            }
                            else
                            {
                                if (_currentWord.ToUpper() != "LT'S" && _currentWord.ToUpper() != "SOX'S" && !_currentWord.ToUpper().StartsWith("HTTP", StringComparison.Ordinal)) // TODO: Get fixed nhunspell
                                {
                                    suggestions = DoSuggest(_currentWord);                                                                                                         // TODO: 0.9.6 fails on "Lt'S"
                                }
                                if (_languageName.StartsWith("fr_", StringComparison.Ordinal) && (_currentWord.StartsWith("I'", StringComparison.Ordinal) || _currentWord.StartsWith("I’", StringComparison.Ordinal)))
                                {
                                    if (_currentWord.Length > 3 && Utilities.LowercaseLetters.Contains(_currentWord[2]) && _currentSpellCheckWord.Index > 3)
                                    {
                                        string ending = _currentParagraph.Text.Substring(0, _currentSpellCheckWord.Index - 1).Trim();
                                        if (ending.Length > 1 && !".!?".Contains(ending[ending.Length - 1]))
                                        {
                                            for (int i = 0; i < suggestions.Count; i++)
                                            {
                                                if (suggestions[i].StartsWith("L'") || suggestions[i].StartsWith("L’"))
                                                {
                                                    suggestions[i] = @"l" + suggestions[i].Substring(1);
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            suggestions.Remove(_currentWord);
                            if (_currentWord.Length == 1 && _currentWord == "L" && _languageName.StartsWith("en_", StringComparison.Ordinal))
                            {
                                suggestions.Remove("I");
                                suggestions.Insert(0, "I");
                            }

                            if (AutoFixNames && _currentWord.Length > 1 && suggestions.Contains(char.ToUpper(_currentWord[0]) + _currentWord.Substring(1)))
                            {
                                ChangeWord = char.ToUpper(_currentWord[0]) + _currentWord.Substring(1);
                                DoAction(SpellCheckAction.ChangeAll);
                                return;
                            }
                            if (AutoFixNames && _currentWord.Length > 1)
                            {
                                if (_currentWord.Length > 3 && suggestions.Contains(_currentWord.ToUpper()))
                                { // does not work well with two letter words like "da" and "de" which get auto-corrected to "DA" and "DE"
                                    ChangeWord = _currentWord.ToUpper();
                                    DoAction(SpellCheckAction.ChangeAll);
                                    return;
                                }
                                if (_spellCheckWordLists.HasName(char.ToUpper(_currentWord[0]) + _currentWord.Substring(1)))
                                {
                                    ChangeWord = char.ToUpper(_currentWord[0]) + _currentWord.Substring(1);
                                    DoAction(SpellCheckAction.ChangeAll);
                                    return;
                                }
                                if (_currentWord.Length > 3 && _currentWord.StartsWith("mc", StringComparison.InvariantCultureIgnoreCase) && _spellCheckWordLists.HasName(char.ToUpper(_currentWord[0]) + _currentWord.Substring(1, 1) + char.ToUpper(_currentWord[2]) + _currentWord.Remove(0, 3)))
                                {
                                    ChangeWord = char.ToUpper(_currentWord[0]) + _currentWord.Substring(1, 1) + char.ToUpper(_currentWord[2]) + _currentWord.Remove(0, 3);
                                    DoAction(SpellCheckAction.ChangeAll);
                                    return;
                                }
                                if (_spellCheckWordLists.HasName(_currentWord.ToUpperInvariant()))
                                {
                                    ChangeWord = _currentWord.ToUpperInvariant();
                                    DoAction(SpellCheckAction.ChangeAll);
                                    return;
                                }
                            }
                            if (_prefix != null && _prefix == "''" && _currentWord.EndsWith("''"))
                            {
                                _prefix = string.Empty;
                                _currentSpellCheckWord.Index += 2;
                                _currentWord = _currentWord.Trim('\'');
                            }
                            if (_prefix != null && _prefix == "'" && _currentWord.EndsWith('\''))
                            {
                                _prefix = string.Empty;
                                _currentSpellCheckWord.Index++;
                                _currentWord = _currentWord.Trim('\'');
                            }

                            if (_postfix != null && _postfix == "'")
                            {
                                _currentSpellCheckWord.Text = _currentWord + _postfix;
                                Initialize(_languageName, _currentSpellCheckWord, suggestions, _currentParagraph.Text, string.Format(Configuration.Settings.Language.Main.LineXOfY, (_currentIndex + 1), _subtitle.Paragraphs.Count));
                            }
                            else
                            {
                                _currentSpellCheckWord.Text = _currentWord;
                                Initialize(_languageName, _currentSpellCheckWord, suggestions, _currentParagraph.Text, string.Format(Configuration.Settings.Language.Main.LineXOfY, (_currentIndex + 1), _subtitle.Paragraphs.Count));
                            }
                            if (!Visible)
                            {
                                ShowDialog(_mainWindow);
                            }
                            return; // wait for user input
                        }
                    }
                }
            }
        }
Esempio n. 11
0
        public void ShowUnknownWord(SpellCheckWord currentSpellCheckWord, Paragraph currentParagraph)
        {
            string text = HtmlUtil.RemoveHtmlTags(currentParagraph.Text, true);
            _textViewFullText.Editable = true;
            _textViewFullText.TextStorage.SetString(new NSAttributedString(""));
            _textViewFullText.InsertText(new NSString(text));
            int idx = text.IndexOf(currentSpellCheckWord.Text);
            while (idx >= 0)
            {
                bool startOk = idx == 0 || text.Substring(idx - 1, 1).ToLower() == text.Substring(idx - 1, 1).ToUpper();
                if (startOk)
                {
                    int endIdx = idx + currentSpellCheckWord.Text.Length;
                    bool endOk = endIdx >= text.Length || text.Substring(endIdx, 1).ToLower() == text.Substring(endIdx, 1).ToUpper();
                    if (endOk)
                    {
                        _textViewFullText.SetTextColor(NSColor.Red, new NSRange(idx, currentSpellCheckWord.Text.Length));
                    }
                }
                if (idx < text.Length - 1)
                {
                    idx = text.IndexOf(currentSpellCheckWord.Text, idx + 1);
                }
            }
            _textViewFullText.Editable = false;

            _textWordNotFound.StringValue = currentSpellCheckWord.Text;
        }
Esempio n. 12
0
        private void PrepareNextWord()
        {
            while (true)
            {
                if (_wordsIndex + 1 < _words.Count)
                {
                    _wordsIndex++;
                    _currentWord = _words[_wordsIndex].Text;
                    _currentSpellCheckWord = _words[_wordsIndex];
                }
                else
                {
                    if (_currentIndex + 1 < _subtitle.Paragraphs.Count)
                    {
                        _currentIndex++;
                        _currentParagraph = _subtitle.Paragraphs[_currentIndex];
                        SetWords(_currentParagraph.Text);
                        _wordsIndex = 0;
                        if (_words.Count == 0)
                        {
                            _currentWord = string.Empty;
                        }
                        else
                        {
                            _currentWord = _words[_wordsIndex].Text;
                            _currentSpellCheckWord = _words[_wordsIndex];
                        }
                    }
                    else
                    {
                        ShowEndStatusMessage(Configuration.Settings.Language.SpellCheck.SpellCheckCompleted);
                        DialogResult = DialogResult.OK;
                        return;
                    }
                }

                int minLength = 2;
                if (Configuration.Settings.Tools.SpellCheckOneLetterWords)
                    minLength = 1;

                if (_currentWord.Trim().Length >= minLength &&
                    !_currentWord.Contains(ExpectedChars))
                {
                    _prefix = string.Empty;
                    _postfix = string.Empty;
                    if (_currentWord.Length > 1)
                    {
                        if (_currentWord.StartsWith('\''))
                        {
                            _prefix = "'";
                            _currentWord = _currentWord.Substring(1);
                        }
                        if (_currentWord.StartsWith('`'))
                        {
                            _prefix = "`";
                            _currentWord = _currentWord.Substring(1);
                        }
                    }
                    if (_spellCheckWordLists.HasName(_currentWord))
                    {
                        _noOfNamesEtc++;
                    }
                    else if (_skipAllList.Contains(_currentWord.ToUpper())
                        || (_currentWord.StartsWith('\'') || _currentWord.EndsWith('\'')) && _skipAllList.Contains(_currentWord.Trim('\'').ToUpper()))
                    {
                        _noOfSkippedWords++;
                    }
                    else if (_spellCheckWordLists.HasUserWord(_currentWord))
                    {
                        _noOfCorrectWords++;
                    }
                    else if (_changeAllDictionary.ContainsKey(_currentWord))
                    {
                        _noOfChangedWords++;
                        _mainWindow.CorrectWord(_changeAllDictionary[_currentWord], _currentParagraph, _currentWord, ref _firstChange, -1);
                    }
                    else if (_changeAllDictionary.ContainsKey(_currentWord.Trim('\'')))
                    {
                        _noOfChangedWords++;
                        _mainWindow.CorrectWord(_changeAllDictionary[_currentWord.Trim('\'')], _currentParagraph, _currentWord.Trim('\''), ref _firstChange, -1);
                    }
                    else if (_spellCheckWordLists.HasNameExtended(_currentWord, _currentParagraph.Text)) // TODO: Verify this!
                    {
                        _noOfNamesEtc++;
                    }
                    else if (_spellCheckWordLists.IsWordInUserPhrases(_wordsIndex, _words))
                    {
                        _noOfCorrectWords++;
                    }
                    else
                    {
                        bool correct;

                        if (_prefix == "'" && _currentWord.Length >= 1 && (DoSpell(_prefix + _currentWord) || _spellCheckWordLists.HasUserWord(_prefix + _currentWord)))
                        {
                            correct = true;
                        }
                        else if (_currentWord.Length > 1)
                        {
                            correct = DoSpell(_currentWord);
                            if (!correct && "`'".Contains(_currentWord[_currentWord.Length - 1]))
                                correct = DoSpell(_currentWord.TrimEnd('\'').TrimEnd('`'));
                            if (!correct && _currentWord.EndsWith("'s", StringComparison.Ordinal) && _currentWord.Length > 4)
                                correct = DoSpell(_currentWord.TrimEnd('s').TrimEnd('\''));
                            if (!correct && _currentWord.EndsWith('\'') && DoSpell(_currentWord.TrimEnd('\'')))
                            {
                                _currentWord = _currentWord.TrimEnd('\'');
                                correct = true;
                            }
                            if (!correct)
                            {
                                string removeUnicode = _currentWord.Replace(Char.ConvertFromUtf32(0x200b), string.Empty); // zero width space
                                removeUnicode = removeUnicode.Replace(Char.ConvertFromUtf32(0x2060), string.Empty); // word joiner
                                removeUnicode = removeUnicode.Replace(Char.ConvertFromUtf32(0xfeff), string.Empty); // zero width no-break space
                                correct = DoSpell(removeUnicode);
                            }
                        }
                        else
                        {
                            correct = false;
                            if (_currentWord == "'")
                                correct = true;
                            else if (_languageName.StartsWith("en_", StringComparison.Ordinal) && (_currentWord.Equals("a", StringComparison.OrdinalIgnoreCase) || _currentWord == "I"))
                                correct = true;
                            else if (_languageName.StartsWith("da_", StringComparison.Ordinal) && _currentWord.Equals("i", StringComparison.OrdinalIgnoreCase))
                                correct = true;
                        }

                        if (!correct && Configuration.Settings.Tools.SpellCheckEnglishAllowInQuoteAsIng &&
                            _languageName.StartsWith("en_", StringComparison.Ordinal) && _currentWord.EndsWith("in'", StringComparison.OrdinalIgnoreCase))
                        {
                            correct = DoSpell(_currentWord.TrimEnd('\'') + "g");
                        }

                        if (correct)
                        {
                            _noOfCorrectWords++;
                        }
                        else
                        {
                            _mainWindow.FocusParagraph(_currentIndex);

                            var suggestions = new List<string>();

                            if ((_currentWord == "Lt's" || _currentWord == "Lt'S") && _languageName.StartsWith("en_"))
                            {
                                suggestions.Add("It's");
                            }
                            else
                            {
                                if (_currentWord.ToUpper() != "LT'S" && _currentWord.ToUpper() != "SOX'S" && !_currentWord.ToUpper().StartsWith("HTTP", StringComparison.Ordinal)) // TODO: Get fixed nhunspell
                                    suggestions = DoSuggest(_currentWord); // TODO: 0.9.6 fails on "Lt'S"
                                if (_languageName.StartsWith("fr_", StringComparison.Ordinal) && (_currentWord.StartsWith("I'", StringComparison.Ordinal) || _currentWord.StartsWith("I’", StringComparison.Ordinal)))
                                {
                                    if (_currentWord.Length > 3 && char.IsLower(_currentWord[2]) && _currentSpellCheckWord.Index > 3)
                                    {
                                        string ending = _currentParagraph.Text.Substring(0, _currentSpellCheckWord.Index - 1).Trim();
                                        if (ending.Length > 1 && !".!?".Contains(ending[ending.Length - 1]))
                                        {
                                            for (int i = 0; i < suggestions.Count; i++)
                                            {
                                                if (suggestions[i].StartsWith("L'") || suggestions[i].StartsWith("L’"))
                                                    suggestions[i] = @"l" + suggestions[i].Substring(1);
                                            }
                                        }
                                    }
                                }
                            }

                            suggestions.Remove(_currentWord);
                            if (_currentWord.Length == 1 && _currentWord == "L" && _languageName.StartsWith("en_", StringComparison.Ordinal))
                            {
                                suggestions.Remove("I");
                                suggestions.Insert(0, "I");
                            }

                            if (AutoFixNames && _currentWord.Length > 1 && suggestions.Contains(char.ToUpper(_currentWord[0]) + _currentWord.Substring(1)))
                            {
                                ChangeWord = char.ToUpper(_currentWord[0]) + _currentWord.Substring(1);
                                DoAction(SpellCheckAction.ChangeAll);
                                return;
                            }
                            if (AutoFixNames && _currentWord.Length > 1)
                            {
                                if (_currentWord.Length > 3 && suggestions.Contains(_currentWord.ToUpper()))
                                { // does not work well with two letter words like "da" and "de" which get auto-corrected to "DA" and "DE"
                                    ChangeWord = _currentWord.ToUpper();
                                    DoAction(SpellCheckAction.ChangeAll);
                                    return;
                                }
                                if (_spellCheckWordLists.HasName(char.ToUpper(_currentWord[0]) + _currentWord.Substring(1)))
                                {
                                    ChangeWord = char.ToUpper(_currentWord[0]) + _currentWord.Substring(1);
                                    DoAction(SpellCheckAction.ChangeAll);
                                    return;
                                }
                                if (_currentWord.Length > 3 && _currentWord.StartsWith("mc", StringComparison.InvariantCultureIgnoreCase) && _spellCheckWordLists.HasName(char.ToUpper(_currentWord[0]) + _currentWord.Substring(1, 1 ) + char.ToUpper(_currentWord[2]) + _currentWord.Remove(0, 3)))
                                {
                                    ChangeWord = char.ToUpper(_currentWord[0]) + _currentWord.Substring(1, 1) + char.ToUpper(_currentWord[2]) + _currentWord.Remove(0, 3);
                                    DoAction(SpellCheckAction.ChangeAll);
                                    return;
                                }
                                if (_spellCheckWordLists.HasName(_currentWord.ToUpperInvariant()))
                                {
                                    ChangeWord = _currentWord.ToUpperInvariant();
                                    DoAction(SpellCheckAction.ChangeAll);
                                    return;
                                }
                            }
                            if (_prefix != null && _prefix == "''" && _currentWord.EndsWith("''"))
                            {
                                _prefix = string.Empty;
                                _currentSpellCheckWord.Index += 2;
                                _currentWord = _currentWord.Trim('\'');
                            }
                            if (_prefix != null && _prefix == "'" && _currentWord.EndsWith('\''))
                            {
                                _prefix = string.Empty;
                                _currentSpellCheckWord.Index++;
                                _currentWord = _currentWord.Trim('\'');
                            }

                            if (_postfix != null && _postfix == "'")
                            {
                                _currentSpellCheckWord.Text = _currentWord + _postfix;
                                Initialize(_languageName, _currentSpellCheckWord, suggestions, _currentParagraph.Text, string.Format(Configuration.Settings.Language.Main.LineXOfY, (_currentIndex + 1), _subtitle.Paragraphs.Count));
                            }
                            else
                            {
                                _currentSpellCheckWord.Text = _currentWord;
                                Initialize(_languageName, _currentSpellCheckWord, suggestions, _currentParagraph.Text, string.Format(Configuration.Settings.Language.Main.LineXOfY, (_currentIndex + 1), _subtitle.Paragraphs.Count));
                            }
                            if (!Visible)
                                ShowDialog(_mainWindow);
                            return; // wait for user input
                        }
                    }
                }
            }
        }
        private void PrepareNextWord()
        {
            _abort = false;
            Task.Run(() =>
                {                    
                    InvokeOnMainThread(() =>
                        {
                            while (!_abort)
                            {
                                if (_wordsIndex + 1 < _words.Count)
                                {
                                    _wordsIndex++;
                                    _currentWord = _words[_wordsIndex].Text;
                                    _currentSpellCheckWord = _words[_wordsIndex];
                                    _currentParagraph = _subtitle.Paragraphs[_currentParagraphIndex];
                                }
                                else
                                {
                                    if (_currentParagraphIndex + 1 < _subtitle.Paragraphs.Count)
                                    {
                                        _currentParagraphIndex++;
                                        _currentParagraph = _subtitle.Paragraphs[_currentParagraphIndex];
                                        _words = SpellChecker.Split(_currentParagraph.Text);
                                        _wordsIndex = 0;
                                        if (_words.Count == 0)
                                        {
                                            _currentWord = string.Empty;
                                        }
                                        else
                                        {
                                            _currentWord = _words[_wordsIndex].Text;
                                            _currentSpellCheckWord = _words[_wordsIndex];
                                            _wordsIndex = -1;
                                        }
                                    }
                                    else
                                    {
                                        Window.ShowProgress(_currentParagraphIndex, _subtitle);
                                        _abort = true;
                                        MessageBox.Show("Spell check completed" + Environment.NewLine +
                                            Environment.NewLine +
                                            "Changed words: " + _noOfChangedWords);
                                        Close();
                                        return;
                                    }
                                }

                                var spelledOk = string.IsNullOrWhiteSpace(_currentWord);

                                if (!spelledOk && _skipAllList.Contains(_currentWord))
                                {
                                    spelledOk = true;
                                }

                                if (!spelledOk && _spellCheckWordLists.HasName(_currentWord))
                                {
                                    spelledOk = true;
                                }

                                if (!spelledOk && _spellCheckWordLists.HasUserWord(_currentWord))
                                {
                                    spelledOk = true;
                                }

                                if (!spelledOk && _spellCheckWordLists.HasNameExtended(_currentWord, _currentParagraph.Text))
                                {
                                    spelledOk = true;
                                }

                                if (!spelledOk && _changeAllDictionary.ContainsKey(_currentWord))
                                {
                                    _noOfChangedWords++;
                                    _spellChecker.CorrectWord(_changeAllDictionary[_currentWord], _currentParagraph, _currentWord, 0);
                                    spelledOk = true;
                                }

                                if (!spelledOk && _changeAllDictionary.ContainsKey(_currentWord.Trim('\'')))
                                {
                                    _noOfChangedWords++;
                                    _spellChecker.CorrectWord(_changeAllDictionary[_currentWord], _currentParagraph, _currentWord.Trim('\''), 0);
                                    spelledOk = true;
                                }

                                if (!spelledOk && _wordsIndex >= 00 && _wordsIndex < _words.Count && _spellCheckWordLists.IsWordInUserPhrases(_wordsIndex, _words))
                                {
                                    spelledOk = true;
                                }
                                    
                                if (!spelledOk)
                                {
                                    spelledOk = _spellChecker.DoSpell(_currentWord);

                                    if (!spelledOk)
                                    {
                                        string removeUnicode = _currentWord.Replace(Char.ConvertFromUtf32(0x200b), string.Empty); // zero width space
                                        removeUnicode = removeUnicode.Replace(Char.ConvertFromUtf32(0x2060), string.Empty); // word joiner
                                        removeUnicode = removeUnicode.Replace(Char.ConvertFromUtf32(0xfeff), string.Empty); // zero width no-break space
                                        spelledOk = _spellChecker.DoSpell(removeUnicode);
                                    }


                                    // auto fix name
                                    if (_autoFixNames && !spelledOk)
                                    {
                                        var suggestions = _spellChecker.Suggest(_currentWord);
                                        if (!spelledOk && _autoFixNames && _currentWord.Length > 1 && suggestions.Contains(char.ToUpper(_currentWord[0]) + _currentWord.Substring(1)))
                                        {
                                            var newWord = char.ToUpper(_currentWord[0]) + _currentWord.Substring(1);
                                            _noOfChangedWords++;
                                            _spellChecker.CorrectWord(newWord, _currentParagraph, _currentWord, 0);
                                            spelledOk = true;
                                        }
                                        if (!spelledOk && _autoFixNames && _currentWord.Length > 1)
                                        {
                                            if (_currentWord.Length > 3 && suggestions.Contains(_currentWord.ToUpper()))
                                            { // does not work well with two letter words like "da" and "de" which get auto-corrected to "DA" and "DE"
                                                var newWord = _currentWord.ToUpper();
                                                _noOfChangedWords++;
                                                _spellChecker.CorrectWord(newWord, _currentParagraph, _currentWord, 0);
                                                spelledOk = true;
                                            }
                                            if (!spelledOk && _spellCheckWordLists.HasName(char.ToUpper(_currentWord[0]) + _currentWord.Substring(1)))
                                            {
                                                var newWord = char.ToUpper(_currentWord[0]) + _currentWord.Substring(1);
                                                _noOfChangedWords++;
                                                _spellChecker.CorrectWord(newWord, _currentParagraph, _currentWord, 0);
                                                spelledOk = true;
                                            }
                                            if (!spelledOk && _currentWord.Length > 3 && _currentWord.StartsWith("mc", StringComparison.InvariantCultureIgnoreCase) && _spellCheckWordLists.HasName(char.ToUpper(_currentWord[0]) + _currentWord.Substring(1, 1) + char.ToUpper(_currentWord[2]) + _currentWord.Remove(0, 3)))
                                            {
                                                var newWord = char.ToUpper(_currentWord[0]) + _currentWord.Substring(1, 1) + char.ToUpper(_currentWord[2]) + _currentWord.Remove(0, 3);
                                                _noOfChangedWords++;
                                                _spellChecker.CorrectWord(newWord, _currentParagraph, _currentWord, 0);
                                                spelledOk = true;
                                            }
                                            if (!spelledOk && _spellCheckWordLists.HasName(_currentWord.ToUpperInvariant()))
                                            {
                                                var newWord = _currentWord.ToUpperInvariant();
                                                _noOfChangedWords++;
                                                _spellChecker.CorrectWord(newWord, _currentParagraph, _currentWord, 0);
                                                spelledOk = true;
                                            }
                                        }
                                    }


                                    if (!spelledOk)
                                    {
                                        if (!_abort)
                                        {
                                            _abort = true;
                                            Window.ShowProgress(_currentParagraphIndex, _subtitle);
                                            if (_subtitleParagraphShow != null)
                                            {
                                                _subtitleParagraphShow.SubtitleParagraphShow(_currentParagraphIndex); // show current line in main window
                                            }
                                            Window.ShowSuggestions(_spellChecker.Suggest(_currentWord));
                                            Window.ShowUnknownWord(_currentSpellCheckWord, _currentParagraph);
                                            return;
                                        }
                                    }
                                }
                            }
                        });

                });
        }