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

2024-04-26 00:36:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个注释语料库的任务,共指解决。你能告诉我如何从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>

Tags: iddfxmllevelclasswordrowspan
1条回答
网友
1楼 · 发布于 2024-04-26 00:36:15

试试这个

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

相关问题 更多 >