Code for Counting Sentences in Text using openNLP Package
R
if (! require (openNLP)) { install.packages ( "openNLP" ) #this will install the package if not present library (openNLP) } text <- "This is gfg sentence. Another sentence from gfg ! And a third one?" sent_token_annotator <- Maxent_Sent_Token_Annotator () sentences <- sent_token_annotator (text) num_sentences <- length (sentences) cat ( "Number of sentences using openNLP:" , num_sentences, "\n" ) |
Output:
Number of sentences using openNLP: 3
- we store text in text variable .
- Then we set data as “sent_token_english” which will load the model .
- maxent sentence tokenizer to count number of sentences .
- Finally we use length() to count length of sentences and we will display it using cat .
- Make Sure you have JAVA installed and path is set to make this code work.
Here there are 3 sentences seperated by full stop(.) , exclamation mark(!) and question mark(?) respectively . Hence the output 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.