繁体   English   中英

在Python中从XML创建邻接矩阵

[英]Creating an adjacency matrix from XML in Python

我正在尝试创建t_lemma的邻接矩阵(可以忽略其他元素(例如, 节点类型 ,ord等),出于完全考虑的目的,我将它们包括在内),这意味着t_lemma是其中的父级-从此表示对(捷克语)句子进行句法分析的XML文档,其中t_lemma表示特定单词的中性形状。

目前,我正在使用Python的cElementTree库,但是如果我要求的内容是不可能的,或者使用cElementTree很难实现计算时,我愿意使用其他方法

<t_tree id="t_tree-cs-s1-root">
    <atree.rf>a_tree-cs-s1-root</atree.rf>
    <ord>0</ord>
    <children id="t_tree-cs-s1-n107">
        <children>
            <LM id="t_tree-cs-s1-n108">
                <nodetype>complex</nodetype>
                <ord>1</ord>
                <t_lemma>muž</t_lemma>
                <functor>ACT</functor>
                <formeme>n:1</formeme>
                <is_clause_head>0</is_clause_head>
                <clause_number>1</clause_number>
                <a>
                    <lex.rf>a_tree-cs-s1-n1</lex.rf>
                </a>
                <gram>
                    <sempos>n.denot</sempos>
                    <gender>anim</gender>
                    <number>sg</number>
                    <negation>neg0</negation>
                </gram>
            </LM>
            <LM id="t_tree-cs-s1-n109">
                <nodetype>complex</nodetype>
                <ord>3</ord>
                <t_lemma>strom</t_lemma>
                <functor>PAT</functor>
                <formeme>n:4</formeme>
                <is_clause_head>0</is_clause_head>
                <clause_number>1</clause_number>
                <a>
                    <lex.rf>a_tree-cs-s1-n3</lex.rf>
                </a>
                <gram>
                    <sempos>n.denot</sempos>
                    <gender>inan</gender>
                    <number>sg</number>
                    <negation>neg0</negation>
                </gram>
            </LM>
        </children>
        <nodetype>complex</nodetype>
        <ord>2</ord>
        <t_lemma>zasadit</t_lemma>
        <functor>PRED</functor>
        <formeme>v:fin</formeme>
        <sentmod>enunc</sentmod>
        <is_clause_head>1</is_clause_head>
        <clause_number>1</clause_number>
        <a>
            <lex.rf>a_tree-cs-s1-n2</lex.rf>
        </a>
        <gram>
            <sempos>v</sempos>
            <verbmod>ind</verbmod>
            <deontmod>decl</deontmod>
            <tense>ant</tense>
            <aspect>cpl</aspect>
            <resultative>res0</resultative>
            <dispmod>disp0</dispmod>
            <iterativeness>it0</iterativeness>
            <negation>neg0</negation>
            <diathesis>act</diathesis>
        </gram>
    </children>
</t_tree>

该XML表示的是一棵看起来像这样的树:

t_tree

我想要得到的是一个像这样的矩阵。

        muž     strom    zasadit
muž     1       0       -1

storm   0       1       -1

zasadit 1       1       1

我想出了一个可以在测试过的大树上使用的答案,尽管我不得不考虑元素<ord> -表示句子中单词的顺序-消除了出现以下情况时会出现的问题像这样的句子: “男人和女人,日夜行走。”

       walking
      /       \
   and        and
  /   \       /  \
man  woman  day  night

仅考虑<t_lemma>会导致对(child->parent)函数的解释不清楚,即:我们将有两个 s分别指向这两个词: 男人,女人,白天,黑夜,如下所示:

element  parent
_______________
man      and
woman    and
day      and
night    and
and      walking
and      walking      

上一张表变成了以下表格:

element  parent
_______________
man:1    and:2
woman:3  and:2
day:5    and:6
night:7  and:6
and:2    walking:4
and:6    walking:4

因此,这是功能性的Python代码:

parentDictionary = {}
def getchildlemma(element, parent):
    for i in element.findall("*"):
        if i.tag == "t_lemma":
            e = i.text

            for i in element.findall("*"):
                if i.tag == "ord":
                    e = e +":"+ i.text
            parentDictionary[e] = parent
            parent = e
        else:
            e = parent
    for i in element.findall("*"):
        if i.tag == "children" or i.tag == "LM":
            getchildlemma(i,parent)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM