Created
February 22, 2019 12:45
-
-
Save agustinustheo/652a8c17294e68413180e30cb5cd5575 to your computer and use it in GitHub Desktop.
Preprocess Text function for Filtering Fake News Blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def preproccess_text(text_messages): | |
| # change words to lower case - Hello, HELLO, hello are all the same word | |
| processed = text_messages.lower() | |
| # Remove remove unnecessary noise | |
| processed = re.sub(r'\[[0-9]+\]|\[[a-z]+\]|\[[A-Z]+\]|\\\\|\\r|\\t|\\n|\\', ' ', processed) | |
| # Remove punctuation | |
| processed = re.sub(r'[.,\/#!%\^&\*;\[\]:|+{}=\-\'"_”“`~(’)?]', ' ', processed) | |
| # Replace whitespace between terms with a single space | |
| processed = re.sub(r'\s+', ' ', processed) | |
| # Remove leading and trailing whitespace | |
| processed = re.sub(r'^\s+|\s+?$', '', processed) | |
| return processed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment