【问题标题】:How to change text sentence into CoNLL-U format?如何将文本句子转换为 CoNLL-U 格式?
【发布时间】:2020-05-14 06:10:43
【问题描述】:

我正在研究使用 CoNLL-U 格式的依赖项解析。 我可以找到如何处理 CoNLL-U 解析器或标记列表,但我找不到如何将文本句子转换为 CoNLL-U 格式。

我尝试从https://github.com/datquocnguyen/jPTDP转换代码

def conllConverter(path):
    writer = open(path) + ".conllu", "w", encoding = "utf-8")
    lines = open(path, "r", encoding = "utf-8").readlines()
    for line in lines:
        tok = line.strip().split()
        if not tok or line.strip() == "":
            writer.write("\n")
        else:
            count += 1
            writer.write(str(count) + "\t" + word + "\t" + "\t".join(['_'] * 8) + "\n")
        writer.write("\n")
    writer.close()

if __name__ == "__main__":
    conllCoverter("test")
    pass

“test”文件是conllCoverter(path)函数的输入,是“_io Text10Wrapper”格式文件,里面包含我要转换成CoNLL-U文件的文本语句,例如: 1.完全令人沮丧的经历。 2. 额外花钱买了有网的空调。

但是,在我尝试了上面定义的 conllConverter(path) 函数后,输出只显示了 10 个原始列(看起来像 CoNLL-U 格式)和原始文本,没有任何额外信息。

最后,我想问一下如何将文本句子转换为 CoNLL-U 格式。

【问题讨论】:

    标签: dependency-parsing conll


    【解决方案1】:

    试试这个

    import spacy
    from spacy_conll import ConllFormatter
    
    nlp = spacy.load('en_core_web_sm')
    conllformatter = ConllFormatter(nlp, ext_names={'conll_pd': 'pandas'},
                                    conversion_maps={'lemma': {'-PRON-': 'PRON'}})
    nlp.add_pipe(conllformatter, after='parser')
    doc = nlp('The quick brown fox jumps over the lazy dog.')
    print(doc._.pandas)
    

    这将为您提供以下输出

       id   form  lemma upostag xpostag           feats  head    deprel deps  misc
        1    The    the     DET      DT               _     5       det    _   _
        2  quick  quick     ADJ      JJ      Degree=pos     5      amod    _   _
        3  brown  brown     ADJ      JJ      Degree=pos     5      amod    _   _
        4    fox    fox    NOUN      NN     Number=sing     5  compound    _   _
        5  jumps   jump    NOUN     NNS     Number=plur     0      ROOT    _   _
        6   over   over     ADP      IN               _     5      prep    _   _
        7    the    the     DET      DT               _     9       det    _   _
        8   lazy   lazy     ADJ      JJ      Degree=pos     9      amod    _   _
        9    dog    dog    NOUN      NN     Number=sing     6      pobj    _   SpaceAfter=No
        10      .      .   PUNCT       .  PunctType=peri     5     punct   _   SpaceAfter=No
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-02
      • 2015-01-13
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多