Last active
September 3, 2020 03:52
-
-
Save brandonko/724d409fbb5331ff89431c7d08b0dfea to your computer and use it in GitHub Desktop.
Text Preprocessing (Punctuation, Capitalization, and Tokenization)
This file contains 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 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