Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve scikit-learn compatibility for CleanTransformer + tests #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion cleantext/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ def __init__(
self.replace_with_punct = replace_with_punct
self.lang = lang

def fit(self, X: Any):
def fit(self, X: Any, y=None):
"""
This method is defined for compatibility. It does nothing.
"""
return self

def partial_fit(self, X: Any, y=None):
"""
This method is defined for compatibility. It does nothing.
"""
Expand All @@ -89,3 +95,14 @@ def transform(self, X: Union[List[str], pd.Series]) -> Union[List[str], pd.Serie
return X.apply(lambda text: clean(text, **self.get_params()))
else:
return list(map(lambda text: clean(text, **self.get_params()), X))

def get_feature_names_out(self, feature_names_out=None):
"""
For compatibility with scikit-learn Pipeline objects.
This transformer will only return one column, which is ``'Clean Text``.
Args:
feature_names_out: Defined for compatibility with scikit-learn.
Returns:
list[str]: List with one element (i.e. The cleaned text column).
"""
return ["Clean Text"]
8 changes: 7 additions & 1 deletion tests/test_clean_transformer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
try:
from cleantext.sklearn import CleanTransformer
import pandas as pd

import pytest

transformer = CleanTransformer()
Expand All @@ -25,5 +24,12 @@ def test_set_params():
assert transformer.get_params()["no_line_breaks"]
assert transformer.get_params()["no_digits"]

def test_get_feature_names_out():
feature_names = transformer.get_feature_names_out()
assert feature_names == ["Clean Text"]

def test_fit():
transformer.fit(["sample1", "sample2"], [0, 1])
transformer.partial_fit(["sample1", "sample2"])
except ImportError:
pass