2016-11-03 6 views
2

Rを使用してWikipedia Dumpsから〜5.8GB .xmlを読み込み、処理しようとしています。RAMがあまりないので、チャンクで処理したいと思います。このように、xmlを読み込んで解析するR

ファイルは、各Wikipediaのページに1つのxml要素をcontais(現在は完全に私のパソコンxml2::read_xmlブロックを使用した場合):

<page> 
    <title>AccessibleComputing</title> 
    <ns>0</ns> 
    <id>10</id> 
    <redirect title="Computer accessibility" /> 
    <revision> 
     <id>631144794</id> 
     <parentid>381202555</parentid> 
     <timestamp>2014-10-26T04:50:23Z</timestamp> 
     <contributor> 
     <username>Paine Ellsworth</username> 
     <id>9092818</id> 
     </contributor> 
     <comment>add [[WP:RCAT|rcat]]s</comment> 
     <model>wikitext</model> 
     <format>text/x-wiki</format> 
     <text xml:space="preserve">#REDIRECT [[Computer accessibility]] 

{{Redr|move|from CamelCase|up}}</text> 
     <sha1>4ro7vvppa5kmm0o1egfjztzcwd0vabw</sha1> 
    </revision> 
</page> 

ファイルのサンプルからhere

を見つけることができます私のパースペクティブでは、ファイル内のページごとのページのようなチャンクで読むことが可能だと思います。処理された各page要素を.csvファイルの行として保存します。

次の列を持つdata.frameが必要です。

id、title、およびtext。

この.xmlをチャンクで読むにはどうすればよいですか?

+0

あなたの問題を解決できるかどうかはわかりません。あなたが私たちに提供したサンプルは小さいので、私はあなたの問題を本当に再現することはできません。あなたは[this](http://stackoverflow.com/questions/21222113/how-to-read-first-1000-lines-of-csv-file-into-r)(jlhoward answer)のようなものを試しましたか? –

+0

質問にあるような多くの、多くの要素を持つ '.xml'を想像してください。 xml構造体が壊れてしまったので、行単位で読むことができません。要素ごとに読みたいですが、これを行う方法がわかりません...明らかに小さなサンプルにリンクしましたが、ここでフルファイルをダウンロードできます:https://dumps.wikimedia.org/ptwiki/ 20161101/ptwiki-20161101-pages-articles.xml.bz2です –

答えて

1

これは改善することができますが、主なイデアがここにあります。あなたはまだあなたがreadLines()関数内の各相互作用に読みに行くラインの量を定義するための最良の方法と、各チャンクを読み込むためのメソッドを定義する必要がありますが、チャンクを取得するための解決策はここにある:

xml <- readLines("ptwiki-20161101-pages-articles.xml", n = 2000) 

inicio <- grep(pattern = "<page>", x = xml) 
fim <- grep(pattern = "</page>", x = xml) 
if (length(inicio) > length(fim)) { # if you get more beginnings then ends 
    inicio <- inicio[-length(inicio)] # drop the last one 
} 

chunks <- vector("list", length(inicio)) 

for (i in seq_along(chunks)) { 
    chunks[[i]] <- xml[inicio[i]:fim[i]] 
} 

chunks <- sapply(chunks, paste, collapse = " ") 

私はread_xml(chunks[1]) %>% xml_nodes("text") %>% xml_text()を試してみました。

関連する問題