在R中为NLP解析XML文件,但代码返回空
我试图预处理大型TEI编码的XML文件,以便以后使用ggplot2可视化结果。我正在使用Matt Jockers的代码,这一切似乎都在运行。但是我没有返回数据,而是得到一个包含6个空值的列表(6是文件数)。为什么我会得到空名单 这里的代码仅供参考,但文件访问受到限制在R中为NLP解析XML文件,但代码返回空,r,xml,nlp,R,Xml,Nlp,我试图预处理大型TEI编码的XML文件,以便以后使用ggplot2可视化结果。我正在使用Matt Jockers的代码,这一切似乎都在运行。但是我没有返回数据,而是得到一个包含6个空值的列表(6是文件数)。为什么我会得到空名单 这里的代码仅供参考,但文件访问受到限制 setwd("C:/Users/Amanda/Desktop/WWO/WWO") input.dir <- "Wroth and Sidney Herbert/R corpus" files.v <- dir(path
setwd("C:/Users/Amanda/Desktop/WWO/WWO")
input.dir <- "Wroth and Sidney Herbert/R corpus"
files.v <- dir(path=input.dir, pattern=".*xml")
file.path(input.dir, files.v)
getTEIWordTableList <- function(doc.object){
paras <- getNodeSet(doc.object,
"/tei:TEI/tei:text/tei:body//tei:p",
c(tei = "http://www.tei-c.org/ns/1.0"))
words <- paste(sapply(paras,xmlValue), collapse=" ")
words.lower <- tolower(words)
words.l <- strsplit(words.lower, "\\W")
word.v <- unlist(words.l)
book.freqs.t <- table(word.v[which(word.v!="")])
book.freqs.rel.t <- 100*(book.freqs.t/sum(book.freqs.t))
return(book.freqs.rel.t)}
book.freqs.l <- list()
for(i in 1:length(files.v)){
doc.object <- xmlTreeParse(file.path(input.dir, files.v[i]),
useInternalNodes=TRUE)
worddata <- getTEIWordTableList(doc.object)
book.freqs.l[[files.v[i]]] <- worddata}
setwd(“C:/Users/Amanda/Desktop/WWO/WWO”)
input.dir一段XML将有助于我们帮助您。欢迎使用Stackoverflow。您的代码似乎在搜索特定的XPath表达式。能否确保TEI文件的结构为TEI/body//p?这意味着根元素是TEI,后跟body,body上面有一个名为paddedxml的子元素;Yannis P.,我将尝试整理我的XPath。
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="../schema/wwp-store.rng" type="application/xml"schematypens="http://relaxng.org/ns/structure/1.0"?><?xml-model href="../schema/wwp-store.isosch" type="application/xml"schematypens="http://purl.oclc.org/dsdl/schematron"?><!-- $Id: sidney.antonie.xml 29592 2016-09-16 13:49:08Zsyd $ --><TEI xmlns="http://www.wwp.northeastern.edu/ns/textbase"xmlns:xi="http://www.w3.org/2001/XInclude">
<teiHeader xml:id="TR00204.hdr">
<fileDesc>
<titleStmt>
<title type="main">The Tragedie of Antony, 1595</title>
<author><persName ref="p:msidney.enw" type="person-female">Sidney, Mary, Countess of Pembroke</persName>, trans. (from <persName ref="p:rgarnier.pbo" type="person-male">Robert Garnier</persName>)</author>
<sponsor>Brown University</sponsor>
<sponsor>Northeastern University</sponsor>
<funder>U.S. National Endowment for the Humanities</funder>