library(readtext)
doc.text <- readtext("test.docx")$text
# Split text into parts using new line character:
doc.parts <- strsplit(doc.text, "\n")[[1]]
# First line in the document- the name of the Journal
journal.name <- doc.parts[1]
journal.name
# [1] "International Journal of Science and Research (IJSR)"
# Similarly we can extract some other parts from a header
issn <- doc.parts[2]
issue <- doc.parts[3]
# Search for the Abstract:
abstract.loc <- grep("Abstract:", doc.parts)[1]
# Search for the Keyword
Keywords.loc <- grep("Keywords:", doc.parts)[1]
# The text in between these 2 keywords will be abstract text:
abstract.text <- paste(doc.parts[abstract.loc:(Keywords.loc-1)], collapse=" ")
# Same way we can get Keywords text:
Background.loc <- Keywords.loc + grep("1\\.", doc.parts[-(1:Keywords.loc)])[1]
Keywords.text <- paste(doc.parts[Keywords.loc:(Background.loc-1)], collapse=" ")
Keywords.text
# [1] "Keywords: Nephronophtisis, NPHP1 deletion, NPHP4 mutations, Tunisian patients"
# Assuming that Methods is part 2
Methods.loc <- Background.loc + grep("2\\.", doc.parts[-(1:Background.loc)])[1]
Background.text <- paste(doc.parts[Background.loc:(Methods.loc-1)], collapse=" ")
# Assuming that Results is Part 3
Results.loc <- Methods.loc- + grep("3\\.", doc.parts[-(1:Methods.loc)])[1]
Methods.text <- paste(doc.parts[Methods.loc:(Results.loc-1)], collapse=" ")
# Similarly with other parts. For example for Acknowledgements section:
Ack.loc <- grep("Acknowledgements", doc.parts)[1]
Ref.loc <- grep("References", doc.parts)[1]
Ack.text <- paste(doc.parts[Ack.loc:(Ref.loc-1)], collapse=" ")
Ack.text
# [1] "6. Acknowledgements We are especially grateful to the study participants.
# This study was supported by a grant from the Tunisian Ministry of Health and
# Ministry of Higher Education ...
5条答案
按热度按时间bqucvtff1#
你可以尝试使用readtext库:
变量x只包含没有任何格式的文本,所以如果你需要提取一些信息,你需要执行字符串搜索。例如,对于你在评论中提到的文档,一种方法可以如下所示:
确切的方法取决于你需要搜索的所有文档的共同结构。例如,如果第一部分总是命名为“Background”,你可以使用这个词进行搜索。但是,如果有时候是“Background”,有时候是“Introduction”,那么你可能需要搜索“1.”模式。
ebdffaop2#
你应该会发现其中一个包会为你做的伎俩。
在一天结束时,现代Office文件格式(OpenXML)只是包含结构化XML内容的 *.zip文件,因此如果您有结构化良好的内容,那么您可能只想以这种方式打开它。我将从这里开始(http://officeopenxml.com/anatomyofOOXML.php),您也应该能够选择OpenXML SDK作为指导(https://msdn.microsoft.com/en-us/library/office/bb448854.aspx)。
jdgnovmf3#
Pandoc是处理此类任务的一个很好的解决方案。
然后,您可以使用R中的regex工具从新创建的
a.md
中提取所需的内容,即文本。默认情况下,图像不会被转换。顺便说一下,Pandoc是RStudio的一部分,所以你可能已经有了。
klsxnrf14#
你可以用
officer
包来实现:yb3bgrhw5#
以下是可以考虑的另一种方法: