Skip to content

Commit 9a1ca86

Browse files
authored
Merge pull request #1114 from PyThaiNLP/add-convert_currency
Add convert_currency
2 parents 560b833 + 4fb3c62 commit 9a1ca86

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
* เบี้ย (Bia)
11+
* อัฐ (At)
12+
* ไพ (Pi)
13+
* เฟื้อง (Feuang)
14+
* สลึง (Saleung)
15+
* บาท (Bath)
16+
* ตำลึง (Tamleung)
17+
* ชั่ง (Chang)
18+
19+
See more:
20+
`Thai money <https://en.wikipedia.org/wiki/History_of_Thai_money>`_.
21+
22+
:param float value: value
23+
:param str from_unit: currency unit \
24+
('เบี้ย', 'อัฐ', 'ไพ', 'เฟื้อง', 'สลึง', 'บาท', 'ตำลึง', 'ชั่ง')
25+
:return: Thai currency
26+
:rtype: dict
27+
28+
:Example:
29+
::
30+
31+
from pythainlp.ancient import convert_currency
32+
33+
print(convert_currency(8, "บาท"))
34+
# output:
35+
# {
36+
# 'เบี้ย': 51200.0,
37+
# 'อัฐ': 512.0,
38+
# 'ไพ': 256.0,
39+
# 'เฟื้อง': 64.0,
40+
# 'สลึง': 32.0,
41+
# 'บาท': 8.0,
42+
# 'ตำลึง': 2.0,
43+
# 'ชั่ง': 0.1
44+
# }
45+
"""
46+
conversion_factors_to_att = {
47+
'เบี้ย': 1,
48+
'อัฐ': 100, # 1 อัฐ = 100 เบี้ย
49+
'ไพ': 2 * 100, # 1 ไพ = 2 อัฐ
50+
'เฟื้อง': 4 * 2 * 100, # 1 เฟื้อง = 4 ไพ
51+
'สลึง': 2 * 4 * 2 * 100, # 1 สลึง = 2 เฟื้อง
52+
'บาท': 4 * 2 * 4 * 2 * 100, # 1 บาท = 4 สลึง
53+
'ตำลึง': 4 * 4 * 2 * 4 * 2 * 100, # 1 ตำลึง = 4 บาท
54+
'ชั่ง': 20 * 4 * 4 * 2 * 4 * 2 * 100, # 1 ชั่ง = 20 ตำลึง
55+
}
56+
57+
if from_unit not in conversion_factors_to_att:
58+
raise NotImplementedError(
59+
f"Currency unit '{from_unit}' is not support."
60+
)
61+
62+
# start from 'อัฐ'
63+
value_in_att = value * conversion_factors_to_att[from_unit]
64+
65+
# Calculate values ​​in other units
66+
results = {}
67+
for unit, factor in conversion_factors_to_att.items():
68+
results[unit] = value_in_att / factor
69+
70+
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)