File size: 12,381 Bytes
74c716c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# MIT License
#
# Copyright (c) 2023 Victor Calderon
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
Module that contains the class definitions for the data preparation tasks.
"""

import logging
import re
from datetime import datetime
from typing import List, Optional, Tuple, Union

import pandas as pd
from spacy.lang.en.stop_words import STOP_WORDS

from src.classes import hugging_face_utils as hf
from src.utils import default_variables as dv
from src.utils import general_utilities as gu

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

# ---------------------------- CLASS DEFINITIONS ------------------------------


# -- Defining functions that can be used for cleaning up and preparing text
class NLPPrep(object):
    """
    Class object for handling the data processing of text.
    """

    def __init__(self):
        # Defining the corresponding stop words
        self.stop_words = list(STOP_WORDS)

    def _lowercase_text(self, input_string: str) -> str:
        """
        Method for making the input text lowercase.

        Parameters
        ------------
        input_string : str
            Text variable to lowercase.

        Returns
        ----------
        output_string : str
            Lower-cased version of ``input_string``.
        """

        return input_string.lower()

    def _only_keep_alphanumeric(self, input_string: str) -> str:
        """
        Method for only keeping alphanumerical characters in the text.

        Parameters
        ------------
        input_string : str
            Text variable to filter.

        Returns
        ----------
        output_string : str
            Filtered version of ``input_string`` that only contains
            alphanumerical characters.
        """
        regex_pattern = r"[^a-zA-z0-9\s]"

        return re.sub(regex_pattern, "", input_string)

    def _remove_stopwords(self, input_string: str) -> str:
        """
        Method for removing stop words from the input text.

        Parameters
        ------------
        input_string : str
            Text variable to filter.

        Returns
        ----------
        output_string : str
            Filtered version of ``input_string`` without stop words in
            the text.
        """
        # Splitting the text into 'tokens'
        tokens = input_string.strip().split()

        return " ".join(
            [word for word in tokens if word not in self.stop_words]
        )

    def _remove_unicode(self, input_str: str) -> str:
        """
        Method for removing Unicode from the input text.

        Parameters
        ------------
        input_str : str
            Text variable, from which to remove Unicode characters.

        Returns
        ----------
        string_decode : str
            Filtered version of ``input_str`` without the Unicode characters.
        """
        string_encode = input_str.encode("ascii", "ignore")

        return string_encode.decode()

    def process_text(self, input_string: str) -> str:
        """
        Method for passing the input variable through NLP-based techniques
        to process the text.

        Parameters
        ------------
        input_string : str
            Variable corresponding to the text that will be processed.

        Returns
        ------------
        processed_string : str
            Variable corresponding to the *processed* version of the input
            string, after having gone through some NLP-based processing
            techniques.

        Notes
        -----------
        This function will perform the following NLP-based techniques:

        1. Make the text lowercase.
        2. Remove any non-alphanumeric character from the string.
        3. Remove any stop words from the text.
        """
        # Remove Unicode characters
        processed_string = self._remove_unicode(input_string)
        # Lower case the text
        processed_string = self._lowercase_text(processed_string)
        # Removing non-alphanumeric characters
        processed_string = self._only_keep_alphanumeric(processed_string)
        # Removing stop words
        processed_string = self._remove_stopwords(processed_string)

        return processed_string


class DatasetPrep(object):
    """
    Class object for the Data Processing of the input dataset.
    """

    def __init__(
        self,
        dataset_path: str,
        **kwargs,
    ):
        """
        Class object for the Data Processing of the input dataset.

        Parameters
        ------------
        dataset_path : str
            Path / URL to the input dataset.
        """
        # Path to the output dataset
        self.datasets_dir = gu.get_project_paths()["data"]

        # Other parameters
        for colname in [
            "save_to_disk",
            "document_id_colname",
            "title_colname",
            "content_colname",
            "clean_content_colname",
        ]:
            setattr(self, colname, kwargs.get(colname, getattr(dv, colname)))

        # Initializing dataset
        self.dataset_path = dataset_path
        self.raw_dataset = self._get_dataset()

        # Extracting the number of rows and columns, and column names
        (
            self.n_rows,
            self.n_cols,
            self.columns_names,
        ) = self._get_columns_and_shape()

        # Initializing NLP-Prep Object
        self.nlp_obj = NLPPrep()

    def show_params(self):
        """
        Method for displaying the set of input parameters of the class.
        """

        gu.show_params(
            params_dict=self.__dict__,
            logger=logger,
            columns_to_omit=["raw_dataset"],
        )

    def _get_dataset(self) -> pd.DataFrame:
        # sourcery skip: class-extract-method
        """
        Method for extracting the dataset from the input source.

        Returns
        ----------
        raw_dataset : pandas.DataFrame
            DataFrame containing the data from the input source.
        """
        logger.info(f">> Extracting dataset from `{self.dataset_path}`")

        # Reading in dataset
        raw_dataset = pd.read_csv(self.dataset_path)

        # Saving to disk, if applicable
        if self.save_to_disk:
            dataset_filepath = self.datasets_dir.joinpath("raw_dataset.csv")
            dataset_filepath.parent.mkdir(exist_ok=True, parents=True)
            raw_dataset.to_csv(dataset_filepath, header=True, index=True)

            logger.info(f">> Raw dataset saved to '{str(dataset_filepath)}'")

        return raw_dataset

    def _get_columns_and_shape(self) -> Tuple[int, int, List]:
        # sourcery skip: use-fstring-for-formatting
        """
        Method for extracting the columns and information about the
        raw dataset.

        Returns
        ----------
        n_rows : int
            Number of rows in the original dataset.

        n_cols : int
            Number of columns in the original dataset.

        column_names_arr : list
            List of columns from the original dataset.
        """
        # Number of rows and columns
        n_rows, n_columns = self.raw_dataset.shape

        logger.info(
            ">> There are '{}' rows and '{}' columns in the dataset".format(
                n_rows,
                n_columns,
            )
        )

        # Column names
        column_names_arr = sorted(self.raw_dataset.columns)

        logger.info(
            ">> Columns in the dataset: \n\t{}".format(
                "\n\t".join(column_names_arr)
            )
        )

        return n_rows, n_columns, column_names_arr

    def _process_text(self, input_text: str) -> str:
        """
        Method for applying NLP-based techniques on an input text in order
        to prepare it to be used by the embedding algorithm.

        Parameters
        -----------
        input_text : str
            Variable corresponding to the input text.

        Returns
        -----------
        processed_text : str
            Processed version of the ``input_text``.

        Notes
        ----------
        This function will perform the following NLP-based techniques:

        1. Make the text lowercase.
        2. Remove any non-alphanumeric character from the string.
        3. Remove any stop words from the text.
        """

        return self.nlp_obj.process_text(input_string=input_text)

    def clean_dataset(self) -> pd.DataFrame:
        """
        Method for cleaning the raw dataset and create a clean version
        of the dataset.

        Returns
        ---------
        dataset_clean : pandas.DataFrame
            Clean version of the input dataset, after having gone through
            data-cleaning techniques.
        """
        # --- Start time
        logger.info(">> Data cleaning process ...")
        start_time = datetime.now()
        #

        # --- Making a copy of the raw dataset
        dataset_df = self.raw_dataset.copy()

        # --- Data-cleaning techniques
        # Removing duplicates
        dataset_df.drop_duplicates(keep="first", inplace=True)

        # Removing entries that have 'NaN' in the dataset
        dataset_df.dropna(how="any", inplace=True)

        # Casting proper data types
        dataset_df = dataset_df.astype(str)

        # Resetting the index of the dataset
        dataset_df.reset_index(drop=True, inplace=True)

        # Removing trailing whitespaces
        for colname in [self.document_id_colname, self.title_colname]:
            dataset_df.loc[:, colname] = dataset_df[colname].apply(
                lambda x: x.strip()
            )

        # Processing content
        dataset_df.loc[:, getattr(self, "clean_content_colname")] = dataset_df[
            getattr(self, "content_colname")
        ].apply(lambda text: self.nlp_obj.process_text(text))

        # --- Saving to disk, if applicable
        if self.save_to_disk:
            dataset_filepath = self.datasets_dir.joinpath("clean_dataset.csv")
            dataset_filepath.parent.mkdir(exist_ok=True, parents=True)
            dataset_df.to_csv(dataset_filepath, header=True, index=True)

            logger.info(f">> Clean dataset saved to '{str(dataset_filepath)}'")

        # --- End time
        end_time = datetime.now()
        logger.info(f">>    Finished at: {end_time}")
        logger.info(f">>    Took: {end_time - start_time}")
        logger.info(">> Data cleaning process ... DONE")

        return dataset_df

    def push_dataset_to_hub(
        self,
        dataset: pd.DataFrame,
        dataset_name: str,
        username: Optional[Union[None, str]] = None,
    ):
        """
        Method for pushing the ``dataset`` to the HuggingFace's Hub.

        Parameters
        -------------
        dataset : pandas.DataFrame
            Dataset that will be pushed to HuggingFace.

        dataset_name : str
            Name of the dataset to use.

        username : str, NoneType, optional
            Us
        """
        # Initializing class object
        hf_obj = hf.HuggingFaceHelper()

        # Transforming dataset type
        hf_dataset = hf_obj.convert_dataframe_to_dataset(input_df=dataset)

        # Push dataset to hub
        hf_obj.push_dataset(
            dataset=hf_dataset,
            dataset_name=dataset_name,
            username=username,
        )