【问题标题】:Looping over a text collection to extract subchapters循环文本集合以提取子章节
【发布时间】:2019-05-15 13:15:03
【问题描述】:

作为我的示例 here 的延续,我现在面临的问题是,我想在 R 中为我的文档集合中的所有文档提取子章节以进行进一步的文本挖掘。这是我的样本数据

doc_title <- c("Example.docx", "AnotherExample.docx")
text <- 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.", "2.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.")

doc_corpus <- data.frame(doc_title, text)

这是将文本分成子章节的功能:

divideInto_subchapters <- function(doc_corpus){

  corpus_text <- doc_corpus$text

  # Replace lines starting with N.N.N+ with space
  corpus_text <- gsub("\\R\\d+(?:\\.\\d+){2,}\\s+[A-Z].*\\R?", " ", corpus_text, perl=TRUE)

  # Split into IDs and Texts
  data <- str_match_all(corpus_text, "(?sm)^(\\d+(?:\\.\\d+)?\\s+[A-Z][^\r\n]*)\\R(.*?)(?=\\R\\d+(?:\\.\\d+)?\\s+[A-Z]|\\z)")

  # Get the chapter ID column
  chapter_id <- trimws(data[[1]][,2])

  # Get the text ID column
  text <- trimws(data[[1]][,3])

  # Create the target DF
  corpus <- data.frame(doc_title, chapter_id, text)

  return(corpus)
}

现在我想遍历doc_corpus 中的所有元素,并将所有纯文本分成子章节。这是我到目前为止尝试过的:

subchapter_corpus <- data.frame()

for (i in 1:nrow(doc_corpus)) {
  temp_corpus <- divideInto_subchapters(doc_corpus[i])
  subchapter_corpus <- rbind(subchapter_corpus, temp_corpus)
}

不幸的是,这会返回一个空数据框。我在这里做错了什么?非常感谢任何帮助。 我对第一个 df 行的预期输出如下所示:

doc_title <- c("Example.docx")
chapter_id <- (c("1 Introduction")) 
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.""))

chapter_one_df <- data.frame(doc_title, chapter_id, text)

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    所以,对我来说,循环给了我“下标越界”,直到我将 doc_corpus[i] 更改为 doc_corpus[i, ]。通过这种更改,我确实在结果数据框中得到了一行。

    然而,这只是chapter_id“2.2 进一步的未来”。它似乎缺少“1.1 Futher”。

    如果这是正则表达式的问题,那么如果您评论一下您正在使用它做什么,那肯定会有所帮助! :)

    请随时发表评论,我会根据需要修改我的答案,直到有帮助为止。不确定它是否是这样工作的,但这只是我回答关于 SO 问题的第三天。

    【讨论】:

    • 太好了,问题是缺少分号!!
    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    相关资源
    最近更新 更多