我想把next string分成几个句子:
library(NLP) # NLP_0.1-7
string <- as.String("Mr. Brown comes. He says hello. i give him coffee.")我想演示两种不同的方式。一个来自openNLP包
library(openNLP) # openNLP_0.2-5
sentence_token_annotator <- Maxent_Sent_Token_Annotator(language = "en")
boundaries_sentences<-annotate(string, sentence_token_annotator)
string[boundaries_sentences]
[1] "Mr. Brown comes." "He says hello." "i give him coffee." 第二个来自stringi包
library(stringi) # stringi_0.5-5
stri_split_boundaries( string , opts_brkiter=stri_opts_brkiter('sentence'))
[[1]]
[1] "Mr. " "Brown comes. "
[3] "He says hello. i give him coffee."在第二种方法之后,我需要准备句子来删除多余的空格,或者再次将一个新的字符串拆分成句子。我可以调整字符串函数来提高结果的质量吗?
当涉及到大数据时,openNLP比stringi (慢得多)。
有没有办法把stringi (->fast)和openNLP (->quality)结合起来?
发布于 2015-08-11 17:28:44
ICU中的文本边界(在本例中为句子边界)分析(以及字符串中的文本边界)由Unicode UAX29中描述的规则控制,另请参阅ICU Users Guide on the topic。我们读到:
Unicode规则不能检测诸如“...Mr.琼斯……“;需要更复杂的剪裁才能检测到这样的情况。
换句话说,如果没有一个自定义的不间断单词字典,就不能做到这一点,这实际上是在openNLP中实现的。因此,为执行此任务而合并字符串的几种可能方案包括:
stri_split_boundaries,然后编写一个函数来决定哪些不正确拆分的标记应该输入到文本中不间断的空格(可能在后面的点之后等,先生,即等等)(请注意,这实际上是在LaTeX中准备文档时所必需的--否则,在将自定义不间断单词列表words).stri_split_regex.时,您会得到太大的空格
诸若此类。
发布于 2015-08-11 09:50:07
这可能是一个可行的正则表达式解决方案:
string <- "Mr. Brown comes. He says hello. i give him coffee."
stringi::stri_split_regex(string, "(?<!\\w\\.\\w.)(?<![A-Z][a-z]\\.)(?<=\\.|\\?|\\!)\\s")
## [[1]]
## [1] "Mr. Brown comes." "He says hello." "i give him coffee."在以下方面表现不佳:
string <- "Mr. Brown comes! He says hello. i give him coffee. i will got at 5 p. m. eastern time. Or somewhere in between"https://stackoverflow.com/questions/31865511
复制相似问题