Skip to content

TextNormalizer


class atom.nlp.TextNormalizer(stopwords=True, custom_stopwords=None, stem=False, lemmatize=True, verbose=0, logger=None)[source]
Normalize the corpus.

Convert words to a more uniform standard. The transformations are applied on the column named corpus, in the same order the parameters are presented. If there is no column with that name, an exception is raised. If the provided documents are strings, words are separated by spaces.

This class can be accessed from atom through the textnormalize method. Read more in the user guide.

Parametersstopwords: bool or str, default=True
Whether to remove a predefined dictionary of stopwords.

  • If False: Don't remove any predefined stopwords.
  • If True: Drop predefined english stopwords from the text.
  • If str: Language from nltk.corpus.stopwords.words.

custom_stopwords: sequence or None, default=None
Custom stopwords to remove from the text.

stem: bool or str, default=False
Whether to apply stemming using SnowballStemmer.

  • If False: Don't apply stemming.
  • If True: Apply stemmer based on the english language.
  • If str: Language from SnowballStemmer.languages.

lemmatize: bool, default=True
Whether to apply lemmatization using WordNetLemmatizer.

verbose: int, default=0
Verbosity level of the class. Choose from:

  • 0 to not print anything.
  • 1 to print basic information.
  • 2 to print detailed information.

logger: str, Logger or None, default=None

  • If None: Doesn't save a logging file.
  • If str: Name of the log file. Use "auto" for automatic naming.
  • Else: Python logging.Logger instance.


See Also

TextCleaner

Applies standard text cleaning to the corpus.

Tokenizer

Tokenize the corpus.

Vectorizer

Vectorize text data.


Example

>>> from atom import ATOMClassifier

>>> X = [
...    ["I àm in ne'w york"],
...    ["New york is nice"],
...    ["new york"],
...    ["hi there this is a test!"],
...    ["another line..."],
...    ["new york is larger than washington"],
...    ["running the test"],
...    ["this is a test"],
... ]
>>> y = [1, 0, 0, 1, 1, 1, 0, 0]

>>> atom = ATOMClassifier(X, y)
>>> print(atom.dataset)

                               corpus  target
0                    running the test       0
1            hi there this is a test!       1
2                      this is a test       0
3  new york is larger than washington       1
4                    New york is nice       0
5                   I àm in ne'w york       1
6                     another line...       1
7                            new york       0

>>> atom.textnormalize(stopwords="english", lemmatize=True, verbose=2)

Fitting TextNormalizer...
Normalizing the corpus...
 --> Dropping stopwords.
 --> Applying lemmatization.

>>> print(atom.dataset)

                           corpus  target
0                     [run, test]       0
1                     [hi, test!]       1
2                          [test]       0
3  [new, york, large, washington]       1
4               [New, york, nice]       0
5             [I, àm, ne'w, york]       1
6              [another, line...]       1
7                     [new, york]       0
>>> from atom.nlp import TextNormalizer

>>> X = [
...    ["I àm in ne'w york"],
...    ["New york is nice"],
...    ["new york"],
...    ["hi there this is a test!"],
...    ["another line..."],
...    ["new york is larger than washington"],
...    ["running the test"],
...    ["this is a test"],
... ]
>>> y = [1, 0, 0, 1, 1, 1, 0, 0]

>>> textnormalizer = TextNormalizer(
...     stopwords="english",
...     lemmatize=True,
...     verbose=2,
... )
>>> X = textnormalizer.transform(X)

Fitting TextNormalizer...
Normalizing the corpus...
 --> Dropping stopwords.
 --> Applying lemmatization.

>>> print(X)

                           corpus
0             [I, àm, ne'w, york]
1               [New, york, nice]
2                     [new, york]
3                     [hi, test!]
4              [another, line...]
5  [new, york, large, washington]
6                     [run, test]
7                          [test]


Methods

fitDoes nothing.
fit_transformFit to data, then transform it.
get_paramsGet parameters for this estimator.
inverse_transformDoes nothing.
logPrint message and save to log file.
saveSave the instance to a pickle file.
set_paramsSet the parameters of this estimator.
transformNormalize the text.


method fit(X=None, y=None, **fit_params)[source]
Does nothing.

Implemented for continuity of the API.

ParametersX: dataframe-like or None, default=None
Feature set with shape=(n_samples, n_features). If None, X is ignored.

y: int, str, sequence, dataframe-like or None, default=None
Target column corresponding to X.

  • If None: y is ignored.
  • If int: Position of the target column in X.
  • If str: Name of the target column in X.
  • If sequence: Target column with shape=(n_samples,) or sequence of column names or positions for multioutput tasks.
  • If dataframe-like: Target columns with shape=(n_samples, n_targets) for multioutput tasks.

**fit_params
Additional keyword arguments for the fit method.

Returnsself
Estimator instance.



method fit_transform(X=None, y=None, **fit_params)[source]
Fit to data, then transform it.

ParametersX: dataframe-like or None, default=None
Feature set with shape=(n_samples, n_features). If None, X is ignored.

y: int, str, sequence, dataframe-like or None, default=None
Target column corresponding to X.

  • If None: y is ignored.
  • If int: Position of the target column in X.
  • If str: Name of the target column in X.
  • If sequence: Target column with shape=(n_samples,) or sequence of column names or positions for multioutput tasks.
  • If dataframe-like: Target columns with shape=(n_samples, n_targets) for multioutput tasks.

**fit_params
Additional keyword arguments for the fit method.

Returnsdataframe
Transformed feature set. Only returned if provided.

series
Transformed target column. Only returned if provided.



method get_params(deep=True)[source]
Get parameters for this estimator.

Parametersdeep : bool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returnsparams : dict
Parameter names mapped to their values.



method inverse_transform(X=None, y=None)[source]
Does nothing.

ParametersX: dataframe-like or None, default=None
Feature set with shape=(n_samples, n_features). If None, X is ignored.

y: int, str, sequence, dataframe-like or None, default=None
Target column corresponding to X.

  • If None: y is ignored.
  • If int: Position of the target column in X.
  • If str: Name of the target column in X.
  • If sequence: Target column with shape=(n_samples,) or sequence of column names or positions for multioutput tasks.
  • If dataframe-like: Target columns with shape=(n_samples, n_targets) for multioutput tasks.

Returnsdataframe
Transformed feature set. Only returned if provided.

series
Transformed target column. Only returned if provided.



method log(msg, level=0, severity="info")[source]
Print message and save to log file.

Parametersmsg: int, float or str
Message to save to the logger and print to stdout.

level: int, default=0
Minimum verbosity level to print the message.

severity: str, default="info"
Severity level of the message. Choose from: debug, info, warning, error, critical.



method save(filename="auto", save_data=True)[source]
Save the instance to a pickle file.

Parametersfilename: str, default="auto"
Name of the file. Use "auto" for automatic naming.

save_data: bool, default=True
Whether to save the dataset with the instance. This parameter is ignored if the method is not called from atom. If False, add the data to the load method.



method set_params(**params)[source]
Set the parameters of this estimator.

Parameters**params : dict
Estimator parameters.

Returnsself : estimator instance
Estimator instance.



method transform(X, y=None)[source]
Normalize the text.

ParametersX: dataframe-like
Feature set with shape=(n_samples, n_features). If X is not a dataframe, it should be composed of a single feature containing the text documents.

y: int, str, sequence, dataframe-like or None, default=None
Does nothing. Implemented for continuity of the API.

Returnsdataframe
Transformed corpus.