2017-06-13 68 views
0

在我的数据(这是文本)中,有缩写。在R中查找数据中的缩写

是否有任何函数或代码在文本中搜索缩写?例如,检测3-4-5大写字母缩写并让我计算它们发生的频率。

非常感谢!

+0

你想在一个时间来算一个缩写或你会缩写的列表。是否可以使用示例更详细地解释要求?谢谢 – Alok

+0

基于正则表达式的函数将成为你的朋友。也可以看看[str_count()](https://cran.r-project.org/web/packages/stringr/vignettes/stringr.html) –

+0

你可以这样做:'strings < - c(“ABC text ABCD文本ABCDef文本再次ABCDE ABCDG“); library(stringr); str_count(strings,pattern =“\\ b [AZ] {3} \\ b”);'为了得到4,5等,您可以将'{3}'改为'{4}'等等 –

回答

1

检测3-4-5大写字母缩写

您可以使用

\b[A-Z]{3,5}\b 

regex demo

详细

  • \b - 一个字边界
  • [A-Z]{3,5} - 3,4或5个大写字母(使用[[:upper:]]匹配ASCII以外的字母,太)
  • \b - 一个字边界。

R demo online(利用从@TheComeOnMan正则表达式出现计数代码)

abbrev_regex <- "\\b[A-Z]{3,5}\\b"; 
x <- "XYZ was seen at WXYZ with VWXYZ and did ABCDEFGH." 
sum(gregexpr(abbrev_regex,x)[[1]] > 0) 
## => [1] 3 
regmatches(x, gregexpr(abbrev_regex, x))[[1]] 
## => [1] "XYZ" "WXYZ" "VWXYZ" 
0

您可以使用正则表达式[A-Z]来匹配acapital字母的任何出现。如果你想这个模式重复3次,你可以添加\1{3}到你的正则表达式。考虑使用变量和循环来完成3到5次重复时间的工作。