aboutsummaryrefslogtreecommitdiff
path: root/spellcheck.py.old
blob: b3a2b04b2f638709e52cf7fee8a639cf2344e701 (plain)
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
"""
Ported from spellcheck.py - Sopel spell check Module
Copyright © 2012, Elad Alfassa, <elad@fedoraproject.org>
Copyright © 2012, Lior Ramati

Licensed under the Eiffel Forum License 2.

http://sopel.chat
"""

try:
    import enchant
except ImportError:
    enchant = None


@register_command('spellcheck', 'spell')
def spellcheck(irc, hostmask, is_admin, args):
    """
    Says whether the given word is spelled correctly, and gives suggestions if
    it's not.
    """
    if enchant is None:
        irc.msg(args[0], 'Missing pyenchant module.')
        return

    word = args[1].rstrip()
    if not word:
        irc.msg(args[0], 'Usage: .spellcheck <word>')
        return
    if ' ' in word:
        irc.msg(args[0], 'One word at a time, please')
        return
    dictionary_us = enchant.Dict('en_US')
    dictionary_uk = enchant.Dict('en_GB')

    if dictionary_uk.check(word):
        if dictionary_us.check(word):
            irc.msg(args[0], f'\u200b{word} is spelled correctly')
        else:
            irc.msg(args[0], f'\u200b{word} is spelled correctly (British)')
    elif dictionary_us.check(word):
        irc.msg(args[0], f'\u200b{word} is spelled correctly (American)')
    else:
        suggestions = {*dictionary_us.suggest(word)[:10],
                       *dictionary_uk.suggest(word)[:10]}
        suggestions_str = ', '.join(f"'{suggestion}'"
                                    for suggestion in sorted(suggestions))
        irc.msg(args[0], f'\u200b{word} is not spelled correctly. '
                         f'Maybe you want one of these spellings: '
                         f'{suggestions_str}')