File: spellcheck.py

package info (click to toggle)
treeline 3.1.5-1.1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 6,508 kB
  • sloc: python: 20,489; javascript: 998; makefile: 54
file content (562 lines) | stat: -rw-r--r-- 22,396 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#!/usr/bin/env python3

#******************************************************************************
# spellcheck.py, provides classes for spell check interfaces and dialogs,
# including interfaces to aspell, ispell, hunspell.
#
# TreeLine, an information storage program
# Copyright (C) 2020, Douglas W. Bell
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License, either Version 2 or any later
# version.  This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY.  See the included LICENSE file for details.
#******************************************************************************

import re
import sys
import subprocess
import collections
from PyQt5.QtCore import QSize, Qt, pyqtSignal
from PyQt5.QtGui import QFontMetrics, QTextCursor
from PyQt5.QtWidgets import (QApplication, QDialog, QFileDialog, QGroupBox,
                             QHBoxLayout, QLabel, QLineEdit, QListWidget,
                             QMessageBox, QPushButton, QTextEdit, QVBoxLayout)
import undo
import globalref

_guessRe = re.compile(r'[&?] (\S+) \d+ (\d+): (.+)')
_noGuessRe = re.compile(r'# (\S+) (\d+)')


class SpellCheckInterface:
    """Interfaces with aspell, ispell or hunspell and stores session hooks.
    """
    def __init__(self, spellPath='', langCode=''):
        """Create initial hooks to outside program.
        
        Arguments:
            spellPath -- use to find engine executable if given
            langCode -- language code to pass to aspell if given
        """
        engineOptions = collections.OrderedDict()
        engineOptions.update([('aspell', ['-a -H --encoding=utf-8']),
                              ('ispell', ['-a -h -Tutf8', '-a']),
                              ('hunspell', ['-a -H -i utf-8'])])
        langPrefix = {'aspell': 'l', 'ispell': 'd', 'hunspell': 'd'}
        if spellPath:
            newEngineOptions = {}
            for engine in engineOptions.keys():
                if engine in spellPath:
                    newEngineOptions[spellPath] = engineOptions[engine]
            engineOptions = newEngineOptions
        for engine, options in engineOptions.items():
            if langCode:
                options.insert(0, '{0} -{1} {2}'.format(options[0],
                                                        langPrefix[engine],
                                                        langCode))
            for option in options:
                cmd = '{0} {1}'.format(engine, option)
                try:
                    p = subprocess.Popen(cmd, shell=True,
                                         stdin=subprocess.PIPE,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE)
                    self.stdIn = p.stdin
                    self.stdOut = p.stdout
                    self.stdOut.readline()  # read header
                    # set terse mode (no correct returns)
                    self.stdIn.write(b'!\n')
                    self.stdIn.flush()
                    return
                except IOError:
                    pass
        raise SpellCheckError('Could not initialize aspell, ispell or '
                              'hunspell')
 
    def checkLine(self, line, skipWords=None):
        """Check one (and only one) line of text.

        Return a list of tuples, each with the mispelled word, position in
        the line, and a list of suggestions.
        Arguments:
            line -- the text string to check
            skipWords -- a set of words to ignore if given
        """
        if not skipWords:
            skipWords = set()
        self.stdIn.write('^{0}\n'.format(line).encode('utf-8'))
        self.stdIn.flush()
        outputs = [self.stdOut.readline()]
        while outputs[-1].strip():
            outputs.append(self.stdOut.readline())
        results = []
        for output in outputs:
            output = output.decode('utf-8').strip()
            match = _guessRe.match(output)
            if match:
                guesses = match.group(3).split(', ')
            else:
                match = _noGuessRe.match(output)
                guesses = []
            if match:
                word = match.group(1)
                if word not in skipWords:
                    wordPos = int(match.group(2)) - 1
                    # work around unicode bug in older versions of aspell
                    while (line[wordPos:wordPos + len(word)] != word and
                           wordPos > 0):
                        wordPos -= 1
                    results.append((word, wordPos, guesses))
        return results

    def close(self):
        """Shut down hooks to outside program.
        """
        self.stdIn.close()
        self.stdOut.close()

    def acceptWord(self, word):
        """Accept given word for the remainder of this session.

        Arguments:
            word -- the word to accept
        """
        self.stdIn.write('@{0}\n'.format(word).encode('utf-8'))
        self.stdIn.flush()

    def addToDict(self, word, lowCase=False):
        """Add word to spell check engine's dictionary.

        Arguments:
            word -- the word to add
            lowCase -- if True, add the word as a lower case word
        """
        if lowCase:
            self.stdIn.write('&{0}\n'.format(word).encode('utf-8'))
        else:
            self.stdIn.write('*{0}\n'.format(word).encode('utf-8'))
        self.stdIn.write(b'#\n')  # saves dict
        self.stdIn.flush()


class SpellCheckError(Exception):
    """Exception class for errors interfacing with the spell check engine.
    """
    pass


# console test for the spell check engine interface
if __name__ == '__main__':
    try:
        sp = SpellCheckInterface()
    except SpellCheckError:
        print('Error - could not initialize aspell, ispell or hunspell')
        sys.exit()
    while True:
        s = input('Enter line-> ').strip()
        if not s:
            sys.exit()
        if s.startswith('Accept->'):
            sp.acceptWord(s[8:])
        elif s.startswith('Add->'):
            sp.addToDict(s[5:])
        elif s.startswith('AddLow->'):
            sp.addToDict(s[8:], True)
        else:
            for word, pos, suggests in sp.checkLine(s):
                print('{0} @{1}: {2}\n'.format(word, pos, ', '.join(suggests)))
    sp.close()


class SpellCheckOperation:
    """Feeds tree node text to the spell check dialog.
    """
    def __init__(self, controlRef):
        """Initialize the spell check engine interface.

        Arguments:
            controlRef - the local control
        """
        self.controlRef = controlRef
        self.selectModel = controlRef.currentSelectionModel()
        self.currentSpot = None
        self.currentField = ''
        self.lineNum = 0
        self.textLine = ''
        parentWidget = QApplication.activeWindow()
        path = globalref.miscOptions['SpellCheckPath']
        while True:
            try:
                self.spellCheckInterface = SpellCheckInterface(path,
                                                               self.controlRef.
                                                               spellCheckLang)
                return
            except SpellCheckError:
                if path:
                    path = ''
                else:
                    if sys.platform.startswith('win'):
                        prompt = (_('Could not find either aspell.exe, '
                                    'ispell.exe or hunspell.exe\n'
                                    'Browse for location?'))
                        ans = QMessageBox.warning(parentWidget,
                                                      _('Spell Check Error'),
                                                      prompt,
                                                      QMessageBox.Yes |
                                                      QMessageBox.Cancel,
                                                      QMessageBox.Yes)
                        if ans == QMessageBox.Cancel:
                            raise
                        title = _('Locate aspell.exe, ipsell.exe or '
                                  'hunspell.exe')
                        path, fltr = QFileDialog.getOpenFileName(parentWidget,
                                                          title, '',
                                                          _('Program (*.exe)'))
                        if path:
                            path = path[:-4]
                            if ' ' in path:
                                path = '"{0}"'.format(path)
                            globalref.miscOptions.changeValue('SpellCheckPath',
                                                              path)
                            globalref.miscOptions.writeFile()
                    else:
                        prompt = (_('TreeLine Spell Check Error\nMake sure '
                                    'aspell, ispell or hunspell is installed'))
                        QMessageBox.warning(parentWidget, 'TreeLine', prompt)
                        raise

    def spellCheck(self):
        """Spell check starting with the selected branches.
        """
        parentWidget = QApplication.activeWindow()
        spellCheckDialog = SpellCheckDialog(self.spellCheckInterface,
                                            parentWidget)
        spellCheckDialog.misspellFound.connect(self.updateSelection)
        spellCheckDialog.changeRequest.connect(self.changeNode)
        origBranches = self.selectModel.selectedBranchSpots()
        if not origBranches:
            origBranches = self.controlRef.structure.rootSpots()
        result = (spellCheckDialog.
                  startSpellCheck(self.textLineGenerator(origBranches)))
        self.selectModel.selectSpots(origBranches, expandParents = True)
        if result and origBranches[0].parentSpot.parentSpot:
            prompt = _('Finished checking the branch\nContinue from the top?')
            ans = QMessageBox.information(parentWidget,
                                          _('TreeLine Spell Check'), prompt,
                                          QMessageBox.Yes | QMessageBox.No)
            if ans == QMessageBox.Yes:
                generator = self.textLineGenerator(self.controlRef.structure.
                                                   rootSpots())
                result = spellCheckDialog.startSpellCheck(generator)
                self.selectModel.selectSpots(origBranches,
                                             expandParents = True)
            else:
                result = False
        if result:
            QMessageBox.information(parentWidget, _('TreeLine Spell Check'),
                                    _('Finished spell checking'))

    def updateSelection(self):
        """Change the tree selection to the node with a misspelled word.
        """
        self.selectModel.selectSpots([self.currentSpot], expandParents = True)

    def changeNode(self, newTextLine):
        """Replace the current text line in the current node.

        Arguments:
            newTextLine -- the new text to use
        """
        node = self.currentSpot.nodeRef
        undo.DataUndo(self.controlRef.structure.undoList, node)
        textLines = node.data.get(self.currentField, '').split('\n')
        textLines[self.lineNum] = newTextLine
        node.data[self.currentField] = '\n'.join(textLines)
        self.controlRef.updateTreeNode(node)

    def textLineGenerator(self, branches):
        """Yield next line to be checked.

        Arguments:
            branches -- a list of branch parent nodes to check.
        """
        for parent in branches:
            for self.currentSpot in parent.spotDescendantGen():
                node = self.currentSpot.nodeRef
                for self.currentField in node.formatRef.fieldNames():
                    text = node.data.get(self.currentField, '')
                    if text:
                        for self.lineNum, self.textLine in \
                                            enumerate(text.split('\n')):
                            yield self.textLine


class SpellCheckDialog(QDialog):
    """Dialog to perform and control the spell check operation.
    """
    misspellFound = pyqtSignal()
    changeRequest = pyqtSignal(str)
    def __init__(self, spellCheckInterface, parent=None):
        """Create the dialog.

        Arguments:
            spellCheckInterface -- a reference to the spell engine interface
            parent -- the parent dialog
        """
        super().__init__(parent)
        self.setWindowFlags(Qt.Dialog | Qt.WindowTitleHint |
                            Qt.WindowCloseButtonHint)
        self.setWindowTitle(_('Spell Check'))
        self.spellCheckInterface = spellCheckInterface
        self.textLineIter = None
        self.textLine = ''
        self.replaceAllDict = {}
        self.tmpIgnoreWords = set()
        self.word = ''
        self.postion = 0

        topLayout = QHBoxLayout(self)
        leftLayout = QVBoxLayout()
        topLayout.addLayout(leftLayout)
        wordBox = QGroupBox(_('Not in Dictionary'))
        leftLayout.addWidget(wordBox)
        wordLayout = QVBoxLayout(wordBox)
        label = QLabel(_('Word:'))
        wordLayout.addWidget(label)
        self.wordEdit = QLineEdit()
        wordLayout.addWidget(self.wordEdit)
        self.wordEdit.textChanged.connect(self.updateFromWord)
        wordLayout.addSpacing(5)
        label = QLabel(_('Context:'))
        wordLayout.addWidget(label)
        self.contextEdit = SpellContextEdit()
        wordLayout.addWidget(self.contextEdit)
        self.contextEdit.textChanged.connect(self.updateFromContext)

        suggestBox = QGroupBox(_('Suggestions'))
        leftLayout.addWidget(suggestBox)
        suggestLayout =  QVBoxLayout(suggestBox)
        self.suggestList = QListWidget()
        suggestLayout.addWidget(self.suggestList)
        self.suggestList.itemDoubleClicked.connect(self.replace)

        rightLayout = QVBoxLayout()
        topLayout.addLayout(rightLayout)
        ignoreButton = QPushButton(_('Ignor&e'))
        rightLayout.addWidget(ignoreButton)
        ignoreButton.clicked.connect(self.ignore)
        ignoreAllButton = QPushButton(_('&Ignore All'))
        rightLayout.addWidget(ignoreAllButton)
        ignoreAllButton.clicked.connect(self.ignoreAll)
        rightLayout.addStretch()
        addButton = QPushButton(_('&Add'))
        rightLayout.addWidget(addButton)
        addButton.clicked.connect(self.add)
        addLowerButton = QPushButton(_('Add &Lowercase'))
        rightLayout.addWidget(addLowerButton)
        addLowerButton.clicked.connect(self.addLower)
        rightLayout.addStretch()
        replaceButton = QPushButton(_('&Replace'))
        rightLayout.addWidget(replaceButton)
        replaceButton.clicked.connect(self.replace)
        self.replaceAllButton = QPushButton(_('Re&place All'))
        rightLayout.addWidget(self.replaceAllButton)
        self.replaceAllButton.clicked.connect(self.replaceAll)
        rightLayout.addStretch()
        cancelButton = QPushButton(_('&Cancel'))
        rightLayout.addWidget(cancelButton)
        cancelButton.clicked.connect(self.reject)
        self.widgetDisableList = [ignoreButton, ignoreAllButton, addButton,
                                  addLowerButton, self.suggestList]
        self.fullDisableList = (self.widgetDisableList +
                                [self.replaceAllButton, self.wordEdit])

    def startSpellCheck(self, textLineIter):
        """Spell check text lines given in the iterator.

        Block execution except for the dialog if mispellings are found.
        Return True if spell check completes, False if cancelled.
        Arguments:
            textLineIter -- an iterator of text lines to check
        """
        self.textLineIter = textLineIter
        try:
            self.textLine = next(self.textLineIter)
        except StopIteration:
            return True
        if self.spellCheck():
            if self.exec_() == QDialog.Rejected:
                return False
        return True

    def continueSpellCheck(self):
        """Check lines, starting with current line.

        Exit the dialog if there are no more lines to check.
        """
        if not self.spellCheck():
            self.accept()

    def spellCheck(self):
        """Step through the iterator and spell check the lines.

        If results found, update the dialog with the results and return True.
        Return false if the end of the iterator is reached.
        """
        while True:
            results = self.spellCheckInterface.checkLine(self.textLine,
                                                         self.tmpIgnoreWords)
            if results:
                self.word, self.position, suggestions = results[0]
                newWord = self.replaceAllDict.get(self.word, '')
                if newWord:
                    self.textLine = self.replaceWord(newWord)
                    self.changeRequest.emit(self.textLine)
                else:
                    self.misspellFound.emit()
                    self.setWord(suggestions)
                    return True
            try:
                self.textLine = next(self.textLineIter)
                self.tmpIgnoreWords.clear()
            except StopIteration:
                return False

    def setWord(self, suggestions):
        """Set dialog contents from the checked line and spell check results.
        
        Arguments:
            suggestions -- a list of suggested replacement words
        """
        self.wordEdit.blockSignals(True)
        self.wordEdit.setText(self.word)
        self.wordEdit.blockSignals(False)
        self.contextEdit.blockSignals(True)
        self.contextEdit.setPlainText(self.textLine)
        self.contextEdit.setSelection(self.position,
                                      self.position + len(self.word))
        self.contextEdit.blockSignals(False)
        self.suggestList.clear()
        self.suggestList.addItems(suggestions)
        self.suggestList.setCurrentItem(self.suggestList.item(0))
        for widget in self.fullDisableList:
            widget.setEnabled(True)

    def replaceWord(self, newWord):
        """Return textLine with word replaced with newWord.
        
        Arguments:
            newWord -- the replacement word
        """
        return (self.textLine[:self.position] + newWord +
                self.textLine[self.position + len(self.word):])

    def ignore(self):
        """Set word to ignored (this check only) and continue spell check.
        """
        self.tmpIgnoreWords.add(self.word)
        self.continueSpellCheck()

    def ignoreAll(self):
        """Add to dictionary's ignore list and continue spell check.
        """
        self.spellCheckInterface.acceptWord(self.word)
        self.continueSpellCheck()

    def add(self):
        """Add misspelling to dictionary and continue spell check"""
        self.spellCheckInterface.addToDict(self.word, False)
        self.continueSpellCheck()

    def addLower(self):
        """Add misspelling to dictionary as lowercase and continue spell check.
        """
        self.spellCheckInterface.addToDict(self.word, True)
        self.continueSpellCheck()

    def replace(self):
        """Replace misspelled word with suggestion or context edit box
        
        Then continue spell check.
        """
        if self.suggestList.isEnabled():
            newWord = self.suggestList.currentItem().text()
            self.textLine = self.replaceWord(newWord)
        else:
            self.textLine = self.contextEdit.toPlainText()
        self.changeRequest.emit(self.textLine)
        self.continueSpellCheck()

    def replaceAll(self):
        """Replace misspelled word with suggestion or word edit (in future too).
        
        Stores changed word in replaceAllDict and continues spell check.
        """
        if self.suggestList.isEnabled():
            newWord = self.suggestList.currentItem().text()
        else:
            newWord = self.wordEdit.text()
        self.textLine = self.replaceWord(newWord)
        self.replaceAllDict[self.word] = newWord
        self.changeRequest.emit(self.textLine)
        self.continueSpellCheck()

    def updateFromWord(self):
        """Update dialog after word line editor change.
        
        Disables suggests and ignore/add controls. Updates the context editor.
        """
        for widget in self.widgetDisableList:
            widget.setEnabled(False)
        newWord = self.wordEdit.text()
        self.suggestList.clearSelection()
        self.contextEdit.blockSignals(True)
        self.contextEdit.setPlainText(self.replaceWord(newWord))
        self.contextEdit.setSelection(self.position,
                                      self.position + len(newWord))
        self.contextEdit.blockSignals(False)

    def updateFromContext(self):
        """Update dialog after context editor change.
        
        Disables controls except for replace.
        """
        for widget in self.fullDisableList:
            widget.setEnabled(False)
        self.suggestList.clearSelection()


class SpellContextEdit(QTextEdit):
    """Editor for spell check word context.
    
    Sets the size hint to 3 lines and simplifies selction.
    """
    def __init__(self, parent=None):
        """Create the editor.

        Arguments:
            parent -- the parent widget
        """
        super().__init__(parent)
        self.setTabChangesFocus(True)

    def sizeHint(self):
        """Set prefered size of 3 lines long.
        """
        fontHeight = QFontMetrics(self.currentFont()).lineSpacing()
        return QSize(QTextEdit.sizeHint(self).width(),
                            fontHeight * 3)

    def setSelection(self, fromPos, toPos):
        """Select the given range in first paragraph.
        
        Arguments:
            fromPos -- the starting position
            toPos -- the ending position
        """
        cursor = self.textCursor()
        cursor.setPosition(fromPos)
        cursor.setPosition(toPos, QTextCursor.KeepAnchor)
        self.setTextCursor(cursor)
        self.ensureCursorVisible()