我core.py在檔案夾中找到的腳本preprocessing需要一個字串并清理它。它是更大模型的一部分(請參閱最后一個匯入,但這并不重要)。dict_english中的,app/core/preprocessing/constants只是我用其他單詞替換的不常見英語單詞的字典。
import string
from app.core.preprocessing.constants import dict_english
from app.core.generic.step import Step
from typing import Optional
from app.api.model.my_project_parameters import MyProjectParameters
class TextPreprocessingBase(Step[str, str]):
def process(self, input_value: str, parameters: Optional[MyProjectParameters] = None) -> str:
input_value = input_value.replace("'", '')
input_value = input_value.replace("\"", '')
printable = set(string.printable)
filter(lambda x: x in printable, input_value)
new_string=''.join(filter(lambda x: x in printable, input_value))
return new_string
class TextPreprocessingEnglish(TextPreprocessingBase):
def process(self, input_value: str, parameters: Optional[MyProjectParameters] = None) -> str:
process_english = super().process(input_value, parameters)
for word, initial in dict_english.items():
process_english = process_english.replace(word.lower(), initial)
return process_english
很容易測驗:
string_example= """ Random 'text' ?"""
a = TextPreprocessingEnglish()
output = a.process(string_example)
print(output)
它列印:
Random text
但我想寫一些自動測驗。我想:
import pytest
from app.core.preprocessing.core import TextPreprocessingBase, TextPreprocessingEnglish
class TestEnglishPreprocessing:
@pytest.fixture(scope='class')
def english_preprocessing:
...
但我被困在這里。我只想在我手動撰寫的幾個不同的字串上測驗我的代碼。是否可以這樣做,或者我只是像上面的簡單測驗示例那樣撰寫它?
uj5u.com熱心網友回復:
這聽起來像是您可以通過引數化測驗來解決的問題,例如:
import pytest
from process import TextPreprocessingEnglish
@pytest.mark.parametrize(
"input,expected",
[
(""" Random 'text' ?""", "Random text"),
(""" Some other 'text' ?""", "Some other text"),
],
)
def test_process(input, expected):
a = TextPreprocessingEnglish()
output = a.process(input)
assert output == expected
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/411817.html
標籤:
