pythainlp.spell

The pythainlp.spell module is a powerful tool for finding the closest correctly spelled word to a given text in the Thai language. It provides functionalities to correct spelling errors and enhance the accuracy of text processing.

Modules

correct

pythainlp.spell.correct(word: str, engine: str = 'pn') → str[source]

Correct the spelling of a word.

Parameters:
  • word (str) – word to be corrected

  • engine (str) –

    engine to use for spell checking. Options are:

    • pn - Peter Norvig’s algorithm [1] (default)

    • phunspell - spell checker using spylls, a port of Hunspell

    • symspellpy - symspellpy, a Python port of SymSpell v6.5

    • wanchanberta_thai_grammarly - WanchanBERTa Thai Grammarly

Returns:

corrected word

Return type:

str

Example:
>>> from pythainlp.spell import correct
>>> correct("เส้นตรบ")
'เส้นตรง'
>>> correct("ครัช")
'ครับ'
>>> correct("สังเกตุ")
'สังเกต'
>>> correct("กระปิ")
'กะปิ'
>>> correct("เหตการณ")
'เหตุการณ์'

The correct function is designed to correct the spelling of a single Thai word. Given an input word, this function returns the closest correctly spelled word from the dictionary, making it valuable for spell-checking and text correction tasks.

correct_sent

pythainlp.spell.correct_sent(list_words: list[str], engine: str = 'pn') → list[str][source]

Correct the spelling of a sentence.

Parameters:
  • list_words (list[str]) – list of words in the sentence

  • engine (str) –

    engine to use for spell checking. Options are:

    • pn - Peter Norvig’s algorithm [1] (default)

    • phunspell - spell checker using spylls, a port of Hunspell

    • symspellpy - symspellpy, a Python port of SymSpell v6.5

    • wanchanberta_thai_grammarly - WanchanBERTa Thai Grammarly

Returns:

list of corrected words in the sentence

Return type:

list[str]

Example:
>>> from pythainlp.spell import correct_sent
>>> correct_sent(
...     ["เด็", "อินอร์เน็ต", "แรง"], engine="symspellpy"
... )
['เด็ก', 'อินเทอร์เน็ต', 'แรง']

The correct_sent function is an extension of the correct function and is used to correct an entire sentence. It tokenizes the input sentence, corrects each word, and returns the corrected sentence. This is beneficial for proofreading and improving the readability of Thai text.

get_words_spell_suggestion

pythainlp.spell.get_words_spell_suggestion(list_words: str | list[str]) → list[str] | list[list[str]][source]

Get spelling suggestions for Thai words.

This function retrieves spelling suggestions for one or more words.

It requires numpy and onnxruntime. Install them before use.

Parameters:

list_words (Union[str, list[str]]) – word, or list of words, to get suggestions for

Returns:

spelling suggestions (at most 5 per word); a list of lists if the input is a list of words

Return type:

Union[list[str], list[list[str]]]

Example:
>>> from pythainlp.spell import (
...     get_words_spell_suggestion,
... )
>>> print(get_words_spell_suggestion("คมดี"))
['คนดีผีคุ้ม', 'มีดคอม้า', 'คดี', 'มีดสองคม', 'มูลคดี']
>>> print(
...     get_words_spell_suggestion(["คมดี", "กระเพาะ"])
... )
[['คนดีผีคุ้ม', 'มีดคอม้า', 'คดี', 'มีดสองคม', 'มูลคดี'],
['กระเพาะ', 'กระพา', 'กะเพรา', 'กระเพาะปลา', 'พระประธาน']]

The get_words_spell_suggestion function is designed to retrieve spelling suggestions for one or more input Thai words.

spell

pythainlp.spell.spell(word: str, engine: str = 'pn') → list[str][source]

Return possible correct spellings of a word.

The candidates are words in the dictionary that have an edit distance of 1 or 2 from the word. They are sorted by their occurrences in the spelling dictionary, in descending order.

Parameters:
  • word (str) – word to be checked

  • engine (str) –

    engine to use for spell checking. Options are:

    • pn - Peter Norvig’s algorithm [1] (default)

    • phunspell - spell checker using spylls, a port of Hunspell

    • symspellpy - symspellpy, a Python port of SymSpell v6.5

    • tltk - wrapper for TLTK

Returns:

list of possible correct words within 1 or 2 edit distance, sorted by frequency of occurrence in the spelling dictionary in descending order

Return type:

list[str]

Example:
>>> from pythainlp.spell import spell
>>> spell("เส้นตรบ", engine="pn")
['เส้นตรง']
>>> spell("เส้นตรบ")
['เส้นตรง']
>>> spell("เส้นตรบ", engine="tltk")
['เส้นตรง']
>>> spell("ครัช")
['ครับ', 'ครัว', 'รัช', 'ครัม', 'ครัน', 'วรัช', 'ครัส',
'ปรัช', 'บรัช', 'ครัง', 'คัช', 'คลัช', 'ครัย', 'ครัด']
>>> spell("กระปิ")
['กะปิ', 'กระบิ']
>>> spell("สังเกตุ")
['สังเกต']
>>> spell("เหตการณ")
['เหตุการณ์']

The spell function is responsible for identifying spelling errors within a given Thai word. It checks whether the input word is spelled correctly or not and returns a Boolean result. This function is useful for validating the correctness of Thai words.

spell_sent

pythainlp.spell.spell_sent(list_words: list[str], engine: str = 'pn') → list[list[str]][source]

Return possible correct spellings of a sentence.

Parameters:
  • list_words (list[str]) – list of words in the sentence

  • engine (str) –

    engine to use for spell checking. Options are:

    • pn - Peter Norvig’s algorithm [1] (default)

    • phunspell - spell checker using spylls, a port of Hunspell

    • symspellpy - symspellpy, a Python port of SymSpell v6.5

Returns:

list of possible correct sentences, each a list of words

Return type:

list[list[str]]

Example:
>>> from pythainlp.spell import spell_sent
>>> spell_sent(
...     ["เด็", "อินอร์เน็ต", "แรง"], engine="symspellpy"
... )
[['เด็ก', 'อินเทอร์เน็ต', 'แรง']]

The spell_sent function extends the spell-checking functionality to entire sentences. It tokenizes the input sentence and checks the spelling of each word. It returns a list of Booleans indicating whether each word in the sentence is spelled correctly or not.

NorvigSpellChecker

class pythainlp.spell.NorvigSpellChecker(custom_dict: Optional[Union[dict[str, int], Iterable[str], Iterable[tuple[str, int]]]] = None, min_freq: int = 2, min_len: int = 2, max_len: int = 40, dict_filter: Optional[Callable[[str], bool]] = <function _is_thai_and_not_num>)[source]

Check and correct spelling using Peter Norvig’s algorithm.

__init__(custom_dict: Optional[Union[dict[str, int], Iterable[str], Iterable[tuple[str, int]]]] = None, min_freq: int = 2, min_len: int = 2, max_len: int = 40, dict_filter: Optional[Callable[[str], bool]] = <function _is_thai_and_not_num>) → None[source]

Initialize Peter Norvig’s spell checker.

The spelling dictionary can be customized. By default, it is from Phupha: Thai Word Frequency Dataset (filtered with the Royal Society of Thailand word list).

Norvig’s spell checker chooses the most likely corrected spelling of a word by searching for candidate words based on edit distance. Then, it selects the candidate with the highest word occurrence probability.

Parameters:
  • custom_dict (Union[dict[str, int], Iterable[str], Iterable[tuple[str, int]]], optional) –

    custom spelling dictionary, one of:

    1. a dict with words (str) as keys and frequencies (int) as values;

    2. an iterable (list, tuple, or set) of tuples of word and frequency: (str, int); or

    3. an iterable of words (str) without frequencies; in this case, 1 is assigned to every word.

    The default is the Phupha dataset, filtered with the Royal Society of Thailand word list (38,160 words).

  • min_freq (int) – minimum frequency of a word to keep (default: 2)

  • min_len (int) – minimum length (in characters) of a word to keep (default: 2)

  • max_len (int) – maximum length (in characters) of a word to keep (default: 40)

  • dict_filter (Optional[Callable[[str], bool]]) – function to filter the dictionary. The default filter removes any word with numbers or non-Thai characters. Use None for no filter

dictionary() → ItemsView[str, int][source]

Return the spelling dictionary used by this spell checker.

Returns:

spelling dictionary of this instance

Return type:

ItemsView[str, int]

Example:
>>> from pythainlp.spell import (
...     NorvigSpellChecker,
... )
>>> dictionary = [
...     ("หวาน", 30),
...     ("มะนาว", 2),
...     ("แอบ", 3223),
... ]
>>> checker = NorvigSpellChecker(
...     custom_dict=dictionary
... )
>>> checker.dictionary()
dict_items([('หวาน', 30), ('มะนาว', 2), ('แอบ', 3223)])
known(words: Iterable[str]) → list[str][source]

Return the given words that are found in the spelling dictionary.

Parameters:

words (Iterable[str]) – words to look up in the spelling dictionary

Returns:

intersection of the given words and words in the spelling dictionary

Return type:

list[str]

Example:
>>> from pythainlp.spell import (
...     NorvigSpellChecker,
... )
>>> checker = NorvigSpellChecker()
>>> checker.known(["เพยน", "เพล", "เพลง"])
['เพล', 'เพลง']
>>> checker.known(["ยกไ", "ไฟล์ม"])
[]
>>> checker.known([])
[]
prob(word: str) → float[source]

Return the occurrence probability of a word.

The probability is according to the spelling dictionary.

Parameters:

word (str) – word to get the occurrence probability of

Returns:

word occurrence probability

Return type:

float

Example:
>>> from pythainlp.spell import (
...     NorvigSpellChecker,
... )
>>> checker = NorvigSpellChecker()
>>> checker.prob("ครัช")
0.0
>>> checker.prob("รัก")
0.0006959172792052158
>>> checker.prob("น่ารัก")
9.482306849763902e-05
freq(word: str) → int[source]

Return the frequency of a word.

The frequency is according to the spelling dictionary.

Parameters:

word (str) – word to get the frequency of

Returns:

frequency of the word in the spelling dictionary

Return type:

int

Example:
>>> from pythainlp.spell import (
...     NorvigSpellChecker,
... )
>>> checker = NorvigSpellChecker()
>>> checker.freq("ปัญญา")
3639
>>> checker.freq("บิญชา")
0
spell(word: str) → list[str][source]

Return correctly spelled words that are similar to a word.

The similarity is by edit distance. The returned words are sorted by word frequency in the spelling dictionary, in descending order.

First, if the word is spelled correctly, this method returns a list of exactly one word, which is the word itself. Next, this method looks for all correctly spelled words with an edit distance of 1 from the word. If there is no such word, the search expands to words with an edit distance of 2. If that still fails, this method returns a list of the input word.

Parameters:

word (str) – word to be checked

Returns:

list of possible correct words within 1 or 2 edit distance, sorted by frequency of occurrence in the spelling dictionary in descending order

Return type:

list[str]

Example:
>>> from pythainlp.spell import (
...     NorvigSpellChecker,
... )
>>> checker = NorvigSpellChecker()
>>> checker.spell("เส้นตรบ")
['เส้นตรง']
>>> checker.spell("ครัช")
['ครับ', 'ครัว', 'รัช', 'ครัม', 'ครัน',
'วรัช', 'ครัส', 'ปรัช', 'บรัช', 'ครัง',
'คัช', 'คลัช', 'ครัย', 'ครัด']
correct(word: str) → str[source]

Return the most probable correct spelling of a word.

The probability is from the spelling dictionary.

Parameters:

word (str) – word to be corrected

Returns:

corrected word

Return type:

str

Example:
>>> from pythainlp.spell import (
...     NorvigSpellChecker,
... )
>>> checker = NorvigSpellChecker()
>>> checker.correct("ปัญชา")
'ปัญหา'
>>> checker.correct("บิญชา")
'บัญชา'
>>> checker.correct("มิตรภาบ")
'มิตรภาพ'
__annotate_func__ = None
__annotations_cache__ = {'__WORDS': 'Counter[str]', '__WORDS_TOTAL': 'int'}
__dict__ = mappingproxy({'__module__': 'pythainlp.spell.pn', '__firstlineno__': 169, '__doc__': "Check and correct spelling using Peter Norvig's algorithm.", '__init__': <function NorvigSpellChecker.__init__>, 'dictionary': <function NorvigSpellChecker.dictionary>, 'known': <function NorvigSpellChecker.known>, 'prob': <function NorvigSpellChecker.prob>, 'freq': <function NorvigSpellChecker.freq>, 'spell': <function NorvigSpellChecker.spell>, 'correct': <function NorvigSpellChecker.correct>, '__static_attributes__': ('__WORDS', '__WORDS_TOTAL'), '__dict__': <attribute '__dict__' of 'NorvigSpellChecker' objects>, '__weakref__': <attribute '__weakref__' of 'NorvigSpellChecker' objects>, '__annotations_cache__': {'__WORDS': 'Counter[str]', '__WORDS_TOTAL': 'int'}})
__firstlineno__ = 169
__module__ = 'pythainlp.spell.pn'
__static_attributes__ = ('__WORDS', '__WORDS_TOTAL')

The NorvigSpellChecker class is a fundamental component of the pythainlp.spell module. It implements a spell-checking algorithm based on the work of Peter Norvig. This class is designed for more advanced spell-checking and provides customizable settings for spell correction.

DEFAULT_SPELL_CHECKER

pythainlp.spell.DEFAULT_SPELL_CHECKER = Default reference of the standard NorvigSpellChecker, using word list data from the Thai National Corpus: https://www.arts.chula.ac.th/ling/tnc3/

Check and correct spelling using Peter Norvig’s algorithm.

The DEFAULT_SPELL_CHECKER is an reference to the NorvigSpellChecker class with default settings. It is pre-configured to use word list data from the Thai National Corpus, making it a reliable choice for general spell-checking tasks.

References