Как и мой вопрос здесь, я хочу извлечь последовательности символов в строке с регулярным выражением в R. Я хочу извлечь разделы из текстового документа, в результате чего во фрейме данных, где каждый подразделрассматривается как собственный вектор, для дальнейшего Text Mining.Это мой пример данных:
chapter_one <- c("One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin.
1 Introduction
He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections.
1.1 Futher
The bedding was hardly able to cover it and seemed ready to slide off any moment.
1.1.1 This Should be Part of One Point One
His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked.
1.2 Futher Fuhter
'What's happened to me?' he thought. It wasn't a dream. His room, a proper human room although a little too small, lay peacefully between its four familiar walls.")
Это мой ожидаемый результат:
chapter_id <- (c("1 Introduction", "1.1 Futher", "1.2 Futher Futher"))
text <- (c("He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections.", "The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked.", "'What's happened to me?' he thought. It wasn't a dream. His room, a proper human room although a little too small, lay peacefully between its four familiar walls."))
chapter_one_df <- data.frame(chapter_id, text)
То, что я до сих пор пробовал, выглядит примерно так:
library(stringr)
regex_chapter_heading <- regex("
[:digit:] # Digit number
# MISSING: Optional dot and optional second digit number
\\s # Space
([[:alpha:]]) # Alphabetic characters (MISSING: can also contain punctuation, as in 'Introduction - A short introduction')
", comments = TRUE)
read.table(text=gsub(regex_chapter_heading,"\\1:",chapter_one),sep=":")
Итакпока что это не приводит к ожидаемому результату - потому что, как указано, части Regex по-прежнему отсутствуют.Любая помощь высоко ценится!