Skip to content

Commit a4ceea4

Browse files
authored
Merge pull request #1384 from phoneee/fix/metasound-algorithm
fix: metasound truncation order, add 4 missing consonants per paper
2 parents 1264a3b + 3afcc75 commit a4ceea4

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

pythainlp/soundex/metasound.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
_CONS_THANTHAKHAT: str = "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬอฮ์"
1717
_THANTHAKHAT: str = "์" # \u0e4c
1818
_C1: str = "กขฃคฆฅ" # sound K -> coded letter 1
19-
_C2: str = "จฉชฌซฐทฒดฎตสศษ" # D -> 2
19+
_C2: str = "จฉชฌซฐฏทฑฒถธดฎตสศษ" # D -> 2
2020
_C3: str = "ฟฝพผภบป" # B -> 3
2121
_C4: str = "ง" # NG -> 4
2222
_C5: str = "ลฬรนณฦญ" # N -> 5
@@ -71,7 +71,8 @@ def metasound(text: str, length: int = 4) -> str:
7171
chars[i] = " "
7272
i += 1
7373

74-
# retain first consonant, encode the rest
74+
# filter out spaces left by karan removal, then truncate
75+
chars = [c for c in chars if c != " "]
7576
chars = chars[:length]
7677
i = 1
7778
while i < len(chars):

pythainlp/soundex/prayut_and_somchaip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
_C0: str = "AEIOUHWYอ"
2020
_C1: str = "BFPVบฝฟปผพภว"
21-
_C2: str = "CGJKQSXZขฃคฅฆฉขฌกจซศษส"
21+
_C2: str = "CGJKQSXZขฃคฅฆฉฌกจซศษส"
2222
_C3: str = "DTฎดฏตฐฑฒถทธ"
2323
_C4: str = "Lลฬ"
2424
_C5: str = "MNมณน"

tests/core/test_soundex.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,18 @@ def test_complete_soundex_similarity(self):
169169
short_code, long_code
170170
)
171171
self.assertAlmostEqual(similarity_diff_len, 5 / 11, places=4)
172+
173+
def test_metasound_karan_truncation(self):
174+
# B1: karan spaces should be filtered before truncation
175+
# "สรรค์พล" has karan in middle — พ must not be lost
176+
self.assertEqual(metasound("สรรค์พล", 4), "ส553")
177+
self.assertEqual(metasound("รักษ์นา", 4), "ร150")
178+
# No karan — should be unaffected
179+
self.assertEqual(metasound("บูรณการ", 4), "บ551")
180+
181+
def test_metasound_consonant_classification(self):
182+
# B2: ถ,ธ,ฏ,ฑ should be class 2 (same sound as ท,ด)
183+
self.assertEqual(metasound("กถ", 2), "ก2")
184+
self.assertEqual(metasound("กธ", 2), "ก2")
185+
self.assertEqual(metasound("กฏ", 2), "ก2")
186+
self.assertEqual(metasound("กฑ", 2), "ก2")

0 commit comments

Comments
 (0)