拼写检查器

本文档介绍了如何配置语言以进行拼写检查、在自定义词典中添加或删除单词、禁用拼写检查等。

默认情况下,拼写检查是启用的。 拼写检查器分析文本并突出显示所有拼写错误的单词。 拼写检查器与配置文件相关联。 要访问和配置特定配置文件的拼写检查器,请使用 SpellChecker 类:

SpellChecker spellChecker = profile.spellChecker();
val spellChecker = profile.spellChecker()

另一种选择是使用 Engine.spellChecker() 方法,该方法返回与默认配置文件关联的 SpellChecker 类的实例。

与 Windows 和 Linux 不同,在 macOS 上,Chromium 使用操作系统提供的拼写检查设置和词典。 因此,SpellChecker 服务在 macOS 上的行为有所不同,您将在本指南中进一步了解。

添加语言

拼写检查器可以检查单个网页上不同语言的文本。 在 Windows 和 Linux 上,可以通过 SpellChecker.addLanguage(Language) 方法配置语言。 Chromium 自动从其服务器下载字典文件并将它们存储在用户数据目录中。

在 macOS 上,此方法仅检查操作系统中是否提供必要的词典。

无论操作系统如何,如果 Chromium 找不到必要的词典,此方法都会抛出 LanguageNotAvailableException

例如:

spellChecker.addLanguage(Language.GERMAN);
spellChecker.addLanguage(Language.of("en", "au"));
spellChecker.addLanguage(Language.GERMAN)
spellChecker.addLanguage(Language.of("en", "au"))

拼写语言配置和词典存储在用户数据中。 因此,如果您将 Engine 配置为使用特定的用户数据目录,那么 Engine 将记住语言设置并在您下次创建它时恢复它们,并自动加载相应的词典。

删除语言

在 Windows 和 Linux 上,使用 SpellChecker.removeLanguage(Language) 方法停止对特定语言的拼写检查:

spellChecker.removeLanguage(Language.GERMAN);
spellChecker.removeLanguage(Language.GERMAN)

在 macOS 上,此方法不执行任何操作。

自定义词典

拼写检查器支持自定义词典。 您可以使用 SpellChecker.getCustomDictionary()方法访问自定义词典。

例如:

Dictionary dictionary = spellChecker.customDictionary();
val dictionary = spellChecker.customDictionary()

添加单词

您可以使用 add(String) 方法将单词添加到自定义词典中:

boolean success = dictionary.add("John");
val success = dictionary.add("John")

单词必须是 UTF-8,长度在 1 到 99 个字节之间,并且没有前导或尾随 ASCII 空格。

删除单词

需要从自定义词典中删除单词,请使用 remove(String) 方法:

boolean success = dictionary.remove("John");
val success = dictionary.remove("John")

禁用拼写检查

默认情况下,拼写检查是启用的。 要禁用它,请使用以下代码:

spellChecker.disable();
spellChecker.disable()

上下文菜单

当一个用户在加载的网页上右键单击突出显示的拼写错误的单词时,您可以显示一个带有建议的上下文菜单。 使用 ShowContextMenuCallback 回调显示一个带有建议的上下文菜单和一个允许将拼写错误的单词添加到自定义词典的菜单项。

Swing

以下示例演示了如何创建和显示带有建议和添加到字典菜单项的 Swing 上下文菜单。 使用此上下文菜单,您可以用其中一个建议替换拼写错误的单词或将其添加到自定义词典中。

browser.set(ShowContextMenuCallback.class, (params, tell) ->
        SwingUtilities.invokeLater(() -> {
            JPopupMenu popupMenu = new JPopupMenu();
            popupMenu.addPopupMenuListener(new PopupMenuListener() {
                ...
                @Override
                public void popupMenuCanceled(PopupMenuEvent e) {
                    tell.close();
                }
            });

            // Add the suggestions menu items.
            SpellCheckMenu spellCheckMenu = params.spellCheckMenu();
            List<String> suggestions = spellCheckMenu.dictionarySuggestions();
            suggestions.forEach(suggestion -> {
                JMenuItem menuItem = new JMenuItem(suggestion);
                menuItem.addActionListener(e -> {
                    browser.replaceMisspelledWord(suggestion);
                    tell.close();
                });
                popupMenu.add(menuItem);
            });

            // Add menu separator if necessary.
            if (!suggestions.isEmpty()) {
                popupMenu.addSeparator();
            }

            // Add the "Add to Dictionary" menu item.
            JMenuItem addToDictionary = new JMenuItem(
                    spellCheckMenu.addToDictionaryMenuItemText());
            addToDictionary.addActionListener(e -> {
                Dictionary dictionary =
                        engine.spellChecker().customDictionary();
                dictionary.add(spellCheckMenu.misspelledWord());
                tell.close();
            });
            popupMenu.add(addToDictionary);

            // Display context menu at specified location.
            Point location = params.location();
            popupMenu.show(view, location.x(), location.y());
        }));
browser.set(ShowContextMenuCallback::class.java, ShowContextMenuCallback { params, tell ->
    SwingUtilities.invokeLater {
        val popupMenu = JPopupMenu()
        popupMenu.addPopupMenuListener(object: PopupMenuListener {
            ...
            override fun popupMenuCanceled(e: PopupMenuEvent) {
                tell.close()
            }
        })

        // Add the suggestions menu items.
        val spellCheckMenu = params.spellCheckMenu()
        val suggestions = spellCheckMenu.dictionarySuggestions()
        suggestions.forEach { suggestion ->
            val menuItem = JMenuItem(suggestion)
            menuItem.addActionListener {
                browser.replaceMisspelledWord(suggestion)
                tell.close()
            }
            popupMenu.add(menuItem)
        }

        // Add menu separator if necessary.
        if (!suggestions.isEmpty()) {
            popupMenu.addSeparator()
        }

        // Add the "Add to Dictionary" menu item.
        val addToDictionary = JMenuItem(spellCheckMenu.addToDictionaryMenuItemText())
        addToDictionary.addActionListener {
            val dictionary = engine.spellChecker().customDictionary()
            dictionary.add(spellCheckMenu.misspelledWord())
            tell.close()
        }
        popupMenu.add(addToDictionary)

        // Display context menu at specified location.
        val location = params.location()
        popupMenu.show(view, location.x(), location.y())
    }
})

以下示例演示了如何创建和显示带有建议和添加到字典菜单项的 JavaFX 上下文菜单。 使用此上下文菜单,您可以用其中一个建议替换拼写错误的单词或将其添加到自定义词典中。 Swing Spell Checker Context Menu

JavaFX

以下示例演示了如何创建和显示带有建议和添加到字典菜单项的 JavaFX 上下文菜单。 使用此上下文菜单,您可以用其中一个建议替换拼写错误的单词或将其添加到自定义词典中。

browser.set(ShowContextMenuCallback.class, (params, tell) ->
        Platform.runLater(() -> {
            ContextMenu contextMenu = new ContextMenu();
            contextMenu.setAutoHide(true);
            contextMenu.setOnHidden(event -> {
                if (!tell.isClosed()) {
                    tell.close();
                }
            });
            view.setOnMousePressed(event -> {
                if (contextMenu.isShowing()) {
                    contextMenu.hide();
                }
            });
            // Add the suggestions menu items.
            SpellCheckMenu spellCheckMenu = params.spellCheckMenu();
            List<String> suggestions = spellCheckMenu.dictionarySuggestions();
            suggestions.forEach(suggestion -> {
                MenuItem item = new MenuItem(suggestion);
                item.setOnAction(event -> {
                    browser.replaceMisspelledWord(suggestion);
                    tell.close();
                });
                contextMenu.getItems().add(item);
            });

            // Add menu separator if necessary.
            if (!suggestions.isEmpty()) {
                contextMenu.getItems().add(new SeparatorMenuItem());
            }

            // Add the "Add to Dictionary" menu item.
            MenuItem addToDictionary = new MenuItem(
                    spellCheckMenu.addToDictionaryMenuItemText());
            addToDictionary.setOnAction(event -> {
                SpellChecker spellChecker = engine.spellChecker();
                Dictionary dictionary = spellChecker.customDictionary();
                dictionary.add(spellCheckMenu.misspelledWord());
                tell.close();
            });
            contextMenu.getItems().add(addToDictionary);

            // Display context menu at specified location on screen.
            Point location = params.location();
            Point2D locationOnScreen = 
                    view.localToScreen(location.x(), location.y());
            contextMenu.show(view, locationOnScreen.getX(), 
                    locationOnScreen.getY());
        }));
browser.set(ShowContextMenuCallback::class.java, ShowContextMenuCallback { params, tell ->
    Platform.runLater {
        val contextMenu = ContextMenu()
        contextMenu.isAutoHide = true
        contextMenu.setOnHidden {
            if (!tell.isClosed) {
                tell.close()
            }
        })
        view.setOnMousePressed {
            if (contextMenu.isShowing) {
                contextMenu.hide()
            }
        }
        // Add the suggestions menu items.
        val spellCheckMenu = params.spellCheckMenu()
        val suggestions = spellCheckMenu.dictionarySuggestions()
        suggestions.forEach { suggestion ->
            val item = MenuItem(suggestion)
            item.setOnAction {
                browser.replaceMisspelledWord(suggestion)
                tell.close()
            }
            contextMenu.items.add(item)
        }

        // Add menu separator if necessary.
        if (!suggestions.isEmpty()) {
            contextMenu.items.add(SeparatorMenuItem())
        }

        // Add the "Add to Dictionary" menu item.
        val addToDictionary = MenuItem(spellCheckMenu.addToDictionaryMenuItemText())
        addToDictionary.setOnAction {
            val spellChecker = engine.spellChecker()
            val dictionary = spellChecker.customDictionary()
            dictionary.add(spellCheckMenu.misspelledWord())
            tell.close()
        }
        contextMenu.items.add(addToDictionary)

        // Display context menu at specified location on screen.
        val location = params.location()
        val locationOnScreen = view.localToScreen(location.x(), location.y())
        contextMenu.show(view, locationOnScreen.getX(), locationOnScreen.getY())
    }
})

如果您右键单击拼写错误的单词,将显示以下上下文菜单: JavaFX Spell Checker Context Menu

拼写检查器事件

当加载的网页上的文本字段或文本区域获得焦点时,拼写检查器会自动检查文本并突出显示拼写错误的单词。 要在检查文本时获得通知,请使用 SpellCheckCompleted 事件。

例如:

browser.on(SpellCheckCompleted.class, event -> {
    // The text that has been checked.
    String text = event.checkedText();
    // The list of the spell checking results.
    event.results().forEach(spellCheckingResult -> {
        // The location of the first symbol in the mis-spelled word 
        // in the checked text that is considered as mis-spelled 
        // by the spell checker.
        int location = spellCheckingResult.location();
        // The length of the mis-spelled word in the checked text
        int length = spellCheckingResult.length();
    });
});
browser.on(SpellCheckCompleted::class.java) { event -> 
    // The text that has been checked.
    val text = event.checkedText()
    // The list of the spell checking results.
    event.results().forEach { spellCheckingResult -> 
        // The location of the first symbol in the mis-spelled word 
        // in the checked text that is considered as mis-spelled 
        // by the spell checker.
        val location = spellCheckingResult.location()
        // The length of the mis-spelled word in the checked text
        val length = spellCheckingResult.length()
    }
}
Go Top