Skip to content

Commit 843e70a

Browse files
committed
Add convert_currency
1 parent 560b833 commit 843e70a

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

docs/api/ancient.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ pythainlp.ancient
66
Modules
77
-------
88

9-
.. autofunction:: aksonhan_to_current
9+
.. autofunction:: aksonhan_to_current
10+
.. autofunction:: convert_currency

pythainlp/ancient/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Ancient versions of the Thai language
77
"""
88

9-
__all__ = ["aksonhan_to_current"]
9+
__all__ = ["aksonhan_to_current", "convert_currency"]
1010

1111
from pythainlp.ancient.aksonhan import aksonhan_to_current
12+
from pythainlp.ancient.currency import convert_currency

pythainlp/ancient/currency.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
3+
# SPDX-FileType: SOURCE
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
def convert_currency(value: float, from_unit: str) -> dict:
7+
"""
8+
Convert ancient Thai currency to other units
9+
10+
:param float value: value
11+
:param str from_unit: currency unit \
12+
('เบี้ย', 'อัฐ', 'ไพ', 'เฟื้อง', 'สลึง', 'บาท', 'ตำลึง', 'ชั่ง')
13+
:return: Thai currency
14+
:rtype: dict
15+
"""
16+
conversion_factors_to_att = {
17+
'เบี้ย': 1,
18+
'อัฐ': 100, # 1 อัฐ = 100 เบี้ย
19+
'ไพ': 2 * 100, # 1 ไพ = 2 อัฐ
20+
'เฟื้อง': 4 * 2 * 100, # 1 เฟื้อง = 4 ไพ
21+
'สลึง': 2 * 4 * 2 * 100, # 1 สลึง = 2 เฟื้อง
22+
'บาท': 4 * 2 * 4 * 2 * 100, # 1 บาท = 4 สลึง
23+
'ตำลึง': 4 * 4 * 2 * 4 * 2 * 100, # 1 ตำลึง = 4 บาท
24+
'ชั่ง': 20 * 4 * 4 * 2 * 4 * 2 * 100, # 1 ชั่ง = 20 ตำลึง
25+
}
26+
27+
if from_unit not in conversion_factors_to_att:
28+
raise NotImplementedError(
29+
f"Currency unit '{from_unit}' is not support."
30+
)
31+
32+
# start from 'อัฐ'
33+
value_in_att = value * conversion_factors_to_att[from_unit]
34+
35+
# Calculate values ​​in other units
36+
results = {}
37+
for unit, factor in conversion_factors_to_att.items():
38+
results[unit] = value_in_att / factor
39+
40+
return results

tests/core/test_ancient.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import unittest
77

8-
from pythainlp.ancient import aksonhan_to_current
8+
from pythainlp.ancient import aksonhan_to_current, convert_currency
99

1010

1111
class AncientTestCase(unittest.TestCase):
@@ -23,3 +23,25 @@ def test_aksonhan_to_current(self):
2323
self.assertEqual(aksonhan_to_current("หลงง"), "หลัง")
2424
self.assertEqual(aksonhan_to_current("บงงคบบ"), "บังคับ")
2525
self.assertEqual(aksonhan_to_current("สรรเพชญ"), "สรรเพชญ")
26+
27+
def test_convert_currency(self):
28+
self.assertEqual(
29+
convert_currency(80, "บาท")["ตำลึง"],
30+
20.0
31+
)
32+
self.assertEqual(
33+
convert_currency(80, "บาท")["ชั่ง"],
34+
1.0
35+
)
36+
self.assertEqual(
37+
convert_currency(80, "บาท")["บาท"],
38+
80.0
39+
)
40+
self.assertEqual(
41+
convert_currency(1,"ชั่ง")["บาท"],
42+
80.0
43+
)
44+
self.assertEqual(
45+
convert_currency(1,"ชั่ง")["ชั่ง"],
46+
1.0
47+
)

0 commit comments

Comments
 (0)