【问题标题】:Creating an adjacency matrix from XML in Python在 Python 中从 XML 创建邻接矩阵
【发布时间】:2019-01-30 18:08:28
【问题描述】:

我正在尝试创建一个 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 代表的是一个看起来像这样的树:

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

        muž     strom    zasadit
muž     1       0       -1

storm   0       1       -1

zasadit 1       1       1

【问题讨论】:

  • 请说明问题所在。你试过什么?
  • 如果我们看一下我包含的树(即上面 XML 表示的精确树),我们可以看到 3 个节点:“zasadit”、“muž”、“strom”。我试图找到一种通用方法 - 对于任意复杂的树 - 找到哪个“单词元素”从属于哪个,例如将生成 对的函数 例如

标签: python xml elementtree adjacency-matrix


【解决方案1】:

我找到了一个适用于我测试过的非常大的树的答案,尽管我必须考虑元素 &lt;ord&gt; - 表示句子中单词的顺序 - 以消除可能出现的问题如果是这样的句子: “男人和女人,日夜走。”

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

仅考虑&lt;t_lemma&gt; 会导致对(child-&gt;parent) 函数的解释不明确,即:我们将有两个,分别是:男人、女人、白天、夜晚 都是这样引导的:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多