R - 将提取的文本数据(每个实例作为行)导出为 data.frame 格式
R - Export Extracted Text Data (Each Instance as Row) to data.frame Format
我正在尝试 extract/export 将 i 个标准化 .txt 表单中的 i 个标准化实例中的文本输入到一个数据框中,其中每个实例都是一个单独的行。然后我想将该数据导出为 .xlsx 文件。到目前为止,我可以成功提取数据(尽管该算法提取的数据比规定的 gregexpr() 参数多一点)但只能导出为 .txt 作为文本的总和。
- 如何创建提取的 txt 文件文本的数据框,其中每个实例都有自己的行?
(一旦数据为 data.frame 格式,我就知道如何从那里导出为 xlsx。)
- 如何只从我设置的参数中提取数据?
在帮助(特别是来自 Ben from the comments of this post)的帮助下,这是我目前的情况:
# Txt Data Format
txt 1 <-
"A. The First: abcdefg hijklmnop qrstuv wxyz.
B. The Second: abcdefg hijklmnop qrstuv wxyz.
C. The Third: abcdefg hijklmnop qrstuv wxyz.
D. The Fourth: abcdefg hijklmnop qrstuv wxyz.
A. The First: abcdefg hijklmnop qrstuv wxyz.
B. The Second: abcdefg hijklmnop qrstuv wxyz.
C. The Third: abcdefg hijklmnop qrstuv wxyz.
D. The Fourth: abcdefg hijklmnop qrstuv wxyz."
txt 2 <-
"A. The First: abcdefg hijklmnop qrstuv wxyz.
B. The Second: abcdefg hijklmnop qrstuv wxyz.
C. The Third: abcdefg hijklmnop qrstuv wxyz.
D. The Fourth: abcdefg hijklmnop qrstuv wxyz.
A. The First: abcdefg hijklmnop qrstuv wxyz.
B. The Second: abcdefg hijklmnop qrstuv wxyz.
C. The Third: abcdefg hijklmnop qrstuv wxyz.
D. The Fourth: abcdefg hijklmnop qrstuv wxyz."
#################################
# Directory and Text Extraction #
#################################
dest <- "C:/Desktop/"
docs_text <- list.files(path = dest, pattern = "txt", full.names = TRUE)
## Assumes that all the content I want to extract is between "A." and "C." in
## the text while ignoring "C." and "D." content.
docs_list <- list.files(path = dest, pattern = "txt", full.names = TRUE)
docs_doc <- lapply(docs_list, function(i) {
j <- paste0(scan(i, what = character()), collapse = " ")
regmatches(j, gregexpr("(?<=A. The First).*?(?=C. The Third)", j, perl=TRUE))
})
lapply(1:length(docs_doc), function(i) write.table(docs_doc[i], file=paste(docs_list[i], " ",
" ", sep="."), quote = FALSE, row.names = FALSE, col.names = FALSE, eol = " " ))
当前输出看起来像这样,其中所有文本都在一行中并且捕获的不仅仅是 "A." 和 "C." 之间的内容:
所需的输出看起来像这样,其中仅捕获 "A." 和 "C." 之间的多行文本,并且每个多行捕获为每个实例分配一行:
您能提供的任何帮助都将非常有帮助!
我最终正在尝试开发一个 NLP 模型,该模型可以从数百个大型 PDF 中提取标准化表单数据,用于年复一年的存储库。如果这个 post 表明我没有考虑如何解决这个问题 efficiently/effectively,我愿意接受指导。
提前致谢!
我使用 dplyr
是为了方便 tibble
对象和非常有效的 bind_rows
命令:
dest <- "~"
docs_text <- list.files(path = dest, pattern = "txt", full.names = TRUE)
library(dplyr)
docs_df <- lapply(docs_text, function(f) {
lines <- readLines(f)
tibble(
file = basename(f),
line = seq_along(lines),
text = lines
)
}) %>%
bind_rows()
一旦您有了合适的 data.frame
,就可以很容易地使用 filter
和 grepl
对其进行子集化以查找匹配的文本。我正在使用正则表达式 "^A.|^B."
查找以 A. 或 B.:
开头的行
docs_df %>%
filter(grepl("^A.|^B.", text))
#> # A tibble: 8 x 3
#> file line text
#> <chr> <int> <chr>
#> 1 txt_1.txt 1 A. The First: abcdefg hijklmnop qrstuv wxyz.
#> 2 txt_1.txt 2 B. The Second: abcdefg hijklmnop qrstuv wxyz.
#> 3 txt_1.txt 6 A. The First: abcdefg hijklmnop qrstuv wxyz.
#> 4 txt_1.txt 7 B. The Second: abcdefg hijklmnop qrstuv wxyz.
#> 5 txt_2.txt 1 A. The First: abcdefg hijklmnop qrstuv wxyz.
#> 6 txt_2.txt 2 B. The Second: abcdefg hijklmnop qrstuv wxyz.
#> 7 txt_2.txt 6 A. The First: abcdefg hijklmnop qrstuv wxyz.
#> 8 txt_2.txt 7 B. The Second: abcdefg hijklmnop qrstuv wxyz.
要导出到 Excel 我推荐 rio::export()
。
我正在尝试 extract/export 将 i 个标准化 .txt 表单中的 i 个标准化实例中的文本输入到一个数据框中,其中每个实例都是一个单独的行。然后我想将该数据导出为 .xlsx 文件。到目前为止,我可以成功提取数据(尽管该算法提取的数据比规定的 gregexpr() 参数多一点)但只能导出为 .txt 作为文本的总和。
- 如何创建提取的 txt 文件文本的数据框,其中每个实例都有自己的行? (一旦数据为 data.frame 格式,我就知道如何从那里导出为 xlsx。)
- 如何只从我设置的参数中提取数据?
在帮助(特别是来自 Ben from the comments of this post)的帮助下,这是我目前的情况:
# Txt Data Format
txt 1 <-
"A. The First: abcdefg hijklmnop qrstuv wxyz.
B. The Second: abcdefg hijklmnop qrstuv wxyz.
C. The Third: abcdefg hijklmnop qrstuv wxyz.
D. The Fourth: abcdefg hijklmnop qrstuv wxyz.
A. The First: abcdefg hijklmnop qrstuv wxyz.
B. The Second: abcdefg hijklmnop qrstuv wxyz.
C. The Third: abcdefg hijklmnop qrstuv wxyz.
D. The Fourth: abcdefg hijklmnop qrstuv wxyz."
txt 2 <-
"A. The First: abcdefg hijklmnop qrstuv wxyz.
B. The Second: abcdefg hijklmnop qrstuv wxyz.
C. The Third: abcdefg hijklmnop qrstuv wxyz.
D. The Fourth: abcdefg hijklmnop qrstuv wxyz.
A. The First: abcdefg hijklmnop qrstuv wxyz.
B. The Second: abcdefg hijklmnop qrstuv wxyz.
C. The Third: abcdefg hijklmnop qrstuv wxyz.
D. The Fourth: abcdefg hijklmnop qrstuv wxyz."
#################################
# Directory and Text Extraction #
#################################
dest <- "C:/Desktop/"
docs_text <- list.files(path = dest, pattern = "txt", full.names = TRUE)
## Assumes that all the content I want to extract is between "A." and "C." in
## the text while ignoring "C." and "D." content.
docs_list <- list.files(path = dest, pattern = "txt", full.names = TRUE)
docs_doc <- lapply(docs_list, function(i) {
j <- paste0(scan(i, what = character()), collapse = " ")
regmatches(j, gregexpr("(?<=A. The First).*?(?=C. The Third)", j, perl=TRUE))
})
lapply(1:length(docs_doc), function(i) write.table(docs_doc[i], file=paste(docs_list[i], " ",
" ", sep="."), quote = FALSE, row.names = FALSE, col.names = FALSE, eol = " " ))
当前输出看起来像这样,其中所有文本都在一行中并且捕获的不仅仅是 "A." 和 "C." 之间的内容:
所需的输出看起来像这样,其中仅捕获 "A." 和 "C." 之间的多行文本,并且每个多行捕获为每个实例分配一行:
您能提供的任何帮助都将非常有帮助!
我最终正在尝试开发一个 NLP 模型,该模型可以从数百个大型 PDF 中提取标准化表单数据,用于年复一年的存储库。如果这个 post 表明我没有考虑如何解决这个问题 efficiently/effectively,我愿意接受指导。
提前致谢!
我使用 dplyr
是为了方便 tibble
对象和非常有效的 bind_rows
命令:
dest <- "~"
docs_text <- list.files(path = dest, pattern = "txt", full.names = TRUE)
library(dplyr)
docs_df <- lapply(docs_text, function(f) {
lines <- readLines(f)
tibble(
file = basename(f),
line = seq_along(lines),
text = lines
)
}) %>%
bind_rows()
一旦您有了合适的 data.frame
,就可以很容易地使用 filter
和 grepl
对其进行子集化以查找匹配的文本。我正在使用正则表达式 "^A.|^B."
查找以 A. 或 B.:
docs_df %>%
filter(grepl("^A.|^B.", text))
#> # A tibble: 8 x 3
#> file line text
#> <chr> <int> <chr>
#> 1 txt_1.txt 1 A. The First: abcdefg hijklmnop qrstuv wxyz.
#> 2 txt_1.txt 2 B. The Second: abcdefg hijklmnop qrstuv wxyz.
#> 3 txt_1.txt 6 A. The First: abcdefg hijklmnop qrstuv wxyz.
#> 4 txt_1.txt 7 B. The Second: abcdefg hijklmnop qrstuv wxyz.
#> 5 txt_2.txt 1 A. The First: abcdefg hijklmnop qrstuv wxyz.
#> 6 txt_2.txt 2 B. The Second: abcdefg hijklmnop qrstuv wxyz.
#> 7 txt_2.txt 6 A. The First: abcdefg hijklmnop qrstuv wxyz.
#> 8 txt_2.txt 7 B. The Second: abcdefg hijklmnop qrstuv wxyz.
要导出到 Excel 我推荐 rio::export()
。