|
| 1 | +import unittest |
| 2 | + |
| 3 | +from iamsystem import CacheFuzzyAlgos |
| 4 | +from iamsystem import Matcher |
| 5 | +from iamsystem.fuzzy.simstring import ESimStringMeasure |
| 6 | +from iamsystem.fuzzy.simstring import SimStringWrapper |
| 7 | + |
| 8 | + |
| 9 | +class MatcherTest(unittest.TestCase): |
| 10 | + def setUp(self) -> None: |
| 11 | + self.matcher = Matcher() |
| 12 | + self.matcher.add_labels(labels=["paracetamol", "les"]) |
| 13 | + |
| 14 | + def test_threshold_1(self): |
| 15 | + """Test threshold=1 is exact match""" |
| 16 | + fuzzy_ss = SimStringWrapper(words=["paracetamol"], threshold=1) |
| 17 | + syns = list(fuzzy_ss.get_syns_of_word("paracetomol")) |
| 18 | + self.assertEqual(0, len(syns)) |
| 19 | + syns = list(fuzzy_ss.get_syns_of_word("paracetamol")) |
| 20 | + self.assertEqual(1, len(syns)) |
| 21 | + |
| 22 | + def test_threshold_0_5(self): |
| 23 | + """Test synonyms returned depend on threshold.""" |
| 24 | + fuzzy_ss = SimStringWrapper(words=["paracetamol"]) |
| 25 | + syns = list(fuzzy_ss.get_syns_of_word("paracetomol")) |
| 26 | + self.assertEqual(1, len(syns)) |
| 27 | + syns = list(fuzzy_ss.get_syns_of_word("para")) |
| 28 | + self.assertEqual(0, len(syns)) |
| 29 | + |
| 30 | + def test_threshold_0_2(self): |
| 31 | + """Test synonyms returned depend on threshold.""" |
| 32 | + fuzzy_ss = SimStringWrapper(words=["paracetamol"], threshold=0.2) |
| 33 | + syns = list(fuzzy_ss.get_syns_of_word("paracetomol")) |
| 34 | + self.assertEqual(1, len(syns)) |
| 35 | + syns = list(fuzzy_ss.get_syns_of_word("para")) |
| 36 | + self.assertEqual(1, len(syns)) |
| 37 | + |
| 38 | + def test_measure_exact(self): |
| 39 | + """Test synonyms returned depend on threshold.""" |
| 40 | + fuzzy_ss = SimStringWrapper( |
| 41 | + words=["paracetamol"], measure=ESimStringMeasure.EXACT |
| 42 | + ) |
| 43 | + syns = list(fuzzy_ss.get_syns_of_word("paracetomol")) |
| 44 | + self.assertEqual(0, len(syns)) |
| 45 | + syns = list(fuzzy_ss.get_syns_of_word("paracetamol")) |
| 46 | + self.assertEqual(1, len(syns)) |
| 47 | + |
| 48 | + def test_other_measures(self): |
| 49 | + """Test other similarity measure ; check it returns a synonym.""" |
| 50 | + for measure in ESimStringMeasure: |
| 51 | + if measure.value == "exact": |
| 52 | + continue |
| 53 | + fuzzy_ss = SimStringWrapper(words=["paracetamol"], measure=measure) |
| 54 | + syns = list(fuzzy_ss.get_syns_of_word("paracetomol")) |
| 55 | + self.assertEqual(1, len(syns)) |
| 56 | + |
| 57 | + def test_matcher(self): |
| 58 | + """Test detection with a matcher""" |
| 59 | + fuzzy_ss = SimStringWrapper(words=self.matcher.get_keywords_unigrams()) |
| 60 | + self.matcher.add_fuzzy_algo(fuzzy_algo=fuzzy_ss) |
| 61 | + annots = self.matcher.annot_text(text="le paractamol") |
| 62 | + self.assertEqual(1, len(annots)) |
| 63 | + |
| 64 | + def test_cache_fuzzy_algos(self): |
| 65 | + """Test it can work with CacheFuzzyAlgos.""" |
| 66 | + fuzzy_ss = SimStringWrapper(words=self.matcher.get_keywords_unigrams()) |
| 67 | + cache = CacheFuzzyAlgos() |
| 68 | + cache.add_algo(algo=fuzzy_ss) |
| 69 | + self.matcher.add_fuzzy_algo(fuzzy_algo=cache) |
| 70 | + annots = self.matcher.annot_text(text="le paractamol") |
| 71 | + self.assertEqual(1, len(annots)) |
| 72 | + |
| 73 | + def test_combine_multiple_algos(self): |
| 74 | + """Test we can add multiple simstring algorithms.""" |
| 75 | + fuzzy_dice = SimStringWrapper( |
| 76 | + words=self.matcher.get_keywords_unigrams(), |
| 77 | + name="ss_dice", |
| 78 | + measure=ESimStringMeasure.DICE, |
| 79 | + ) |
| 80 | + fuzzy_jaccard = SimStringWrapper( |
| 81 | + words=self.matcher.get_keywords_unigrams(), |
| 82 | + name="ss_jaccard", |
| 83 | + measure=ESimStringMeasure.JACCARD, |
| 84 | + ) |
| 85 | + self.matcher.add_fuzzy_algo(fuzzy_algo=fuzzy_dice) |
| 86 | + self.matcher.add_fuzzy_algo(fuzzy_algo=fuzzy_jaccard) |
| 87 | + annots = self.matcher.annot_text(text="le paractamol") |
| 88 | + self.assertEqual(1, len(annots)) |
| 89 | + annot = annots[0] |
| 90 | + algos_token_0 = annot.algos[0] |
| 91 | + self.assertEqual(["ss_dice", "ss_jaccard"], algos_token_0) |
| 92 | + |
| 93 | + def test_combine_multiple_algos_2(self): |
| 94 | + """Test the two simstring databases are independent to allow |
| 95 | + the user to customize different files.""" |
| 96 | + fuzzy_dice = SimStringWrapper( |
| 97 | + words=self.matcher.get_keywords_unigrams(), |
| 98 | + name="ss_dice", |
| 99 | + measure=ESimStringMeasure.DICE, |
| 100 | + ) |
| 101 | + fuzzy_jaccard = SimStringWrapper( |
| 102 | + words=["NothingInterestingHere"], |
| 103 | + name="ss_jaccard", |
| 104 | + measure=ESimStringMeasure.JACCARD, |
| 105 | + ) |
| 106 | + self.matcher.add_fuzzy_algo(fuzzy_algo=fuzzy_dice) |
| 107 | + self.matcher.add_fuzzy_algo(fuzzy_algo=fuzzy_jaccard) |
| 108 | + annots = self.matcher.annot_text(text="le paractamol") |
| 109 | + self.assertEqual(1, len(annots)) |
| 110 | + annot = annots[0] |
| 111 | + algos_token_0 = annot.algos[0] |
| 112 | + self.assertEqual(["ss_dice"], algos_token_0) |
0 commit comments