Change language

Tokenizing text using NLTK in Python

| | |

To run the following Python program, you must have the Natural Language Toolkit (NLTK) installed on your system. 
Module NLTK — it is a massive set of tools designed to help you use the entire natural language processing (NLP) methodology. 
To install NLTK, run the following commands in your terminal.

  • sudo pip install nltk
  • Then enter the python shell into your terminal, just typing python
  • Type nltk import
  • nltk.download (& # 39; all & # 39;)

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 often to be used:

  • 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.

    # import existing word and tokenization sentence
    # libraries

    from nltk.tokenize import sent_tokenize , word_tokenize

     

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

      " of computer science, artificial int elligence " +

      "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 language" +

    "generation frequently from formal, machine" +

    "- readable logical forms), connecting language" +

    "and machine perception, managing human-" +

      "computer dialog systems, or some combination" +

      " thereof. "

     

    print (sent_tokenize (text))

    print (word_tokenize (text)) ’

    EXIT
    ["Natural Language Processing (NLP)" — it is the field of computer science, artificial intelligence and computational linguistics, related to the interaction of computers and human (natural) languages, and, in particular, related to the programming of computers for the fruitful processing of large corpuses of natural language. & # 39; & # 39; Problems in natural language processing often include natural language understanding, natural language generation (often from formal, machine-readable logical forms), linking language and machine perception, managing human-computer dialog systems, or some combination of these. & # 39;]
    [& # 39; Natural & # 39 ;, & # 39; language & # 39 ;, & # 39; processing & # 39;, & # 39; (& # 39;, & # 39 ; NLP & # 39 ;, & # 39;) & # 39 ;, & # 39; is & # 39 ;, & # 39; a & # 39;, & # 39; field & # 39 ;, & # 39; of & # 39 ;, & # 39; computer & # 39 ;, & # 39; science & # 39;, & # 39; , & # 39;, & # 39; artificial & # 39;, & # 39; intelligence & # 39;, & # 39; and & # 39;, & # 39; computing & # 39;, & # 39; linguistics & # 39;, & # 39; interested & # 39;, & # 39; with & # 39;, & # 39; the & # 39;, & # 39; interactions & # 39;, & # 39; between & # 39;, & # 39; computers & # 39;, & # 39; and & # 39; , & # 39; man & # 39;, & # 39; (& # 39;, & # 39; natural & # 39;, & # 39;) & # 39;, & # 39; languages ​​& # 39;, & # 39;, & # 39 ;, & # 39; and & # 39 ;, & # 39;, & # 39 ;, & # 39; in & # 39 ;, & # 39; specific & # 39;, & # 39 ;, & # 39;, interested & # 39;, & # 39; with & # 39;, & # 39; programming & # 39;, & # 39; computers & # 39;, & # 39; to & # 39;, & # 39; fruitfully & # 39;, & # 39; process & # 39;, & # 39; great & # 39;, & # 39; natural & # 39;, & # 39; language & # 39;, & # 39; corporation & # 39;, & # 39 ;. & # 39;, & # 39; calls & # 39;, & # 39; at & # 39; , “Natural”, “language”, “processing”, “often”, “involve”, “natural”, “language”, “understanding”, “,”, “natural”, “language”, “generation”, “ (& # 39;, & # 39; often & # 39;, & # 39; from & # 39;, & # 39; formally & # 39;, & # 39;, & # 39;, & # 39; machine-readable & # 39;, & # 39; logical & # 39;, & # 39; forms & # 39;, & # 39;) & # 39;, & # 39;, & # 39;, & # 39; connect & # 39;, & # 39; language & # 39;, & # 39; and & # 39;, & # 39; machine & # 39;, & # 39; perception & # 39;, & # 39;, & # 39;, & # 39; management & # 39;, & # 39; human computer & # 39;, & # 39; dialogue & # 39;, & # 39; systems & # 39;, & # 39;, & # 39;, & # 39; or & # 39;, & # 39; some & # 39;, & # 39; combination & # 39;, & # 39; of which & # 39;, & # 39 ;. & # 39;]

    So, we have created tokens, which are sentences first, but words — later.

    This article is courtesy of Pratima Upadhyay . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting the article [email protected] ... See my article appearing on the Python.Engineering homepage and help other geeks.

    Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

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