Skip to content

Instantly share code, notes, and snippets.

@hkaraoguz
Last active October 28, 2020 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkaraoguz/21a13571eba26d8afad411f14535356b to your computer and use it in GitHub Desktop.
Save hkaraoguz/21a13571eba26d8afad411f14535356b to your computer and use it in GitHub Desktop.
sample text preprocessing for nlp
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