Package: diaparser
DiaParserDiaParseris astate-of-the-art dependency parser, that extends the architecture of the Biaffine Parser (Dozat and Manning, 2017) by exploiting both embeddings and attentions provided bytransformers.By exploiting the rich hidden linguistic information in contextual embeddings from transformers,DiaParsercan avoid using intermediate annotations like POS, lemma and morphology, used in traditional parsers.
Therefore the only stages in the parsing pipeline are tokenization and parsing.The parser may also workdirectly on plain text.
The parser automatically dowloads pretrained models as well as tokenizers and produces dependency parsing trees, as detailed inUsage.Exploiting attentions from transformer heads provides improvements in accuracy, without resorting to fine tuning or training its own attention.
Overall, this simplifies the architecture and lowers the cost of resources needed during training, especially memory, and allows the parser to improve as new versions of transformers become available.
The parser uses theHuggingFace Transformers APIand in particular the genericAutoClasses interfaceto access the transformer models avaiable.We plan to track the improvements in transformer technology and to realease models of the parser that incorporate these models.
Currently we providepretrained modelsfor 18 languages.We encourage anyone to contribute trained models for other combinatiobns of language/transformer pairs, that we will publish.
We will soon provide a web form where to upload new models.You can also train your own models and contribute them to the repository, to share with others.DiaParseruses pretrained contextual embeddings for representing input from models intransformers.Pretrained tokenizers are provided byStanza.Alternatively to contextual embeddings,DiaParseralso allows to utilize CharLSTM layers to produce character/subword-level features.
Both BERT and CharLSTM avoid the need of generating POS tags.DiaParseris derived fromSuPar, which provides additional variants of dependency and constituency parsers.ContentsContentsInstallationPerformanceUsageTrainingEvaluationTODOReferencesInstallationDiaParsercan be installed viapip:$pipinstall-UdiaparserInstalling from sources is also possible:$gitclonehttps://github.com/Unipisa/diaparser&&cddiaparser
$pythonsetup.pyinstallThe package has the following requirements:python: >= 3.6pytorch: >= 1.4transformers: >= 3.1optional tokenizersstanza: >= 1.1.1PerformanceDiaParserprovides pretrained models for English, Chinese and other 21 languages from the Universal Dependencies treebanksv2.6.
English models are trained on the Penn Treebank (PTB) with Stanford Dependencies, with 39,832 training sentences, while Chinese models are trained on Penn Chinese Treebank version 7 (CTB7) with 46,572 training sentences.The accuracy and parsing speed of these models are listed in the following tables.
The first table shows the result of parsing starting from gold tokenized text.
Notably, punctuation is ignored in the evaluation metrics for PTB, but included in all the others.
The numbers in bold represent state-of-the-art values.LanguageCorpusNameUASLASSpeed (Sents/s)EnglishPTBen_ptb.electra96.0394.37352ChineseCTBzh_ptb.hfl92.1485.74319CatalanAnCoraca_ancora.mbert95.5593.78249GermanHDTde_htd.dbmdz-bert-base97.9796.97184JapaneseGSDja_gsd.mbert95.4193.98397LatinITTB, LLCTla_ittb_llct.mbert94.0391.70139NorwegianNynorskno_nynorsk.mbert92.5090.13185RomanianRRTro_rrt.mbert93.0387.18286SpanishAnCoraes_ancora.mbert96.0394.37352TurkishBountr_boun.electra-base83.5375.871198Below are the results on the dataset of theIWPT 2020 Shared Task on Enhanced Dependencies, where the tokenization was done by the parser itself:LanguageCorpusNameUASLASSpeed (Sents/s)ArabicPADTar_padt.bert87.7583.2599BulgarianBTBbg_btb.DeepPavlov95.0292.20479CzechPDT, CAC, FicTreecs_pdt_cac_fictree.DeepPavlov94.0292.06403EnglishEWTen_ewt.electra91.6689.51397EstonianEDT, EWTet_edt_ewt.mbert86.3982.44247FinnishTDTfi_tdt.turkunlp94.2892.56364Frenchsequoiafr_sequoia.camembert92.8189.55200GermanHDTde_hdt.dbmdz-bert-base97.9796.97381ItalianISDTit_isdt.dbmdz-electra-xxl95.4894.16379LatvianLVBTlv_lvtb.mbert87.4683.51290LithuanianALKSNISlt_alksnis.mbert80.0975.14290DutchAlpino, LassySmallnl_alpino_lassysmall.wietsedv90.8088.34367PolishPDB, LFGpl_pdb_lfg.dkleczek94.3891.70563RussianSynTagRusru_syntagrus.DeepPavlov94.9793.72445SlovakSNKsk_snk.mbert93.1190.44381SwediskhTalbankensv_talbanken.KB90.7988.08491TamilTTBta_ttb.mbert74.2066.49175UkrainianIUuk_iu.TurkuNLP90.3987.61301These results were obtained on a server with Intel(R) Xeon(R) Gold 6132 CPU @ 2.60GHz
and Nvidia T4 GPU.UsageDiaParseris simple to use: you can just download a pretrained model and run syntactic parsing over sentences with a few lines of code:>>>fromdiaparser.parsersimportParser>>>parser=Parser.load('en_ewt-electra')>>>dataset=parser.predict([['She','enjoys','playing','tennis','.']],prob=True)The call toparser.predictwill return an instance ofdiaparser.utils.Datasetcontaining the predicted syntactic trees.
You can access each sentence within thedataset:>>>print(dataset.sentences[0])1She____2nsubj__2enjoys____0root__3playing____2xcomp__4tennis____3dobj__5.____2punct__To parse plain text just requires specifying the language code:>>>dataset=parser.predict('She enjoys playing tennis.',text='en')An SVG picture illusrating the parse tree can be produced with:>>>sent=dataset.sentences[0]>>>displacy.render(sent.to_displacy(),style='dep',manual=True,options={'compact':True,'distance':120})The input can be provided in a file in CoNLL-U format.Further examples of how to use the parser and experiment with it can be found in thisnotebook.TrainingTo train a model from scratch, it is preferred to use the command-line option, which is more flexible and customizable.
Here are some training examples:# Biaffine Dependency Parser# some common and default arguments are stored in config.ini$python-mdiaparser.cmds.biaffine_dependencytrain-b-d0\-cconfig.ini\-pexp/en_ptb.char/model\-fchar# to use BERT, `-f` and `--bert` (default to bert-base-cased) should be specified$python-mdiaparser.cmds.biaffine_dependencytrain-b-d0\-pexp/en_ptb.bert-base/model\-fbert\--bertbert-base-casedWarning. There is currently a limit of 500 to the length of tokenized sentences, due to the maximum size of embeddings in most pretrained trnsformer models.For further instructions on training, please typepython -m diaparser.cmds.<parser> train -h.Alternatively,DiaParserprovides an equivalent command entry points registered insetup.py:diaparser.$diaparsertrain-b-d0-cconfig.ini-pexp/en_ptb.electra-base/model-fbert--bertgoogle/electra-base-discriminatorFor handling large models, distributed training is also supported:$python-mtorch.distributed.launch--nproc_per_node=4--master_port=10000\-mparser.cmds.biaffine_dependencytrain-b-d0,1,2,3\-pexp/en_ptb.electra-base/model\-fbert--bertgoogle/electra-base-discriminatorYou may consult the PyTorchdocumentationandtutorialsfor more details.EvaluationThe evaluation process resembles prediction:>>>parser=Parser.load('biaffine-dep-en')>>>loss,metric=parser.evaluate('data/ptb/test.conllx')2020-07-2520:59:17INFOLoadingthedata2020-07-2520:59:19INFODataset(n_sentences=2416,n_batches=11,n_buckets=8)2020-07-2520:59:19INFOEvaluatingthedataset2020-07-2520:59:20INFOloss:0.2326-UCM:61.34%LCM:50.21%UAS:96.03%LAS:94.37%2020-07-2520:59:20INFO0:00:01.253601selapsed,1927.25Sents/sTODOProvide a repository where to upload models, like HuggingFace.ReferencesGiuseppe Attardi, Daniele Sartiano, Yu Zhang. 2021. DiaParser attentive dependency parser. Submitted for publication.Timothy Dozat and Christopher D. Manning. 2017.Deep Biaffine Attention for Neural Dependency Parsing.Xinyu Wang, Jingxian Huang, and Kewei Tu. 2019.Second-Order Semantic Dependency Parsing with End-to-End Neural Networks.