File size: 1,487 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 | # -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""
Thai abbreviation tools
"""
from typing import List, Tuple, Union
def abbreviation_to_full_text(text: str, top_k: int=2) -> List[Tuple[str, Union[float, None]]]:
"""
This function converts Thai text (with abbreviation) to full text.
This function uses KhamYo for handles abbreviations.
See more `KhamYo <https://github.com/wannaphong/KhamYo>`_.
:param str text: Thai text
:param int top_k: Top K
:return: Thai full text with abbreviations converted to full text and cos scores (original text - modified text).
:rtype: List[Tuple[str, Union[float, None]]]
:Example:
::
from pythainlp.util import abbreviation_to_full_text
text = "รร.ของเราน่าอยู่"
abbreviation_to_full_text(text)
# output: [
# ('โรงเรียนของเราน่าอยู่', tensor(0.3734)),
# ('โรงแรมของเราน่าอยู่', tensor(0.2438))
# ]
"""
try:
from khamyo import replace as _replace
except ImportError:
raise ImportError(
"""
This function needs to use khamyo.
You can install by pip install khamyo or
pip install pythainlp[abbreviation].
"""
)
return _replace(text, top_k=top_k)
|