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
3 changes: 2 additions & 1 deletion docs/api/ancient.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pythainlp.ancient
Modules
-------

.. autofunction:: aksonhan_to_current
.. autofunction:: aksonhan_to_current
.. autofunction:: convert_currency
3 changes: 2 additions & 1 deletion pythainlp/ancient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Ancient versions of the Thai language
"""

__all__ = ["aksonhan_to_current"]
__all__ = ["aksonhan_to_current", "convert_currency"]

from pythainlp.ancient.aksonhan import aksonhan_to_current
from pythainlp.ancient.currency import convert_currency
70 changes: 70 additions & 0 deletions pythainlp/ancient/currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0

def convert_currency(value: float, from_unit: str) -> dict:
"""
Convert ancient Thai currency to other units

* เบี้ย (Bia)
* อัฐ (At)
* ไพ (Pi)
* เฟื้อง (Feuang)
* สลึง (Saleung)
* บาท (Bath)
* ตำลึง (Tamleung)
* ชั่ง (Chang)

See more:
`Thai money <https://en.wikipedia.org/wiki/History_of_Thai_money>`_.

:param float value: value
:param str from_unit: currency unit \
('เบี้ย', 'อัฐ', 'ไพ', 'เฟื้อง', 'สลึง', 'บาท', 'ตำลึง', 'ชั่ง')
:return: Thai currency
:rtype: dict

:Example:
::

from pythainlp.ancient import convert_currency

print(convert_currency(8, "บาท"))
# output:
# {
# 'เบี้ย': 51200.0,
# 'อัฐ': 512.0,
# 'ไพ': 256.0,
# 'เฟื้อง': 64.0,
# 'สลึง': 32.0,
# 'บาท': 8.0,
# 'ตำลึง': 2.0,
# 'ชั่ง': 0.1
# }
"""
conversion_factors_to_att = {
'เบี้ย': 1,
'อัฐ': 100, # 1 อัฐ = 100 เบี้ย
'ไพ': 2 * 100, # 1 ไพ = 2 อัฐ
'เฟื้อง': 4 * 2 * 100, # 1 เฟื้อง = 4 ไพ
'สลึง': 2 * 4 * 2 * 100, # 1 สลึง = 2 เฟื้อง
'บาท': 4 * 2 * 4 * 2 * 100, # 1 บาท = 4 สลึง
'ตำลึง': 4 * 4 * 2 * 4 * 2 * 100, # 1 ตำลึง = 4 บาท
'ชั่ง': 20 * 4 * 4 * 2 * 4 * 2 * 100, # 1 ชั่ง = 20 ตำลึง
}

if from_unit not in conversion_factors_to_att:
raise NotImplementedError(
f"Currency unit '{from_unit}' is not support."
)

# start from 'อัฐ'
value_in_att = value * conversion_factors_to_att[from_unit]

# Calculate values ​​in other units
results = {}
for unit, factor in conversion_factors_to_att.items():
results[unit] = value_in_att / factor

return results
24 changes: 23 additions & 1 deletion tests/core/test_ancient.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import unittest

from pythainlp.ancient import aksonhan_to_current
from pythainlp.ancient import aksonhan_to_current, convert_currency


class AncientTestCase(unittest.TestCase):
Expand All @@ -23,3 +23,25 @@ def test_aksonhan_to_current(self):
self.assertEqual(aksonhan_to_current("หลงง"), "หลัง")
self.assertEqual(aksonhan_to_current("บงงคบบ"), "บังคับ")
self.assertEqual(aksonhan_to_current("สรรเพชญ"), "สรรเพชญ")

def test_convert_currency(self):
self.assertEqual(
convert_currency(80, "บาท")["ตำลึง"],
20.0
)
self.assertEqual(
convert_currency(80, "บาท")["ชั่ง"],
1.0
)
self.assertEqual(
convert_currency(80, "บาท")["บาท"],
80.0
)
self.assertEqual(
convert_currency(1,"ชั่ง")["บาท"],
80.0
)
self.assertEqual(
convert_currency(1,"ชั่ง")["ชั่ง"],
1.0
)
Loading