Android-Spelling Checker Framework

The Android platform offers a spelling checker framework that lets you implement and access spell checking in your application. The framework is one of the Text Service APIs offered by the Android platform.

》api:SpellCheckerService.Session.The SpellCheckerService implements both the Service class and the spelling checker framework interface. 

onGetSentenceSuggestionsMultiple()
Does the actual spell checking. This method returns an array of  SentenceSuggestionsInfocontaining suggestions for the sentences passed to it. Note:  You must implement all aspects of spell checking as asynchronous and thread-safe. A spelling checker may be called simultaneously by different threads running on different cores. The  SpellCheckerService and  SpellCheckerService.Session  take care of this automatically.

> The manifest file defines the application, the service, and the activity for controlling settings, as shown in the following snippet:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.samplespellcheckerservice" >
    <application
        android:label="@string/app_name" >
        <service
            android:label="@string/app_name"
            android:name=".SampleSpellCheckerService"
            android:permission="android.permission.BIND_TEXT_SERVICE" >
            <intent-filter >
                <action android:name="android.service.textservice.SpellCheckerService" />
            </intent-filter>

            <meta-data
                android:name="android.view.textservice.scs"
                android:resource="@xml/spellchecker" />
        </service>

        <activity
            android:label="@string/sample_settings"
            android:name="SpellCheckerSettingsActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
    </application>
</manifest>

The metadata file spellchecker.xml contains the following XML:

<spell-checker xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/spellchecker_name"
        android:settingsActivity="com.example.SpellCheckerSettingsActivity">
    <subtype
            android:label="@string/subtype_generic"
            android:subtypeLocale="en”
    />
    <subtype
            android:label="@string/subtype_generic"
            android:subtypeLocale="fr”
    />
</spell-checker>
The  Spell Checker Client  sample app shows how to interact with a spelling checker service. The LatinIME input method editor in the Android Open Source Project also contains an example of spell checking.
您可以使用QTextEdit控件的contextMenuEvent事件来实现右键菜单中添加“拼写检查”的功能,代码示例如下: ```python from PyQt5.QtWidgets import QTextEdit, QApplication, QMenu from PyQt5.QtGui import QTextCursor class MyTextEdit(QTextEdit): def __init__(self, parent=None): super(MyTextEdit, self).__init__(parent) def contextMenuEvent(self, event): menu = self.createStandardContextMenu() spellCheckAction = menu.addAction("拼写检查") spellCheckAction.triggered.connect(self.spellCheck) menu.exec_(event.globalPos()) def spellCheck(self): textCursor = self.textCursor() text = textCursor.selectedText() if len(text) == 0: text = self.toPlainText() words = text.split() misspelled = [] for word in words: if not QSpellChecker().check(word): misspelled.append(word) if len(misspelled) > 0: newText = " ".join(misspelled) textCursor.insertText(newText) else: self.setStatusTip("没有拼写错误") if __name__ == '__main__': import sys app = QApplication(sys.argv) textEdit = MyTextEdit() textEdit.show() sys.exit(app.exec_()) ``` 在这个示例中,我们继承了QTextEdit控件,并重写了它的contextMenuEvent事件。我们创建了一个标准的右键菜单,并在菜单中添加了“拼写检查”的选项。当用户选择该选项时,我们获取当前选中的文本,如果没有选中文本,则获取整个文本。然后将文本按空格分割成单词,对每个单词进行拼写检查,如果有错误则记录下来。最后,如果有拼写错误,则用错误的单词替换掉原来的文本。如果没有拼写错误,则在状态栏中显示“没有拼写错误”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值