Code for Counting Sentences in Text using tokenizers Package
R
if (! require (tokenizers)) { install.packages ( "tokenizers" ) library (tokenizers) } text <- "This is an example gfg sentence. Another gfg sentence! this is last example." sentences <- unlist ( tokenize_sentences (text)) num_sentences <- length (sentences) cat ( "Number of sentences using tokenizers:" , num_sentences, "\n" ) |
Output:
Number of sentences using tokenizers: 3
- we store text data in text variable.
- use tokenize_sentences() to tokenize text into sentences.
- unlist() to list the sentences and store it in sentences .
- length() to count sentences and display it using cat .
As there are three sentences in text variable . Two of them separated by full stop(.) and one of them separated by exclamation mark(!). The count is 3.
How to count the number of sentences in a text in R
A fundamental task in R that is frequently used in text analysis and natural language processing is counting the number of sentences in a text. Sentence counting is necessary for many applications, including language modelling, sentiment analysis, and text summarization. In this article, we’ll look at various techniques and R packages for quickly and correctly counting the amount of phrases in a given text using R.