如何计算R中文本中的句子数?

Sav*_*SUS 7 r text-mining

我使用该readChar()函数将文本读入R中.我的目的是检验这样一个假设,即文本的句子出现的字母"a"与字母"b"的出现次数一样多.我最近发现了这个{stringr}软件包,这对我的文本做了很多有用的事情,比如计算字符数和整个文本中每个字母的出现次数.现在,我需要知道整篇文章中的句子数量.R有任何功能,可以帮我做到吗?非常感谢你!

Sav*_*SUS 11

谢谢@ gui11aume的回答.我刚刚发现的一个非常好的包可以帮助完成工作{openNLP}.这是执行此操作的代码:

install.packages("openNLP") ## Installs the required natural language processing (NLP) package
install.packages("openNLPmodels.en") ## Installs the model files for the English language
library(openNLP) ## Loads the package for use in the task
library(openNLPmodels.en) ## Loads the model files for the English language

text = "Dr. Brown and Mrs. Theresa will be away from a very long time!!! I can't wait to see them again." ## This sentence has unusual punctuation as suggested by @gui11aume

x = sentDetect(text, language = "en") ## sentDetect() is the function to use. It detects and seperates sentences in a text. The first argument is the string vector (or text) and the second argument is the language.
x ## Displays the different sentences in the string vector (or text).

[1] "Dr. Brown and Mrs. Theresa will be away from a very long time!!! "
[2] "I can't wait to see them again."

length(x) ## Displays the number of sentences in the string vector (or text).

[1] 2
Run Code Online (Sandbox Code Playgroud)

{openNLP}包是R中的自然语言处理真正伟大的,你可以找到一个很好的和短的介绍到它这里,或者你可以检查出包的文档在这里.

包中还支持三种语言.您只需安装并加载相应的模型文件即可.

  1. {openNLPmodels.es} 为西班牙语
  2. {openNLPmodels.ge} 对于德国人
  3. {openNLPmodels.th} 泰国人


gui*_*ume 6

你正在寻找的是句子标记化,它并不像看起来那么简单,即使是在英语中(句子如"我遇见了Bennett博士,约翰逊夫人的前夫."可以包含句号).

R绝对不是自然语言处理的最佳选择.如果你是Python精通,我建议你看一下nltk模块,它涵盖了这个和许多其他主题.您还可以复制此博客文章中的代码,该文章执行句子标记化和单词标记化.

如果你想坚持到R,我建议你算结束句子的字符(.,?,!),因为你可以算个字符.使用正则表达式执行此操作的方式如下:

text <- 'Hello world!! Here are two sentences for you...'
length(gregexpr('[[:alnum:] ][.!?]', text)[[1]])
Run Code Online (Sandbox Code Playgroud)