File size: 2,426 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
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
from pythainlp import thai_consonants
from pythainlp.transliterate import pronunciate

_list_consonants = list(thai_consonants.replace("ห", ""))


def puan(word: str, show_pronunciation: bool = True) -> str:
    """
    Thai Spoonerism

    This function converts Thai word to spoonerism word.

    :param str word: Thai word to be spoonerized
    :param bool show_pronunciation: True (default) or False

    :return: A string of Thai spoonerism word.
    :rtype: str

    :Example:
    ::

        from pythainlp.transliterate import puan

        puan("นาริน")
        # output: 'นิน-รา'

        puan("นาริน", False)
        # output: 'นินรา'
    """
    word = pronunciate(word, engine="w2p")
    _list_char = []
    _list_pron = word.split("-")
    _mix_list = ""
    if len(_list_pron) == 1:
        return word
    if show_pronunciation:
        _mix_list = "-"
    for i in _list_pron:
        for j in i:
            if j in _list_consonants:
                _list_char.append(j)
                break
            elif "ห" == j and "หฺ" not in i and len(i) == 2:
                _list_char.append(j)
                break

    list_w_char = list(zip(_list_pron, _list_char))
    _list_w = []
    if len(list_w_char) == 2:
        _list_w.append(
            list_w_char[1][0].replace(list_w_char[1][1], list_w_char[0][1], 1)
        )
        _list_w.append(
            list_w_char[0][0].replace(list_w_char[0][1], list_w_char[1][1], 1)
        )
    elif len(list_w_char) == 3:
        _list_w.append(_list_pron[0])
        _list_w.append(
            list_w_char[2][0].replace(list_w_char[2][1], list_w_char[1][1], 1)
        )
        _list_w.append(
            list_w_char[1][0].replace(list_w_char[1][1], list_w_char[2][1], 1)
        )
    else:  # > 3 syllables
        _list_w.append(
            _list_pron[0].replace(list_w_char[0][1], list_w_char[-1][1], 1)
        )
        for i in range(1, len(list_w_char) - 1):
            _list_w.append(_list_pron[i])
        _list_w.append(
            _list_pron[-1].replace(list_w_char[-1][1], list_w_char[0][1], 1)
        )
    if not show_pronunciation:
        _list_w = [i.replace("หฺ", "").replace("ฺ", "") for i in _list_w]
    return _mix_list.join(_list_w)