2

I am studying dependency parsing using CoNLL-U format. I can find how to handle CoNLL-U parser or tokenlist, but I cannot find how to convert a text sentence into a CoNLL-U format.

I tried converting code from 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" file, which is the input of conllCoverter(path) function, is "_io Text10Wrapper" format file which contains text sentences I want to convert to CoNLL-U file such as: 1. Completely frustrating experience . 2. Paid extra money to get the air conditioner with connectivity .

However, after I tried conllConverter(path) function which is defined above, the output shows me only the 10-raw columns (seems like CoNLL-U format) and raw text without any extra information.

In conclusion, I want to ask how could I convert text sentence into a CoNLL-U format.

1 Answers1

2

Try this

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)

Which will give you the following output

   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
Abu Shoeb
  • 4,747
  • 2
  • 40
  • 45