如何从带有MMAX2注释的XML语料库访问数据

时间:2018-09-04 15:49:43

标签: python xml python-3.x pandas nlp

我有一个带注释的语料库用于“共指解析”任务。您能让我知道如何从xml文件中提取数据。我做了以下工作,但是没用。

from lxml import objectify
import pandas as pd

    xml = objectify.parse(open('Dari_Coref_2_coref_level.xml'))
    root = xml.getroot()

    df = pd.DataFrame(columns='markable')

    for i in range(0, 2):
        obj = root.getchildren()[i].getchildren()
        row = dict(zip(['markable'], [obj[0].text]))
        row_s = pd.Series(row)
        row_s.name = i
        df = df.append(row_s)

 print(df)

我的xml文件的结构如下:

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE markables SYSTEM "markables.dtd">
<markables xmlns="www.eml.org/NameSpaces/coref">
<markable id="markable_1" span="word_1..word_4" mentiontype="ne"  
coref_class="set_1"  mmax_level="coref"  coreftype="ident" />
<markable id="markable_3" span="word_33..word_34" mentiontype="ne"  
coref_class="set_2"  mmax_level="coref"  coreftype="ident" />
<markable id="markable_2" span="word_5..word_9" mentiontype="np"  
coref_class="set_1"  mmax_level="coref"  coreftype="ident" />
<markable id="markable_5" span="word_89..word_90" mentiontype="np"  
coref_class="set_3"  mmax_level="coref"  coreftype="ident" />
<markable id="markable_4" span="word_35..word_44" mentiontype="np"  
coref_class="set_2"  mmax_level="coref"  coreftype="ident" />
<markable id="markable_7" span="word_124..word_126" mentiontype="ne"  
coref_class="set_4"  mmax_level="coref"  coreftype="ident" />
<markable id="markable_6" span="word_91..word_95" mentiontype="np"  
coref_class="set_3"  mmax_level="coref"  coreftype="ident" />
</markables>

1 个答案:

答案 0 :(得分:1)

尝试一下,

import lxml.html

with open('Dari_Coref_2_coref_level.xml', 'rb') as file:
    xml = file.read()

tree = lxml.html.fromstring(xml)

#Use Xpath to extract the data you want.
# For example to extract ids of the tag markable, you can do
ids = tree.xpath("//markable/@id")
print(ids) # ['markable_1', 'markable_3', 'markable_2', ...]

Xpath语法教程:https://www.w3schools.com/xml/xpath_syntax.asp