R regexpr用字符向量中的相应输入替换括号之间的文本(create mad lib)

时间:2017-09-27 03:47:24

标签: r regex

我可能有几个角色向量:

cv <- "[dramatically/immensely] [increases/improves] [one's/your] career prospects in the field and allows [one/you] to pursue positions in"

cv <- "While [holders of/students with/graduates with] a degree in"

我想在括号中选择单词,并选择其中一个选项。例如,在"[dramatically/immensely]"之间,我希望我的代码可以选择(随机),"dramatically""immensely"。或者在[holders of/students with/graduates with]之间我希望我的代码选择holders ofstudents withgraduates with。我还需要确保它有意义,所以如果"[your/one's]"之类的第一选择将决定第二个

到目前为止,我有:

library(foreach)

wordChoices <- gsub("(\\[|\\])", "",regmatches(phrase1,gregexpr("\\[.*?\\]",phrase1))[[1]])
randomWords <- unlist(foreach(wordChoice=wordChoices) %do% {
        sample(strsplit(wordChoice,"/")[[1]],1)
      })
      if (any(regexpr("(you)",randomWords)>0)) {
        randomWords <- unlist(foreach(randomWord=randomWords) %do% {
        randomWord <- gsub("\\bone's\\b","your",randomWord)
        randomWord <- gsub("\\bone\\b","you",randomWord)
        })
      }

我最终得到了:

print(randomWords)
[1] "immensely" "increases" "your"      "you" 

现在我希望用这些代替句子中的各个位置,但是我被卡住了。

我希望有这个:

cv <- "immensely increases your career prospects in the field and allows you to pursue positions in"
编辑:我想我有一个解决方案的想法。循环遍历每个替换单词并在括号中进行非贪婪的替换选择。每次,您都会替换一个选项,因此下一个选择将是下一个循环的第一个匹配。

1 个答案:

答案 0 :(得分:0)

如果我们需要随机选择[]中的一个字词,我们可以尝试gsubfn

library(gsubfn)
f1 <- function(string){
   gsubfn("(\\[[^]]*\\])", ~sapply(strsplit(x,
      "[][/]"), function(y) sample(y[y!=""])[1]), string)
 }

f1(cv)
#[1] "dramatically increases one's career prospects in the field and allows one to pursue positions in"

f1(cv)
#[1] "immensely improves one's career prospects in the field and allows you to pursue positions in"

cv1 <- "While [holders of/students with/graduates with] a degree in"
f1(cv1)
#[1] "While holders of a degree in"
f1(cv1)
#[1] "While students with a degree in"