Thank you for your reply
I have included the complete code:
#example.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from spellcheckwrapper import SpellCheckWrapper
from spelltextedit import SpellTextEdit
class Ui_MainWindow(QMainWindow):
def __init__(self, word_list_path: str):
super().__init__()
self.word_list_path = word_list_path
self.speller = SpellCheckWrapper(self.getWords(), self.addToDictionary)
self.setupUi()
def setupUi(self):
self.resize(500, 500)
self.setWindowTitle("Example app")
self.centralWidget = QWidget(self)
self.setCentralWidget(self.centralWidget)
self.layout = QVBoxLayout(self.centralWidget)
self.centralWidget.setLayout(self.layout)
self.textEdit1 = SpellTextEdit(self.speller, self.centralWidget)
self.layout.addWidget(self.textEdit1)
self.textEdit2 = SpellTextEdit(self.speller, self.centralWidget)
self.layout.addWidget(self.textEdit2)
def getWords(self) -> list[str]:
with open(self.word_list_path, "r") as f:
word_list = [line.strip() for line in f]
return word_list
def addToDictionary(self, new_word: str):
with open(self.word_list_path, "a") as f:
f.write("\n" + new_word)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Ui_MainWindow("example_word_list.txt")
window.show()
sys.exit(app.exec_())
#------------------------
#spellcheckhighlighter.py
import re
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat
from spellcheckwrapper import SpellCheckWrapper
class SpellCheckHighlighter(QSyntaxHighlighter):
wordRegEx = re.compile(r"\b([A-Za-z]{2,})\b")
def highlightBlock(self, text: str) -> None:
if not hasattr(self, "speller"):
return
self.misspelledFormat = QTextCharFormat()
self.misspelledFormat.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline)
self.misspelledFormat.setUnderlineColor(Qt.red)
for word_object in self.wordRegEx.finditer(text):
if not self.speller.check(word_object.group()):
self.setFormat(
word_object.start(),
word_object.end() - word_object.start(),
self.misspelledFormat,
)
def setSpeller(self, speller: SpellCheckWrapper):
self.speller = speller
#------------------------------------
from typing import Callable
import enchant
from enchant import DictWithPWL
from PyQt5.QtCore import QTemporaryFile
class SpellCheckWrapper:
def __init__(
self, personal_word_list: list[str], addToDictionary: Callable[[str], None]
):
# Creating temporary file
self.file = QTemporaryFile()
self.file.open()
self.dictionary = DictWithPWL(
"en_US",
self.file.fileName(),
)
self.addToDictionary = addToDictionary
self.word_list = set(personal_word_list)
self.load_words()
def load_words(self):
for word in self.word_list:
self.dictionary.add(word)
def suggestions(self, word: str) -> list[str]:
return self.dictionary.suggest(word)
def correction(self, word: str) -> str:
return self.dictionary.suggest(word)[0]
def add(self, new_word: str) -> bool:
if self.check(new_word):
return False
self.word_list.add(new_word)
self.addToDictionary(new_word)
self.dictionary.add(new_word)
return True
def check(self, word: str) -> bool:
return self.dictionary.check(word)
def getNewWords(self) -> set[str]:
return self.word_list
#--------------------------------------------
# highlighter.py
import re
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat
from spellcheckwrapper import SpellCheckWrapper
class SpellCheckHighlighter(QSyntaxHighlighter):
wordRegEx = re.compile(r"\b([A-Za-z]{2,})\b")
def highlightBlock(self, text: str) -> None:
if not hasattr(self, "speller"):
return
self.misspelledFormat = QTextCharFormat()
self.misspelledFormat.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline)
self.misspelledFormat.setUnderlineColor(Qt.red)
for word_object in self.wordRegEx.finditer(text):
if not self.speller.check(word_object.group()):
self.setFormat(
word_object.start(),
word_object.end() - word_object.start(),
self.misspelledFormat,
)
def setSpeller(self, speller: SpellCheckWrapper):
self.speller = speller
#
# correction_action.py
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QAction
class SpecialAction(QAction):
actionTriggered = pyqtSignal(str)
def __init__(self, *args):
super().__init__(*args)
self.triggered.connect(self.emitTriggered)
def emitTriggered(self):
self.actionTriggered.emit(self.text())
The error message is
Error:
Traceback (most recent call last):
File "/home/ProtoTyping/SpellChecker/pyqt-spellcheck/src/example.py", line 5, in <module>
from spellcheckwrapper import SpellCheckWrapper
File "/home/ProtoTyping/SpellChecker/pyqt-spellcheck/src/spellcheckwrapper.py", line 7, in <module>
class SpellCheckWrapper:
File "/home/ProtoTyping/SpellChecker/pyqt-spellcheck/src/spellcheckwrapper.py", line 9, in SpellCheckWrapper
self, personal_word_list: list[str], addToDictionary: Callable[[str], None]
TypeError: 'type' object is not subscriptable