File size: 2,228 Bytes
e4b9a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0

import unittest

import numpy as np

from pythainlp.tools.misspell import misspell


def _count_difference(st1: str, st2: str) -> int:
    # this assumes len(st1) == len(st2)

    count = 0
    for i in range(len(st1)):
        if st1[i] != st2[i]:
            count += 1

    return count


class MisspellTestCase(unittest.TestCase):
    def setUp(self):
        self.texts = ["เรารักคุณมากที่สุดในโลก", "เราอยู่ที่มหาวิทยาลัยขอนแก่น"]

    def test_misspell_naive(self):
        for text in self.texts:
            misspelled = misspell(text, ratio=0.1)

            self.assertEqual(len(text), len(misspelled))

            diff = _count_difference(text, misspelled)

            self.assertGreater(diff, 0, "we have some misspells.")

    def test_misspell_with_ratio_0_percent(self):
        for text in self.texts:
            misspelled = misspell(text, ratio=0.0)

            self.assertEqual(len(text), len(misspelled))

            diff = _count_difference(text, misspelled)

            self.assertEqual(
                diff, 0, "we shouldn't have any misspell with ratio=0."
            )

    def test_misspell_with_ratio_50_percent(self):
        for text in self.texts:
            misspelled = misspell(text, ratio=0.5)

            self.assertEqual(len(text), len(misspelled))

            diff = _count_difference(text, misspelled)

            self.assertLessEqual(
                np.abs(diff - 0.5 * len(text)),
                2,
                f"expect 0.5*len(text)±2 misspells with ratio=0.5. (Δ={diff})",
            )

    def test_misspell_with_ratio_100_percent(self):
        for text in self.texts:
            misspelled = misspell(text, ratio=1)

            self.assertEqual(len(text), len(misspelled))

            diff = _count_difference(text, misspelled)

            self.assertLessEqual(
                np.abs(diff - len(text)),
                2,
                f"expect len(text)-2 misspells with ratio=1.5. (Δ={diff})",
            )