【问题标题】:how to extract plain text from .docx file using R如何使用R从.docx文件中提取纯文本
【发布时间】:2018-05-22 10:57:39
【问题描述】:

任何人都知道他们可以推荐的任何东西,以便从 .docx 格式的文章中提取纯文本(最好使用 R)?

速度并不重要,我们甚至可以使用具有一些 API 的网站来上传和提取文件,但我一直找不到。我需要提取介绍、方法、结果和结论 我要删除摘要、参考文献,特别是图形和表格 谢谢

【问题讨论】:

    标签: r docx extraction


    【解决方案1】:

    您可以尝试使用 readtext 库:

    library(readtext)
    x <- readtext("/path/to/file/myfile.docx")
    # x$text will contain the plain text in the file
    

    变量 x 只包含没有任何格式的文本,因此如果您需要提取一些信息,您需要执行字符串搜索。例如,对于您在评论中提到的文档,一种方法可能如下:

    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 ...
    

    确切的方法取决于您需要搜索的所有文档的通用结构。例如,如果第一部分始终命名为“背景”,您可以使用该词进行搜索。但是,如果这有时可能是“背景”,有时可能是“介绍”,那么您可能需要搜索“1”。模式。

    【讨论】:

    • 非常感谢您的帮助,这正是我正在寻找的。如果你不重要,我还有一个问题。是否有解决方案来提取表格和图形(包括标题),因为该算法在这种情况下会出现一些问题
    • @AzzaAbidi 这些表格和图表有什么一致的吗?例如,它们是否总是像您给我的文件中的参考文献一样位于论文的末尾?
    • 代码在开始时运行良好,但在提取 2 个 wordparsed 之间的文本时运行未出错 > abstract.text
    【解决方案2】:

    您应该会发现其中一个软件包可以为您解决问题。

    归根结底,现代 Office 文件格式 (OpenXML) 只是包含结构化 XML 内容的 *.zip 文件,因此如果您有结构良好的内容,那么您可能只想以这种方式打开它。我将从这里开始 (http://officeopenxml.com/anatomyofOOXML.php),您也应该能够取消选择 OpenXML SDK 以获得指导 (https://msdn.microsoft.com/en-us/library/office/bb448854.aspx)

    【讨论】:

    • 嗯,它们可能是“简单”的 XML,但文本通常隐藏在大量可能人类无法阅读的标签之间。
    • 我正在处理的 docx 文件已经从 pdf 转换,它们的 xml 结构不正确
    【解决方案3】:

    Pandoc 是此类任务的绝佳解决方案。使用名为 a.docx 的文档,您将在命令行中运行

    pandoc -f docx -t markdown -o a.md a.docx
    

    然后您可以使用 R 中的正则表达式工具从新创建的 a.md(文本)中提取您需要的内容。默认情况下,不转换图像。

    顺便说一下,Pandoc 是 RStudio 的一部分,所以你可能已经拥有它。

    【讨论】:

      【解决方案4】:

      您可以使用包officer

      library(officer)
      example_pptx <- system.file(package = "officer", "doc_examples/example.docx")
      doc <- read_docx(example_pptx)
      summary_paragraphs <- docx_summary(doc)
      summary_paragraphs[summary_paragraphs$content_type %in% "paragraph", "text"]
      #>  [1] "Title 1"                                                                
      #>  [2] "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "              
      #>  [3] "Title 2"                                                                
      #>  [4] "Quisque tristique "                                                     
      #>  [5] "Augue nisi, et convallis "                                              
      #>  [6] "Sapien mollis nec. "                                                    
      #>  [7] "Sub title 1"                                                            
      #>  [8] "Quisque tristique "                                                     
      #>  [9] "Augue nisi, et convallis "                                              
      #> [10] "Sapien mollis nec. "                                                    
      #> [11] ""                                                                       
      #> [12] "Phasellus nec nunc vitae nulla interdum volutpat eu ac massa. "         
      #> [13] "Sub title 2"                                                            
      #> [14] "Morbi rhoncus sapien sit amet leo eleifend, vel fermentum nisi mattis. "
      #> [15] ""                                                                       
      #> [16] ""                                                                       
      #> [17] ""
      

      【讨论】:

      • 很酷,谢谢你的帮助。在我的情况下,处理科学文章数据集,我需要提取介绍、方法和结果,但没有摘要和参考)你能帮我吗?这样做的一段代码
      • @AzzaAbidi 您有包含此信息的示例文档吗?
      • @Katia 我有一个包含 400 篇 docx 格式的科学文章的数据集,结构类似这个标题
        作者
        摘要
        介绍
        方法
        结果
        结论
        参考文献
      • @Katia 有一个sample
      • @AzzaAbidi 我添加了一些尝试来处理您提供的文件。正如我在示例中提到的那样,确切的算法将取决于您需要通过的所有文档的通用结构。
      猜你喜欢
      • 2011-08-06
      • 2014-10-03
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 2018-08-13
      • 1970-01-01
      相关资源
      最近更新 更多