generated from Pseudo-Lab/Jupyter-Book-Template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
``` | ||
✗ tree tests | ||
tests | ||
├── __init__.py | ||
├── test_normalize.py | ||
└── text_processing.py | ||
``` | ||
- text_processing.py | ||
```python | ||
def normalize(input_str): | ||
""" | ||
인풋으로 받는 스트링에서 아래의 규칙으로 정규화된 스트링을 반환하는 함수입니다. | ||
* 모든 단어들은 소문자로 변환됨 | ||
* 띄어쓰기는 한칸으로 되도록 함 | ||
* 앞뒤 필요없는 띄어쓰기는 제거함 | ||
Parameters: | ||
input_str (string): 영어로 된 대문자, 소문자, 띄어쓰기, 문장부호, 숫자로 이루어진 string | ||
ex - " EXTRA SPACE " | ||
Returns: | ||
normalized_string (string): 정규회된 string | ||
ex - 'extra space' | ||
Examples: | ||
>>> import text_processing as tp | ||
>>> example = " EXTRA SPACE " | ||
>>> tp.normalize(example) | ||
'extra space' | ||
""" | ||
out = input_str.lower() | ||
out = out.strip() | ||
while ' ' in out: | ||
out = out.replace(' ', ' ') | ||
return out | ||
``` | ||
- test_normalize.py | ||
```python | ||
import unittest | ||
from text_processing import normalize | ||
|
||
|
||
class TestTextNormalize(unittest.TestCase): | ||
def test_normalize(self): | ||
test_str = "This is an example." | ||
pred = normalize(test_str) | ||
self.assertEqual(pred, "this is an example.") | ||
|
||
def test_extra_space(self): | ||
test_str = " EXTRA SPACE " | ||
pred = normalize(test_str) | ||
self.assertEqual(pred, "extra space") | ||
|
||
def test_uppercase(self): | ||
test_str = "THIS IS ALL UPPERCASE!!" | ||
pred = normalize(test_str) | ||
self.assertEqual(pred, "this is all uppercase!!") | ||
|
||
def test_lowercase(self): | ||
test_str = "this is all lowercase..." | ||
pred = normalize(test_str) | ||
self.assertEqual(pred, "this is all lowercase...") | ||
|
||
def test_whitespace(self): | ||
test_str = " " | ||
pred = normalize(test_str) | ||
self.assertEqual(pred, "") | ||
``` | ||
### Result | ||
<img width="733" alt="image" src="https://github.com/heehehe/CPython-Guide/assets/41580746/d8798a21-6399-42a2-a280-5d435e9ced16"> |