File size: 1,988 Bytes
e4b9a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0

from pythainlp import thai_consonants, thai_tonemarks
from pythainlp.corpus import thai_orst_words
from pythainlp.tokenize import Tokenizer
from pythainlp.util import Trie

_dict_aksonhan = {}
for i in list(thai_consonants):
    if i == "ร":
        continue
    for j in list(thai_tonemarks):
        _dict_aksonhan[i + j + i] = "ั" + j + i
        _dict_aksonhan[i + i + j + i] = i + "ั" + j + i
    _dict_aksonhan[i + i] = "ั" + i
_set_aksonhan = set(_dict_aksonhan.keys())
_trie = Trie(list(_dict_aksonhan.keys()) + list(thai_consonants))
_tokenizer = Tokenizer(custom_dict=_trie, engine="mm")
_dict_thai = set(thai_orst_words())  # call Thai words


def aksonhan_to_current(word: str) -> str:
    """
    Convert AksonHan words to current Thai words

    AksonHan (อักษรหัน) writes down two consonants for the \
    spelling of the /a/ vowels. (สระ อะ).

    Today, รร is an aksonHan word that is still used in Thai.

    :param str word: Thai word
    :return: Thai AksonHan to be converted to current Thai word
    :rtype: str

    :Example:
    ::

        from pythainlp.ancient import aksonhan_to_current

        print(aksonhan_to_current("จกก"))
        # output: จัก

        print(aksonhan_to_current("บงงคบบ"))
        # output: บังคับ

        print(aksonhan_to_current("สรรเพชญ")) # รร is still used.
        # output: สรรเพชญ
    """
    if len(word) < 3:
        return word
    elif word in _set_aksonhan:
        return _dict_aksonhan[word]
    elif word in _dict_thai:  # word in Thai words
        return word

    _seg = _tokenizer.word_tokenize(word)
    _w = []
    for i in _seg:
        if i in _set_aksonhan:
            _w.append(_dict_aksonhan[i])
        else:
            _w.append(i)
    return "".join(_w)