text_plain_output_1.png
from nltk.tokenize import TreebankWordTokenizer

from nltk.tokenize import WordPunctTokenizer

from nltk.tokenize import sent_tokenize

import nltk

import nltk

import nltk

text1 = "ThIs's   ã sent tokenize test  .  this's sent two. is this sent three? sent 4 is cool! Now it's your turn."

"""
Sentence tokenize in NLTK with sent_tokenize 
The sent_tokenize function uses an instance of NLTK known as PunktSentenceTokenizer
This instance of NLTK has already been trained to perform tokenization on different European languages on the basis of letters or punctuation that mark the beginning and end of sentences
"""
from nltk.tokenize import sent_tokenize
'\nAutres manières \n'
punkt_st = nltk.tokenize.PunktSentenceTokenizer()
sample_sentences = punkt_st.tokenize(text1)

""" Autre manière de procéder """
import nltk.data
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
french_tokenizer = nltk.data.load('tokenizers/punkt/french.pickle')

""" 
Naive Split
"""
'\nTokenizing text into words\n'
import nltk
tokens = nltk.word_tokenize(text1)
'\nEquivalent method with TreebankWordTokenizer\n'
from nltk.tokenize import TreebankWordTokenizer
tokenizer = TreebankWordTokenizer()
'\nEquivalent method with WordPunctTokenizer  \n'
from nltk.tokenize import WordPunctTokenizer
tokenizer = WordPunctTokenizer()

def tokenize_word_text(text):
    tokens = nltk.word_tokenize(text)
    tokens = [token.strip() for token in tokens]
    return tokens

"""
Starting point : tokens
"""
tokens = tokenize_word_text(text1)

"""
Converting all letters to lower or upper case (common : lower case)
"""
def convert_letters(tokens, style='lower'):
    if style == 'lower':
        tokens = [token.lower() for token in tokens]
    else:
        tokens = [token.upper() for token in tokens]
    return tokens
tokens = convert_letters(tokens)

"""
Remove blancs
"""
def remove_blanc(tokens):
    tokens = [token.strip() for token in tokens]
    return tokens
tokens = remove_blanc(tokens)

"""
Removing accent marks and other diacritics 
"""
def remove_accent(tokens):
    tokens = [unidecode.unidecode(token) for token in tokens]
    return tokens
tokens = remove_accent(tokens)

"""
Use a stopwords list
"""
stopword_list = nltk.corpus.stopwords.words('english')
' \nCreate your own stopwords list\n'
stopwords = ['a', 'about', 'above', 'across', 'after', 'afterwards']
stopwords += ['again', 'against', 'all', 'almost', 'alone', 'along']
stopwords += ['this', 'is', 'your']

def removeStopwords(wordlist, stopwords):
    return [w for w in wordlist if w not in stopwords]
tokens = nltk.word_tokenize(text1)
removeStopwords(tokens, stopwords)