From 492e732ccb94f4e412df982e3725b19198f04fe2 Mon Sep 17 00:00:00 2001 From: Martin Arrieta Date: Wed, 2 Aug 2017 00:13:59 -0300 Subject: [PATCH] Features added: 1- Custom stop words option in the remove_stop_words method 2- Added the method "remove_numbers" to remove the numbers 3- Added the method "replace_custom_regex" to remove a custom regex in the text. --- cucco/cucco.py | 66 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/cucco/cucco.py b/cucco/cucco.py index 6fc2fd1..52d6e0e 100644 --- a/cucco/cucco.py +++ b/cucco/cucco.py @@ -174,7 +174,7 @@ def remove_extra_white_spaces(text): """ return ' '.join(text.split()) - def remove_stop_words(self, text, ignore_case=True, language=None): + def remove_stop_words(self, text, ignore_case=True, language=None, custom_stop_words=None): """Remove stop words. Stop words are loaded on class instantiation according @@ -184,6 +184,7 @@ def remove_stop_words(self, text, ignore_case=True, language=None): text: The text to be processed. ignore_case: Whether or not to ignore case. language: Code of the language to use (defaults to 'en'). + custom_stop_words: Use a custom stop words list. Returns: The text without stop words. @@ -191,13 +192,47 @@ def remove_stop_words(self, text, ignore_case=True, language=None): if not language: language = self._config.language - if language not in self.__stop_words: - if not self._load_stop_words(language): - self._logger.error('No stop words file for the given language') - return text + if custom_stop_words: + stop_words = custom_stop_words + else: + if language not in self.__stop_words: + if not self._load_stop_words(language): + self._logger.error('No stop words file for the given language') + return text + stop_words = self.__stop_words[language] + + if ignore_case: + text = text.lower() + + words = text.split(' ') + ret_words = [] + for word in words: + if word not in stop_words: + ret_words.append(word) + + return ' '.join(ret_words) - return ' '.join(word for word in text.split(' ') if ( - word.lower() if ignore_case else word) not in self.__stop_words[language]) + def replace_numbers(self, text, replacement=''): + """Remove characters from text. + + Removes custom characters from input text or replaces them + with a string if specified. + + Args: + text: The text to be processed. + replacement: New text that will replace the custom characters. + + Returns: + The text without numbers. + """ + + words = text.split(' ') + ret_words = [] + for word in words: + if not word.isnumeric(): + ret_words.append(word) + + return ' '.join(ret_words) def replace_characters(self, text, characters, replacement=''): """Remove characters from text. @@ -340,3 +375,20 @@ def replace_urls(text, replacement=''): The text without URLs. """ return re.sub(regex.URL_REGEX, replacement, text) + + @staticmethod + def replace_custom_regex(text, regex, replacement=''): + """Replace URLs in text. + + Removes a custom regex from input text or replaces them with a + string if specified. + + Args: + text: The text to be processed. + regex: Custom compiled regex with re.compile() + replacement: New text that will replace URLs. + + Returns: + The text after replcae the regex. + """ + return re.sub(regex, replacement, text)