在Python中从XML创建邻接矩阵

时间:2019-01-30 18:08:29

标签: python xml elementtree adjacency-matrix

我正在尝试创建 t_lemma 的邻接矩阵(其他元素,例如 nodetype,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

1 个答案:

答案 0 :(得分:0)

我想出了一个对测试过的大树都有效的答案,尽管我不得不考虑元素<ord>的含义(表示句子中单词的顺序)以消除以下问题:会出现以下句子:  “男人和女人,昼夜行走。”

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

仅考虑<t_lemma>会导致对(child->parent)函数的解释不明确,即:我们将有两个,其中的单词: man,woman ,白天,黑夜都这样引导:

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)