Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/util.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Modules
.. autofunction:: thaiword_to_num
.. autofunction:: thaiword_to_time
.. autofunction:: time_to_thaiword
.. autofunction:: tis620_to_utf8
.. autofunction:: tone_detector
.. autofunction:: words_to_num
.. autofunction:: nectec_to_ipa
Expand Down
2 changes: 2 additions & 0 deletions pythainlp/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"nectec_to_ipa",
"ipa_to_rtgs",
"remove_tone_ipa",
"tis620_to_utf8",
]

from pythainlp.util.collate import collate
Expand Down Expand Up @@ -121,3 +122,4 @@
syllable_open_close_detector,
)
from pythainlp.util.phoneme import nectec_to_ipa, ipa_to_rtgs, remove_tone_ipa
from pythainlp.util.encoding import tis620_to_utf8
29 changes: 29 additions & 0 deletions pythainlp/util/encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding_utf-8 -*-
# Copyright (C) 2016-2023 PyThaiNLP Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
def tis620_to_utf8(text: str)->str:
"""
Convert TIS-620 to UTF-8

:param str text: Text that use TIS-620 encoding
:return: Text that use UTF-8 encoding
:rtype: str

:Example:

from pythainlp.util import tis620_to_utf8
tis620_to_utf8("¡ÃзÃÇ§ÍØµÊÒË¡ÃÃÁ")
# output: 'กระทรวงอุตสาหกรรม'
"""
return text.encode("cp1252", "ignore").decode("tis-620")
4 changes: 4 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
nectec_to_ipa,
ipa_to_rtgs,
remove_tone_ipa,
tis620_to_utf8,
)


Expand Down Expand Up @@ -840,3 +841,6 @@ def test_ipa_to_rtgs(self):

def test_remove_tone_ipa(self):
self.assertEqual(remove_tone_ipa("laː˦˥.sa˨˩.maj˩˩˦"), "laː.sa.maj")

def test_tis620_to_utf8(self):
self.assertEqual(tis620_to_utf8("¡ÃзÃÇ§ÍØµÊÒË¡ÃÃÁ"), "กระทรวงอุตสาหกรรม")