|
| 1 | +``` |
| 2 | +✗ tree tests |
| 3 | +tests |
| 4 | +├── __init__.py |
| 5 | +├── test_normalize.py |
| 6 | +└── text_processing.py |
| 7 | +``` |
| 8 | +- text_processing.py |
| 9 | + ```python |
| 10 | + def normalize(input_str): |
| 11 | + """ |
| 12 | + 인풋으로 받는 스트링에서 아래의 규칙으로 정규화된 스트링을 반환하는 함수입니다. |
| 13 | + * 모든 단어들은 소문자로 변환됨 |
| 14 | + * 띄어쓰기는 한칸으로 되도록 함 |
| 15 | + * 앞뒤 필요없는 띄어쓰기는 제거함 |
| 16 | + Parameters: |
| 17 | + input_str (string): 영어로 된 대문자, 소문자, 띄어쓰기, 문장부호, 숫자로 이루어진 string |
| 18 | + ex - " EXTRA SPACE " |
| 19 | + Returns: |
| 20 | + normalized_string (string): 정규회된 string |
| 21 | + ex - 'extra space' |
| 22 | + Examples: |
| 23 | + >>> import text_processing as tp |
| 24 | + >>> example = " EXTRA SPACE " |
| 25 | + >>> tp.normalize(example) |
| 26 | + 'extra space' |
| 27 | + """ |
| 28 | + out = input_str.lower() |
| 29 | + out = out.strip() |
| 30 | + while ' ' in out: |
| 31 | + out = out.replace(' ', ' ') |
| 32 | + return out |
| 33 | + ``` |
| 34 | +- test_normalize.py |
| 35 | + ```python |
| 36 | + import unittest |
| 37 | + from text_processing import normalize |
| 38 | + |
| 39 | + |
| 40 | + class TestTextNormalize(unittest.TestCase): |
| 41 | + def test_normalize(self): |
| 42 | + test_str = "This is an example." |
| 43 | + pred = normalize(test_str) |
| 44 | + self.assertEqual(pred, "this is an example.") |
| 45 | + |
| 46 | + def test_extra_space(self): |
| 47 | + test_str = " EXTRA SPACE " |
| 48 | + pred = normalize(test_str) |
| 49 | + self.assertEqual(pred, "extra space") |
| 50 | + |
| 51 | + def test_uppercase(self): |
| 52 | + test_str = "THIS IS ALL UPPERCASE!!" |
| 53 | + pred = normalize(test_str) |
| 54 | + self.assertEqual(pred, "this is all uppercase!!") |
| 55 | + |
| 56 | + def test_lowercase(self): |
| 57 | + test_str = "this is all lowercase..." |
| 58 | + pred = normalize(test_str) |
| 59 | + self.assertEqual(pred, "this is all lowercase...") |
| 60 | + |
| 61 | + def test_whitespace(self): |
| 62 | + test_str = " " |
| 63 | + pred = normalize(test_str) |
| 64 | + self.assertEqual(pred, "") |
| 65 | + ``` |
| 66 | +### Result |
| 67 | +<img width="733" alt="image" src="https://github.com/heehehe/CPython-Guide/assets/41580746/d8798a21-6399-42a2-a280-5d435e9ced16"> |
0 commit comments