如何在R Studio中将xml节点和键值提取到data.frame中,包括NA值?

时间:2019-02-07 13:12:40

标签: python r xml extract

数据样本包含单词(正交)和类别(prop key =“ sense:ukb:unitsstr”)。我想提取成对的数据,例如orth和prop key =“ sense:ukb:unitsstr作为数据帧的行。但是,有些单词可能没有任何prop数据,就像最后两个记录一样。 然后我想将它们视为NA。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chunkList SYSTEM "ccl.dtd">
<chunkList>
 <chunk id="ch1" type="p">
  <sentence id="s1">
   <tok>
    <orth>ktoś</orth>
    <lex disamb="1"><base>ktoś</base><ctag>subst:sg:nom:m1</ctag></lex>
    <prop key="polarity">0</prop>
    <prop key="sense:ukb:syns_id">11511</prop>
    <prop key="sense:ukb:syns_rank">11511/128.6156573170 243094/95.1234745165</prop>
    <prop key="sense:ukb:unitsstr">ktoś.2(15:os)</prop>
   </tok>
   <tok>
    <orth>go</orth>
    <lex disamb="1"><base>go</base><ctag>subst:sg:nom:n</ctag></lex>
    <prop key="polarity">0</prop>
    <prop key="sense:ukb:syns_id">47620</prop>
    <prop key="sense:ukb:syns_rank">47620/108.9010709884 234524/90.4766173102</prop>
    <prop key="sense:ukb:unitsstr">go.1(2:czy)</prop>
   </tok>
   <tok>
    <orth>krokodyl</orth>
    <lex disamb="1"><base>krokodyl</base><ctag>subst:sg:nom:m2</ctag></lex>
    <prop key="polarity">0</prop>
    <prop key="sense:ukb:syns_id">12879</prop>
    <prop key="sense:ukb:syns_rank">12879/40.5162836207 254796/35.9915058408 7063215/33.3657479890 7063214/26.6770712118 7063217/25.5775738130 7063213/23.6851347572 7063212/23.6300037076</prop>
    <prop key="sense:ukb:unitsstr">krokodyl.1(21:zw) krokodyl_właściwy.1(21:zw)</prop>
   </tok>
   <tok>
    <orth>się</orth>
    <lex disamb="1"><base>się</base><ctag>qub</ctag></lex>
   </tok>
   <tok>
    <orth>ja</orth>
    <lex disamb="1"><base>ja</base><ctag>ppron12:sg:nom:m1:pri</ctag></lex>
   </tok>

我以为我可以使用一些xml路径行来获取它,但是我被卡住了:

doc = xmlTreeParse("statsUCZESTxfreqkeyword xml.txt",useInternal = TRUE)
top = xmlRoot(doc)
xmlName(top)
names(top) 
names( top[[ 1 ]] )
sent <- top[[ 1 ]] [[ "sentence" ]]
names(sent)
names(sent[[1]])
xmlSApply(sent[[1]], xmlValue)
xmlSApply(sent, function(x) xmlSApply(x, xmlValue))
nodes = getNodeSet(top, "//prop[@key='sense:ukb:unitsstr']")
lapply(nodes, function(x) xmlSApply(x, xmlValue)) # 152 words have prop
xmlSApply(sent, function(x) xmlSApply(x, xmlValue))

1 个答案:

答案 0 :(得分:0)

这是使用xml2库的解决方案。我发现xml2的语法比xml库更容易。两者都有优点和缺点。
逻辑与我在此处提供的答案类似:rvest: Return NAs for empty nodes given multiple listings。该代码的注释解释了每个步骤。 xmltext下面的代码中是您要处理的xml文本或xml的文件名。

library(xml2)

#read the xml page
page<-read_xml(xmltext)
#find the listing nodes and id of each node
listings<-xml_find_all(page, ".//tok")

#find the text associated witht the ortho nodes
orthotext<-sapply(listings, function(x){xml_text(xml_find_first(x, ".//orth"))})

#find text associated with the prop key="sense:ukb:unitsstr"
ukb<-sapply(listings, function(x){ nodes<-xml_find_all(x, ".//prop")
                            #find node with wanted key
                           wantednode<-nodes[xml_attr(nodes, "key" )=="sense:ukb:unitsstr"]
                           #extract text
                           wantednode<-xml_text(wantednode)
                           #return NA if node is empty.
                           ifelse(is.character(wantednode), wantednode, NA)
})


#create dataframe
finalanswer<-data.frame(orthotext, ukb)