Return a Vector of Each Word Found Before the End of a Sentence
[This article was first published on RLang.io | R Language Programming, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This little function returns the vector of each word found before the end of a sentence. I ended up writing this for a pet project to help with the babble function within the ngrams R package.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
termination_words <- toupper(as.character(sapply(unlist(strsplit(text,'\\.')),function(x) { | |
words <- unlist(strsplit(x,' ')) | |
return(words[length(words)]) | |
}))) |
It can be used to find the best spot to terminate sentences from the resulting babbles and adjusted to fit your needs.
stops <- data.frame(table(termination_words)) stops <- stops[which(stops$Freq>10),]
The stringr package also has similar functionality, str_extract to work the way I wanted. Probably just me though.
To leave a comment for the author, please follow the link and comment on their blog: RLang.io | R Language Programming.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.