У меня есть датафрейм с предложениями; в некоторых предложениях слова используются более одного раза:
df <- data.frame(Turn = c("well this is what the grumble about do n't they ?",
"it 's like being in a play-group , in n it ?",
"oh is that that steak i got the other night ?",
"well where have the middle sized soda stream bottle gone ?",
"this is a half day , right ? needs a full day",
"yourself , everybody 'd be changing your hair in n it ?",
"cos he finishes at four o'clock on that day anyway .",
"no no no i 'm dave and you 're alan .",
"yeah , i mean the the film was quite long though",
"it had steve martin in it , it 's a comedy",
"oh it is a dreary old day in n it ?",
"no it 's not mother theresa , it 's saint theresa .",
"oh have you seen that face lift job he wants ?",
"yeah bolshoi 's right so which one is it then ?"))
Я хочу сопоставить те предложения, в которых слово, любое слово повторяется один или несколько раз.
РЕДАКТИРОВАТЬ 1 :
Повторяющиеся слова ** могут * быть смежными, но это не обязательно. Вот почему Регулярное выражение для последовательных повторяющихся слов не дает ответа на мой вопрос.
У меня был скромный успех с этим кодом:
df[grepl("(\\w+\\b\\s)\\1{1,}", df$Turn),]
[1] well this is what the grumble about do n't they ?
[2] it 's like being in a play-group , in n it ?
[3] oh is that that steak i got the other night ?
[4] this is a half day , right ? needs a full day
[5] yourself , everybody 'd be changing your hair in n it ?
[6] no no no i 'm dave and you 're alan .
[7] yeah , i mean the the film was quite long though
[8] it had steve martin in it , it 's a comedy
[9] oh it is a dreary old day in n it ?
Успех просто скромный, потому что некоторые предложения совпадают, что не должно совпадать , например, yourself , everybody 'd be changing your hair in n it ?
, в то время как другие не совпадают, что должно быть , например, no it 's not mother theresa , it 's saint theresa .
. Как улучшить код для получения точных совпадений?
Ожидаемый результат :
df
Turn
2 it 's like being in a play-group , in n it ?
3 oh is that that steak i got the other night ?
5 this is a half day , right ? needs a full day
8 no no no i 'm dave and you 're alan .
9 yeah , i mean the the film was quite long though
10 it had steve martin in it , it 's a comedy
11 oh it is a dreary old day in n it ?
12 no it 's not mother theresa , it 's saint theresa .
РЕДАКТИРОВАТЬ 2 :
Другой вопрос заключается в том, как определить точное количество повторных слов. Вышеупомянутое, несовершенное, регулярное выражение соответствует словам, которые повторяются по крайней мере один раз. Если я изменю квантификатор на {2}
, таким образом, ища тройное вхождение слова, я получу этот код и такой результат:
df[grepl("(\\w+\\b\\s)\\1{2}", df$Turn),]
[1] no no no i 'm dave and you 're alan . # "no" occurs 3 times
Но опять же совпадение несовершенно, поскольку ожидаемый результат будет:
[1] no no no i 'm dave and you 're alan . # "no" occurs 3 times
[2] it had steve martin in it , it 's a comedy # "it" occurs 3 times
Любая помощь очень ценится!