Change language

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. ”)]

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically