Python 读取.xml数据集

问题

英-中翻译模型需要英文和中文的对照文本。文件类型为.text格式,要求两个文件的每一行是严格对应的。寻找到的文件为.xml格式。需要将英文的句子和中文的句子分别从.xml文件中提取出来。


数据文件:

.xml文件中的数据如下(test文件部分):

<?xml version="1.0" encoding="utf-8"?>
<letsmt>

<s id="1">
 <chunk type="NP" id="c-1">
  <w id="w1.1" pos="NN" tree="NN" hun="NN">http</w>
 </chunk>
 <w hun=":" pos=":" lem=":" id="w1.2" tree=":">:</w>
 <chunk type="NP" id="c-3">
  <w hun="CD" tree="SYM" id="w1.3" pos="JJ" lem="/">/</w>
  <w tree="SYM" pos="NN" id="w1.4" lem="/" hun="CD">/</w>
  <w tree="NN" id="w1.5" pos="NN" hun="NN">www.ted.com</w>
  <w pos="NN" lem="/" id="w1.6" tree="SYM" hun="NN">/</w>
  <w tree="NNS" pos="NNS" id="w1.7" lem="talk" hun="NNS">talks</w>
 </chunk>
 <chunk type="VP" id="c-4">
  <w tree="SYM" pos="VBP" id="w1.8" lem="/" hun="JJ">/</w>
  <w pos="VBN" id="w1.9" tree="NN" hun="NN">stephen</w>
 </chunk>
 <chunk type="NP" id="c-5">
  <w hun="NNS" tree="SYM" id="w1.10" pos="JJ" lem="_">_</w>
  <w id="w1.11" pos="NN" tree="NP" hun="VBP">palumbi</w>
  <w tree="SYM" lem="_" id="w1.12" pos="NN" hun="VBN">_</w>
  <w hun="VBG" tree="NN" lem="following" id="w1.13" pos="NN">following</w>
 </chunk>
 <chunk id="c-6" type="VP">
  <w tree="SYM" lem="_" pos="VBD" id="w1.14" hun="VBG">_</w>
 </chunk>
 <chunk type="NP" id="c-7">
  <w hun="DT" lem="the" pos="DT" id="w1.15" tree="NP">the</w>
  <w lem="_" pos="JJ" id="w1.16" tree="SYM" hun="JJ">_</w>
  <w lem="mercury" pos="NN" id="w1.17" tree="NN" hun="NN">mercury</w>
  <w tree="SYM" lem="_" pos="NN" id="w1.18" hun="NN">_</w>
  <w hun="NN" pos="NN" id="w1.19" tree="NN">trail.html</w>
 </chunk>
</s>





<s id="2">
 <chunk id="c-1" type="NP">
  <w lem="there" id="w2.1" pos="EX" tree="EX" hun="EX">There</w>
 </chunk>
 <chunk id="c-2" type="VP">
  <w hun="VBZ" pos="VBZ" lem="be" id="w2.2" tree="VBZ">'s</w>
 </chunk>
 <chunk id="c-3" type="NP">
  <w pos="DT" id="w2.3" lem="a" tree="DT" hun="DT">a</w>
  <w hun="JJ" lem="tight" pos="JJ" id="w2.4" tree="JJ">tight</w>
  <w hun="CC" pos="CC" id="w2.5" lem="and" tree="CC">and</w>
  <w lem="surprising" pos="JJ" id="w2.6" tree="JJ" hun="JJ">surprising</w>
  <w hun="NN" tree="NN" id="w2.7" lem="link" pos="NN">link</w>
 </chunk>
 <chunk type="PP" id="c-4">
  <w hun="IN" tree="IN" pos="IN" id="w2.8" lem="between">between</w>
 </chunk>
 <chunk id="c-5" type="NP">
  <w lem="the" id="w2.9" pos="DT" tree="DT" hun="DT">the</w>
  <w tree="NN" id="w2.10" pos="NN" lem="ocean" hun="NN">ocean</w>
 </chunk>
 <chunk id="c-6" type="NP">
  <w tree="POS" id="w2.11" pos="POS" lem="'s" hun="POS">'s</w>
  <w lem="health" id="w2.12" pos="NN" tree="NN" hun="NN">health</w>
  <w hun="CC" lem="and" id="w2.13" pos="CC" tree="CC">and</w>
  <w hun="JJ" lem="ours" id="w2.14" pos="NNS" tree="JJ">ours</w>
 </chunk>
 <w hun="," tree="," lem="," id="w2.15" pos=",">,</w>
 <chunk type="VP" id="c-8">
  <w hun="VBZ" tree="VVZ" pos="VBZ" lem="say" id="w2.16">says</w>
 </chunk>
 <chunk type="NP" id="c-9">
  <w tree="JJ" id="w2.17" pos="NNP" lem="marine" hun="JJ">marine</w>
  <w hun="NN" id="w2.18" pos="NNP" lem="biologist" tree="NN">biologist</w>
  <w pos="NNP" id="w2.19" lem="Stephen" tree="NP" hun="NNP">Stephen</w>
  <w hun="NNP" id="w2.20" pos="NNP" tree="NP">Palumbi</w>
 </chunk>
 <w pos="VBD" id="w2.21" lem="." tree="SENT" hun=".">.</w>
 <chunk id="c-11" type="NP">
  <w tree="PP" pos="PRP" id="w2.22" lem="he" hun="PRP">He</w>
 </chunk>
 <chunk type="VP" id="c-12">
  <w hun="VBZ" lem="show" pos="VBZ" id="w2.23" tree="VVZ">shows</w>
 </chunk>
 <chunk type="ADVP" id="c-13">
  <w pos="WRB" id="w2.24" lem="how" tree="WRB" hun="WRB">how</w>
 </chunk>
 <chunk id="c-14" type="NP">
  <w tree="NNS" lem="toxin" id="w2.25" pos="NNS" hun="NNS">toxins</w>
 </chunk>
 <chunk id="c-15" type="PP">
  <w lem="at" pos="IN" id="w2.26" tree="IN" hun="IN">at</w>
 </chunk>
 <chunk id="c-16" type="NP">
  <w pos="DT" lem="the" id="w2.27" tree="DT" hun="DT">the</w>
  <w tree="NN" id="w2.28" pos="NN" lem="bottom" hun="NN">bottom</w>
 </chunk>
 <chunk id="c-17" type="PP">
  <w id="w2.29" pos="IN" lem="of" tree="IN" hun="IN">of</w>
 </chunk>
 <chunk type="NP" id="c-18">
  <w hun="DT" id="w2.30" lem="the" pos="DT" tree="DT">the</w>
  <w hun="NN" tree="NN" id="w2.31" pos="JJ" lem="ocean">ocean</w>
  <w tree="NN" pos="NN" id="w2.32" lem="food" hun="NN">food</w>
  <w id="w2.33" pos="NNS" lem="chain" tree="NN" hun="NN">chain</w>
 </chunk>
 <chunk id="c-19" type="VP">
  <w tree="VVP" lem="find" id="w2.34" pos="VBP" hun="VB">find</w>
 </chunk>
 <chunk type="NP" id="c-20">
  <w hun="PRP$" tree="PP$" id="w2.35" lem="their" pos="PRP$">their</w>
  <w id="w2.36" pos="NN" lem="way" tree="NN" hun="NN">way</w>
 </chunk>
 <chunk type="PP" id="c-21">
  <w tree="IN" pos="IN" id="w2.37" lem="into" hun="IN">into</w>
 </chunk>
 <chunk id="c-22" type="NP">
  <w tree="PP$" lem="our" id="w2.38" pos="PRP$" hun="PRP$">our</w>
  <w lem="body" pos="NNS" id="w2.39" tree="NNS" hun="NNS">bodies</w>
 </chunk>
 <w hun="," pos="," lem="," id="w2.40" tree=",">,</w>
 <chunk type="PP" id="c-24">
  <w hun="IN" pos="IN" lem="with" id="w2.41" tree="IN">with</w>
 </chunk>
 <chunk type="NP" id="c-25">
  <w hun="DT" tree="DT" id="w2.42" lem="a" pos="DT">a</w>
  <w tree="JJ" id="w2.43" pos="JJ" lem="shocking" hun="JJ">shocking</w>
  <w hun="NN" tree="NN" pos="NN" lem="story" id="w2.44">story</w>
 </chunk>
 <chunk type="PP" id="c-26">
  <w hun="IN" tree="IN" lem="of" pos="IN" id="w2.45">of</w>
 </chunk>
 <chunk type="NP" id="c-27">
  <w pos="JJ" id="w2.46" lem="toxic" tree="JJ" hun="JJ">toxic</w>
  <w tree="NN" lem="contamination" pos="NN" id="w2.47" hun="NN">contamination</w>
 </chunk>
 <chunk id="c-28" type="PP">
  <w hun="IN" id="w2.48" pos="IN" lem="from" tree="IN">from</w>
 </chunk>
 <chunk type="NP" id="c-29">
  <w tree="DT" pos="DT" id="w2.49" lem="a" hun="DT">a</w>
  <w hun="JJ" tree="JJ" pos="JJ" id="w2.50" lem="Japanese">Japanese</w>
  <w hun="NN" id="w2.51" lem="fish" pos="NN" tree="NN">fish</w>
  <w hun="NN" pos="NN" id="w2.52" lem="market" tree="NN">market</w>
 </chunk>
 <w hun="." tree="SENT" id="w2.53" lem="." pos="VBD">.</w>
 <chunk type="NP" id="c-31">
  <w hun="PRP$" tree="PP$" id="w2.54" pos="PRP$" lem="his">His</w>
  <w hun="NN" pos="NN" lem="work" id="w2.55" tree="NN">work</w>
 </chunk>
 <chunk type="VP" id="c-32">
  <w pos="VBZ" lem="point" id="w2.56" tree="VVZ" hun="NNS">points</w>
 </chunk>
 <chunk type="NP" id="c-33">
  <w pos="DT" id="w2.57" lem="a" tree="DT" hun="DT">a</w>
  <w tree="NN" id="w2.58" pos="NN" lem="way" hun="NN">way</w>
 </chunk>
 <chunk type="ADVP" id="c-34">
  <w tree="RB" lem="forward" pos="RB" id="w2.59" hun="RB">forward</w>
 </chunk>
 <chunk id="c-35" type="PP">
  <w hun="IN" tree="IN" id="w2.60" pos="IN" lem="for">for</w>
 </chunk>
 <chunk id="c-36" type="VP">
  <w lem="save" pos="VBG" id="w2.61" tree="VVG" hun="VBG">saving</w>
 </chunk>
 <chunk type="NP" id="c-37">
  <w tree="DT" id="w2.62" pos="DT" lem="the" hun="DT">the</w>
  <w tree="NNS" lem="ocean" id="w2.63" pos="NNS" hun="NNS">oceans</w>
 </chunk>
 <chunk id="c-38" type="NP">
  <w pos="POS" id="w2.64" lem="'" tree="POS" hun="POS">'</w>
  <w tree="NN" lem="health" id="w2.65" pos="NN" hun="NN">health</w>
 </chunk>
 <w id="w2.66" pos=":" lem="--" tree=":" hun=":">--</w>
 <w tree="CC" id="w2.67" lem="and" pos="CC" hun="CC">and</w>
 <chunk type="NP" id="c-41">
  <w pos="NN" lem="humanity" id="w2.68" tree="NN" hun="NN">humanity</w>
 </chunk>
 <chunk id="c-42" type="VP">
  <w lem="'s" pos="VBZ" id="w2.69" tree="POS" hun="POS">'s</w>
 </chunk>
 <w hun="." id="w2.70" pos="." lem="." tree="SENT">.</w>
</s>


数据集的特点:

每一个s标签(tag)包含一句话。字符在节点(chunk)下的子节点(w)中,标点在与节点(chunk)平行的(w)节点中。

资料查找:

http://www.cnblogs.com/fnng/p/3581433.html

说的很详细,但是实际的文档中(w)有一部分在(chunk)中,有一部分在(s)中。介绍的方法不能很好的适用


http://www.cnblogs.com/CheeseZH/p/4026686.html

提到了一个很重要的函数:

tree = ET.parse('country_data.xml')

root = tree.getroot()
.getroot()函数直接将相对于根目录下的所有子节点存放到了一个list中。很有效,有些博客上面没有介绍这个函数。

>>> for child in root:
用于遍历子节点。


完成的代码如下:

import xml.etree.ElementTree as ET

tree = ET.parse('20090831_en.xml')
root = tree.getroot()
# print("%d" % len(root))  # 进入了根目录s.
# print("%s" % root[0].tag)
# print("%s" % root[0].attrib)

with open("D:/pythonex/rnn/chatbot-en-ch/firsttime.xml", "wb") as new_file:
    for i in range(len(root)):
        # 进入根下的chunk
        sentence = ''
        for child in root[i]:
            # print("%s---%s" % (child.tag, child.attrib))
            if(child.tag == 'chunk'):
                for child in child:   # 进入了最深的层次
                    # print("%s" % child.tag)
                    if(child.tag == 'w'):
                        # print("%s" % child.text)
                        if(sentence == ''):
                            sentence = sentence + child.text
                        else:
                            sentence = sentence + ' ' + child.text
            elif(child.tag == 'w'):
                # print("It's the comon time")
                sentence = sentence + ' ' + child.text
        sentence = sentence + '\r\n'
        print("%s" % sentence)
        sentence_bytes = sentence.encode('utf-8')
        print("%s" % sentence_bytes)
        new_file.write(sentence_bytes)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值