|
| 1 | +from werx import weighted_wer |
| 2 | + |
| 3 | +# ---------------------------------------------------------------------- |
| 4 | +# Test 1: Alias Consistency |
| 5 | +# ---------------------------------------------------------------------- |
| 6 | +ref = ["i love cold pizza"] |
| 7 | +hyp = ["i love pizza"] |
| 8 | +test1 = weighted_wer(ref, hyp, insertion_weight=0.5, deletion_weight=0.5, substitution_weight=1.0) |
| 9 | +print("Test 1 Result:", test1) |
| 10 | + |
| 11 | +# ---------------------------------------------------------------------- |
| 12 | +# Test 2: Basic Weighted WER with Reduced Insertion and Deletion Weights |
| 13 | +# ---------------------------------------------------------------------- |
| 14 | +ref = ["i love cold pizza"] |
| 15 | +hyp = ["i love pizza"] |
| 16 | +# 1 deletion, deletion weight = 0.5 → weighted cost = 0.5 / 4 = 0.125 |
| 17 | +expected_result = 0.125 |
| 18 | +test2 = weighted_wer(ref, hyp, insertion_weight=0.5, deletion_weight=0.5, substitution_weight=1.0) |
| 19 | +print("Test 2 Result:", test2) |
| 20 | + |
| 21 | +# ---------------------------------------------------------------------- |
| 22 | +# Test 3: Two-Sentence Input with Increased Substitution Weight |
| 23 | +# ---------------------------------------------------------------------- |
| 24 | +ref = ["i love cold pizza", "the sugar bear character was popular"] |
| 25 | +hyp = ["i love pizza", "the sugar bare character was popular"] |
| 26 | +# 1 deletion, 1 substitution; deletion_weight = 0.5, substitution_weight = 2.0 |
| 27 | +expected_result = (0.5 + 2.0) / (4 + 6) # Total words = 10 |
| 28 | +test3 = weighted_wer(ref, hyp, insertion_weight=0.5, deletion_weight=0.5, substitution_weight=2.0) |
| 29 | +print("Test 3 Result:", test3) |
| 30 | + |
| 31 | +# ---------------------------------------------------------------------- |
| 32 | +# Test 4: Edge Case with Zero Weights (Should Return 0 Regardless of Errors) |
| 33 | +# ---------------------------------------------------------------------- |
| 34 | +ref = ["i love cold pizza"] |
| 35 | +hyp = ["i love pizza"] |
| 36 | +test4 = weighted_wer(ref, hyp, insertion_weight=0.0, deletion_weight=0.0, substitution_weight=0.0) |
| 37 | +print("Test 4 Result:", test4) |
| 38 | + |
| 39 | +# ---------------------------------------------------------------------- |
| 40 | +# Test 5: All Weights Set to 1 (Equivalent to Standard WER) |
| 41 | +# ---------------------------------------------------------------------- |
| 42 | +ref = ["i love cold pizza"] |
| 43 | +hyp = ["i love pizza"] |
| 44 | +expected_result = 0.25 # 1 deletion / 4 words |
| 45 | +test5 = weighted_wer(ref, hyp) |
| 46 | +print("Test 5 Result:", test5) |
| 47 | + |
| 48 | +# ---------------------------------------------------------------------- |
| 49 | +# Test 6: Invalid Input Types Should Raise Exceptions |
| 50 | +# ---------------------------------------------------------------------- |
| 51 | +try: |
| 52 | + ref = [1, 2, 3] |
| 53 | + hyp = [2, 3, 4] |
| 54 | + test6 = weighted_wer(ref, hyp) |
| 55 | +except Exception as e: |
| 56 | + print("Test 6 Result: Exception Raised -", e) |
| 57 | + |
| 58 | +# ---------------------------------------------------------------------- |
| 59 | +# Test 7: Edge Case with Empty Input Strings — Should Return WER of 0.0 |
| 60 | +# ---------------------------------------------------------------------- |
| 61 | +ref = [""] |
| 62 | +hyp = [""] |
| 63 | +test7 = weighted_wer(ref, hyp) |
| 64 | +print("Test 7 Result:", test7) |
| 65 | + |
| 66 | +# ---------------------------------------------------------------------- |
| 67 | +# Test 8: Mismatched Reference and Hypothesis Lengths |
| 68 | +# ---------------------------------------------------------------------- |
| 69 | +try: |
| 70 | + ref = ["hello world", "another sentence"] |
| 71 | + hyp = ["hello world"] |
| 72 | + test8 = weighted_wer(ref, hyp) |
| 73 | +except Exception as e: |
| 74 | + print("Test 8 Result: Exception Raised -", e) |
| 75 | + |
| 76 | +# ---------------------------------------------------------------------- |
| 77 | +# Test 9: None Inputs Should Raise an Exception |
| 78 | +# ---------------------------------------------------------------------- |
| 79 | +try: |
| 80 | + ref = None |
| 81 | + hyp = ["hello world"] |
| 82 | + test9a = weighted_wer(ref, hyp) |
| 83 | +except Exception as e: |
| 84 | + print("Test 9a Result: Exception Raised -", e) |
| 85 | + |
| 86 | +try: |
| 87 | + ref = ["hello world"] |
| 88 | + hyp = None |
| 89 | + test9b = weighted_wer(ref, hyp) |
| 90 | +except Exception as e: |
| 91 | + print("Test 9b Result: Exception Raised -", e) |
| 92 | + |
| 93 | + |
| 94 | +# ---------------------------------------------------------------------- |
| 95 | +# Test 10: werpy comparison |
| 96 | +# ---------------------------------------------------------------------- |
| 97 | +ref = ['it was beautiful and sunny today'] |
| 98 | +hyp = ['it was a beautiful and sunny day'] |
| 99 | +werp = weighted_wer(ref, hyp, insertion_weight=0.5, deletion_weight=0.5, substitution_weight=1) |
| 100 | +print("Test 10 Result:", werp) |
| 101 | + |
| 102 | + |
| 103 | +# ---------------------------------------------------------------------- |
| 104 | +# Test 11: Heavy Substitution Impact |
| 105 | +# ---------------------------------------------------------------------- |
| 106 | +ref = ["the quick brown fox jumps over the lazy dog multiple times in the evening"] |
| 107 | +hyp = ["the fast brown cat leaps across the sleepy dog several times at night"] |
| 108 | +# Expect higher impact from substitutions due to synonym replacements |
| 109 | +test11 = weighted_wer(ref, hyp, insertion_weight=1.0, deletion_weight=1.0, substitution_weight=3.0) |
| 110 | +print("Test 11 (High Substitution Weight) Result:", test11) |
| 111 | + |
| 112 | + |
| 113 | +# ---------------------------------------------------------------------- |
| 114 | +# Test 12: Heavy Substitution Impact |
| 115 | +# ---------------------------------------------------------------------- |
| 116 | +ref = ["i love cold pizza", "the sugar bear character was popular"] |
| 117 | +hyp = ["i love pizza", "the sugar bare character was popular"] |
| 118 | +# 1 deletion, 1 substitution; |
| 119 | +test12a = weighted_wer(ref, hyp, insertion_weight=.5, deletion_weight=.5, substitution_weight=2.0) |
| 120 | +test12b = weighted_wer(ref, hyp, insertion_weight=1, deletion_weight=1, substitution_weight=1.0) |
| 121 | +test12c = weighted_wer(ref, hyp, insertion_weight=2, deletion_weight=2, substitution_weight=1.0) |
| 122 | +print("Test 12a Result:", test12a) |
| 123 | +print("Test 12b Result:", test12b) |
| 124 | +print("Test 12c Result:", test12c) |
| 125 | + |
| 126 | +# ---------------------------------------------------------------------- |
| 127 | +# Test 13: Example in weighted_wer.py |
| 128 | +# ---------------------------------------------------------------------- |
| 129 | +ref = ['it was beautiful and sunny today', 'tomorrow may not be as nice'] |
| 130 | +hyp = ['it was a beautiful and sunny day', 'tomorrow may not be as nice'] |
| 131 | +test13 = weighted_wer(ref, hyp, insertion_weight=0.5, deletion_weight=0.5, substitution_weight=1.0) |
| 132 | +print("Test 13 Result:", test13) |
0 commit comments