Python | Tokenizing text using TextBlob

Install TextBlob using the following commands in a terminal:

 pip install -U textblob python -m textblob.download_corpora 

This will install TextBlob and download the required NLTK packages. The above installation will take quite a long time due to the sheer number of tokenizers, chunkers, other algorithms and the entire downloadable corporation.

Some terms that will be used frequently:

  • Housing — The main body of the text, singular. Corpora is the plural of this.
  • Lexicon — Words and their meanings.
  • Token — each “entity” that is part of what has been split based on rules. For example, each word is a token when a sentence is “tokenized” to words. Each sentence can also be a token if you`ve tokenized sentences from a paragraph.

So basically, tokenization involves separating sentences and words from the body of the text.

# from text block lib. import TextBlob method

from textblob import TextBlob

 

text = ( "Natural language processing (NLP) is a field " +  

  " of computer science , artificial intelligence " +  

  " and computational linguistics concerned with " +  

  "the interactions between computers and human" +  

"(natural) languages, and, in particul ar, " +  

  " concerned with programming computers to " +  

  "fruitfully process large natural language" +  

" corpora. Challenges in natural language " +  

  "processing frequently involve natural" +  

"language understanding, natural language" +  

" generation frequently from formal, machine " +  

  "- readable logical forms), connecting language" +  

"and machine perception, managing human-" +  

" computer dialog systems, or some combination " +  

  " thereof. " )

  
# create TextBlob

blob_object = TextBlob (text)

 
# break the paragraph into words.

print ( "Word Tokenize:" , blob_object.words)

 
# break the paragraph into sentences.

print ( "Sentence Tokenize:" , blob_object.sentences)

Output:

Word Tokenize:
[`Natural`, `l anguage `,` processing `,` NLP `,` is`, `a`, `field`, `of`, `computer`, `science`, `artificial`, `intelligence`, `and`, `computational` , `linguistics`,` concerned `,` with `,` the `,` interactions`, `between`, `computers`,` and `,` human `,` natural `,` languages`, `and`, ` in `,` particular `,` concerned `,` with `,` programming `,` computers`, `to`, `fruitfully`, `process`,` large `,` natural `,` language `,` corpora ` , `Challenges`,` in `,` natural `,` language `,` processing `,` frequently `,` involve `,` natural `,` language `,` understanding `,` natural `,` languagegeneration `,` frequently `,` from `,` formal `,` machine-readable `,` logical `,` forms `,` connecting `,` language `,` and `,` machine `,` perception `,` managing `,` human-computer `,` dialog `,` systems`, `or`, `some`, `combination`, `thereof`]

Sentence Tokenize:
[Sentence (“Natural language processing (NLP) is a field of computer science, artificial intelligence and computational linguistics concerned with the interactions between computers and human (natural) languages, and, in particular, concerned with programming computers to fruitfully process large natural language corpora. ”), Sentence (“ Challenges in natural language processing frequently involve natural language understanding, natural language generation frequently from formal, machine-readable logical forms), connecting language and machine perception, managing human-computer dialog systems, or some combination thereof. ”)]