Last active
October 28, 2020 20:53
-
-
Save hkaraoguz/21a13571eba26d8afad411f14535356b to your computer and use it in GitHub Desktop.
sample text preprocessing for nlp
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
import string | |
import re | |
import nltk | |
def preprocess_text(text): | |
# Make lowercase | |
text = text.lower() | |
#print(text) | |
# Remove mentions and http links | |
text = re.sub(r"(?:\@|https?\://)\S+", "", text) | |
# Remove punctionation | |
text = ''.join(char for char in text if char not in string.punctuation) | |
# Get words | |
words = nltk.word_tokenize(text) | |
# Perform stemming | |
porter = nltk.stem.porter.PorterStemmer() | |
stemmed = [porter.stem(word) for word in words] | |
#print(words) | |
#print(stemmed) | |
return stemmed | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment