gpt4 book ai didi

r - 如何使用 tm 创建 *.docx 文件语料库?

转载 作者:行者123 更新时间:2023-12-05 02:24:08 25 4
gpt4 key购买 nike

我有一个混合文件类型的 MS Word 文档集。有些文件是 *.doc,有些是 *.docx。我正在学习使用 tm 并且我已经(或多或少*)成功地创建了一个由 *.doc 文件组成的语料库:

ex_eng <- Corpus(DirSource('~/R/expertise/corpus/english'), 
readerControl=list(reader=readDOC,
language='en_CA',
load=TRUE));

此命令不处理 *.docx 文件。我假设我需要一个不同的读者。来自 this article , 我知道我可以自己写(如果我对 .docx 格式有很好的理解,我目前没有)。

readDOC 阅读器使用 antiword解析 *.doc 文件。是否有类似的应用程序可以解析 *.docx 文件?

或者更好的是,是否已经有一种使用 tm 创建 *.docx 文件语料库的标准方法?


* 或多或少,因为虽然文件进入并且可读,但我对每个文档都收到此警告:In readLines(y, encoding = x$Encoding) : incomplete final line found on 'path/to/a/file.doc'

最佳答案

.docx 文件是压缩的 XML 文件。如果你执行这个:

> uzfil <- unzip(file.choose())

然后在你的目录中选择一个.docx文件,你会得到:

> str(uzfil)
chr [1:13] "./[Content_Types].xml" "./_rels/.rels" "./word/_rels/document.xml.rels" ...
> uzfil
[1] "./[Content_Types].xml" "./_rels/.rels" "./word/_rels/document.xml.rels"
[4] "./word/document.xml" "./word/theme/theme1.xml" "./docProps/thumbnail.jpeg"
[7] "./word/settings.xml" "./word/webSettings.xml" "./word/styles.xml"
[10] "./docProps/core.xml" "./word/numbering.xml" "./word/fontTable.xml"
[13] "./docProps/app.xml"

这也会默默地将所有这些文件解压缩到您的工作目录中。 "./word/document.xml" 文件包含您要查找的单词,因此您可以使用 XML 包中的一种 XML 工具来读取它们。我猜你会按照以下方式做一些事情:

 library(XML)
xtext <- xmlTreeParse(unz(uzfil[4]), useInternalNodes = TRUE) )

实际上,您可能需要将其保存到临时目录并将该路径添加到文件名“./word/document.xml”。

您可能希望使用@GaborGrothendieck 在此答案中提供的进一步步骤:How to extract xml data from a CrossRef using R?

关于r - 如何使用 tm 创建 *.docx 文件语料库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16065952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com