Skip to content

Instantly share code, notes, and snippets.

@brandonko
Last active September 3, 2020 03:52
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 brandonko/724d409fbb5331ff89431c7d08b0dfea to your computer and use it in GitHub Desktop.
Save brandonko/724d409fbb5331ff89431c7d08b0dfea to your computer and use it in GitHub Desktop.
Text Preprocessing (Punctuation, Capitalization, and Tokenization)
import gensim
import string
# Uses gensim to process the sentences
def sentence_to_words(sentences):
for sentence in sentences:
sentence_tokenized = gensim.utils.simple_preprocess(sentence,
deacc=True,
min_len=2,
max_len=15)
# Make sure we don't yield empty arrays
if len(sentence_tokenized) > 0:
yield sentence_tokenized
# Process the sentences manually
def sentence_to_words_from_scratch(sentences):
for sentence in sentences:
sentence_tokenized = [token.lower() for token in
word_tokenize(sentence.translate(str.maketrans('','',string.punctuation)))]
# Make sure we don't yield empty arrays
if len(sentence_tokenized) > 0:
yield sentence_tokenized
sentences = list(sentence_to_words(sentences))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment