Python nltk.sent_tokenize() Function

NLTK in Python:

NLTK is a Python toolkit for working with natural language processing (NLP). It provides us with a large number of test datasets for various text processing libraries. NLTK can be used to perform a variety of tasks such as tokenizing, parse tree visualization, and so on.

Tokenization

Tokenization is the process of dividing a large amount of text into smaller pieces/parts known as tokens. These tokens are extremely valuable for detecting patterns and are regarded as the first stage in stemming and lemmatization. Tokenization also aids in the replacement of sensitive data elements with non-sensitive data elements.

Natural language processing is utilized in the development of applications such as text classification, intelligent chatbots, sentiment analysis, language translation, and so on. To attain the above target, it is essential to consider the pattern in the text.

nltk.sent_tokenize() Function in Python:

The method sent_tokenize() is to used split the text into sentences.

The sent tokenize sub-module is provided for the above. The obvious question is why phrase tokenization is required when word tokenization is available. Assume you need to count the average number of words every sentence; how will you do it? To determine the ratio, you’ll need both the NLTK sentence tokenizer and the NLTK word tokenizer. Because the answer is numerical, such output is useful for machine learning.

Syntax:

sent_tokenize(text)

Advantages of sentence tokenization

The benefits of using NLTK for sentence tokenization are described below.

  • NLTK allows you to perform text data mining on sentences.
  • The NLTK sentence tokenization method compares different text corporas at the sentence level.
  • Sentence tokenization with NLTK allows you to see how many sentences are used in various sources of text, such as websites, books, and papers.
  • The NLTK “sent tokenize” function allows you to observe how sentences are linked to one another and which bridge words are used.
  • It is feasible to perform an overall sentiment analysis for the sentences using the NLTK sentence tokenizer.
  • One of the advantages of NLTK sentence tokenization is the ability to do Semantic Role Labeling on the sentences in order to understand how the sentences are related to one another.

nltk.sent_tokenize() Function in Python

Method #1: Using sent_tokenize Function (Static Input)

Approach:

  • Import sent_tokenize() function from tokenize of the nltk module using the import keyword
  • Give the string as static input and store it in a variable.
  • Pass the above-given string as an argument to the sent_tokenize() function to tokenize into sentences and print the result.
  • The Exit of the Program.

Below is the implementation:

Output:

['hello this is Python-programs!', 'good morning...']

Here, the text is tokenized into sentences. By putting all of the sentences into a list and tokenizing them with NLTK, you can see which sentences are related to which ones, the average word count per sentence, and the unique sentence count.

Method #2: Using sent_tokenize Function (User Input)

Approach:

  • Import sent_tokenize() function from tokenize of the nltk module using the import keyword
  • Give the string as user input using the input() function and store it in a variable.
  • Pass the above-given string as an argument to the sent_tokenize() function to tokenize into sentences and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import sent_tokenize() function from tokenize of the nltk module using the import keyword
from nltk.tokenize import sent_tokenize

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")

# Pass the above given string as an argument to the sent_tokenize() function to 
# tokenize into sentences and print the result.
print(sent_tokenize(gvn_str))

Output:

Enter some random string = good morning. Python-programs. welcome all
['good morning.', 'Python-programs.', 'welcome all']