使用xml2从TEI XML创建数据框

时间:2015-06-05 10:57:23

标签: xml r dataframe tei xml2

我正在尝试使用Hadley Wickham的xml2软件包创建一个TEI-XML版Moby Dick的数据框。我希望数据框最终看起来像这样(对于小说中的所有单词):

df <- data.frame(
chapter = c("1", "1", "1"),
words = c("call", "me", "ishmael"))

我能够得到碎片,但不是整件事。这是我到目前为止所得到的:

library("xml2")

# Read file
melville <- read_xml("data/melville.xml")

# Get chapter divs (remember, doesn't include epilogue)
chap_frames <- xml_find_all(melville, "//d1:div1[@type='chapter']", xml_ns(melville))

这给了我们一个长度为134的列表(即每个章节)。我们可以得到特定元素的章节编号如下:

xml_attr(chap_frames[[1]], "n")

我们可以得到一个特定章节的段落(即减去章节标题)如下:

words <- xml_find_all(chap_frames[[1]], ".//d1:p", xml_ns(melville)) %>%  # remember doesn't include epilogue
xml_text()

我们可以得到以下章节的内容:

# Split words function
split_words <- function (ll) {
  result <- unlist(strsplit(ll, "\\W+"))
  result <- result[result != ""]
  tolower(result)
}

# Apply function
words <- split_words(words)

我无法弄清楚如何获得每个单词的章节编号。我有一个有用的玩具示例:

mini <- read_xml(
'
<div1 type="chapter" n="1" id="_75784">
<head>Loomings</head>
    <p rend="fiction">Call me Ishmael.</p>
    <p rend="fiction">There now is your insular city of the Manhattoes, belted round by wharves as Indian isles by coral reefs- commerce surrounds it with her surf.</p> 
</div1>
')

# Function
process_chap <- function(div){
chapter <- xml_attr(div, "n")
words <- xml_find_all(div, "//p") %>%
    xml_text()
data.frame(chapter = chapter,
           word = split_words(words))
}

process_chap(mini)

但它不适用于较长的例子

 process_chap2 <- function(div){
 chapter <- xml_attr(div, "n")
 words <- xml_find_all(div, ".//d1:p", xml_ns(melville)) %>%  # remember doesn't include epilogue
 xml_text()
 data.frame(chapter = chapter,
           word = split_words(words))
}

# Fails because there are more words than chapter names
df <- process_chap2(chap_frames)

# Gives all the words p (not chapters), chapter numbers are `NULL`. 
df2 <- process_chap2(melville)

(我知道为什么玩具示例有效,但Melville没有,但我想把它包括在内以显示我想要做的事情)。我猜我可能需要某种循环,但我不知道从哪里开始。有什么建议吗?

PS:我不完全确定我是否应该链接到我在Github上找到的Moby Dick的xml版本,但你可以很容易地找到melville1.xml

1 个答案:

答案 0 :(得分:2)

方法是一次一个地绘制每个章节的数据。然后将一章的单词和章节编号组合成一个数据框。 R将根据需要经常重复章节号的单个值:

ERROR:  syntax error at or near "+"
LINE 4:     INITCOND = max + 10

在整齐的数据框中收集了所有章节的信息后,您可以使用words <- letters[1:3] n <- 1 df <- data.frame(words, n) df ## words n ## 1 a 1 ## 2 b 1 ## 3 c 1 将整体合并到一个数据框中。

对于数据的前两章,这可能是这样的......

rbind()

要为所有章节提高效率,您可以构建一个循环,重复所有章节的步骤,或者您可以考虑执行提取的函数,将其应用于所有章节,然后通过{{1}组合数据以后。

......我可能会这样做:

library(xml2)
library(dplyr)
library(stringr)


# Read file
url <- "https://raw.githubusercontent.com/reganna/TextAnalysisWithR/master/data/XML1/melville1.xml"
melville <- read_xml(url)


# get chapter frame and number
chap_frames <- xml_find_all(melville, "//d1:div1[@type='chapter']", xml_ns(melville))
chap_n <- xml_attr(chap_frames, "n")


# get the date for first chapter
words1 <- 
  xml_find_all(chap_frames[[1]], ".//d1:p", xml_ns(melville))  %>% 
    xml_text() %>% 
    unlist() %>% 
    str_split("\\W+") %>% 
    unlist()  %>% 
    tolower()

n1 <- xml_attr(chap_frames[[1]], "n")


# get the data for the second chapter
words2 <- 
  xml_find_all(chap_frames[[2]], ".//d1:p", xml_ns(melville))  %>% 
  xml_text() %>% 
  unlist() %>% 
  str_split("\\W+") %>% 
  unlist()  %>% 
  tolower()

n2 <- xml_attr(chap_frames[[2]], "n")


# put it together
df <- 
  rbind(
    data_frame(words=words1, chapter=n1),
    data_frame(words=words2, chapter=n2)
  )
df


## Source: local data frame [3,719 x 2]
## 
##      words chapter
## 1     call       1
## 2       me       1
## 3  ishmael       1
## 4     some       1
## 5    years       1
## 6      ago       1
## 7    never       1
## 8     mind       1
## 9      how       1
## 10    long       1
## ..     ...     ...