HPSG Neural Parser

September 11, 2019 ยท View on GitHub

This is a Python implementation of the parsers described in "Head-Driven Phrase Structure Grammar Parsing on Penn Treebank" from ACL 2019.

Contents

  1. Requirements
  2. Training
  3. Citation
  4. Credits

Requirements

  • Python 3.6 or higher.
  • Cython 0.25.2 or any compatible version.
  • PyTorch 0.4.0. This code has not been tested with PyTorch 1.0, but it should work.
  • EVALB. Before starting, run make inside the EVALB/ directory to compile an evalb executable. This will be called from Python for evaluation.
  • AllenNLP 0.7.0 or any compatible version (only required when using ELMo word representations)
  • pytorch-transformers PyTorch 1.0.0+ or any compatible version (only required when using BERT and XLNet, XLNet only for joint span version.)

Pre-trained Models (PyTorch)

The following pre-trained parser models are available for download:

The pre-trained model with Glove embeddings obtains 93.78 F-scores of constituent parsing and 96.09 UAS, 94.68 LAS of dependency parsing on the test set.

The pre-trained model with BERT obtains 95.84 F-scores of constituent parsing and 97.00 UAS, 95.43 LAS of dependency parsing on the test set.

The pre-trained model with XLNet obtains 96.33 F-scores of constituent parsing and 97.20 UAS, 95.72 LAS of dependency parsing on the test set.

To use ELMo embeddings, download the following files into the data/ folder (preserving their names):

There is currently no command-line option for configuring the locations/names of the ELMo files.

Pre-trained BERT and XLNet weights will be automatically downloaded as needed by the pytorch-transformers package.

Training

Download the 3 PTB data files from https://github.com/nikitakit/self-attentive-parser/tree/master/data, and put them in the data/ folder. The dependency structures are mainly obtained by converting constituent structure with version 3.3.0 of Stanford Parser in the data/ folder:

java -cp stanford-parser_3.3.0.jar edu.stanford.nlp.trees.EnglishGrammaticalStructure -basic -keepPunct -conllx -treeFile 02-21.10way.clean > ptb_train_3.3.0.sd

For CTB, we use the same datasets and preprocessing from the Distance Parser. For PTB, we use the same datasets and preprocessing from the self-attentive-parser. GloVe embeddings are optional.

Training Instructions

Some of the available arguments are:

ArgumentDescriptionDefault
--model-path-basePath base to use for saving modelsN/A
--evalb-dirPath to EVALB directoryEVALB/
--train-ptb-pathPath to training constituent parsingdata/02-21.10way.clean
--dev-ptb-pathPath to development constituent parsingdata/22.auto.clean
--dep-train-ptb-pathPath to training dependency parsingdata/ptb_train_3.3.0.sd
--dep-dev-ptb-pathPath to development dependency parsingdata/ptb_dev_3.3.0.sd
--batch-sizeNumber of examples per training update250
--checks-per-epochNumber of development evaluations per epoch4
--subbatch-max-tokensMaximum number of words to process in parallel while training (a full batch may not fit in GPU memory)2000
--eval-batch-sizeNumber of examples to process in parallel when evaluating on the development set30
--numpy-seedNumPy random seedRandom
--use-wordsUse learned word embeddingsDo not use word embeddings
--use-tagsUse predicted part-of-speech tags as inputDo not use predicted tags
--use-chars-lstmUse learned CharLSTM word representationsDo not use CharLSTM
--use-elmoUse pre-trained ELMo word representationsDo not use ELMo
--use-bertUse pre-trained BERT word representationsDo not use BERT
--use-xlnetUse pre-trained XLNet word representationsDo not use XLNet
--pad-leftWhen using pre-trained XLNet padding on leftDo not pad on left
--bert-modelPre-trained BERT model to use if --use-bert is passedbert-large-uncased
--no-bert-do-lower-caseInstructs the BERT tokenizer to retain case information (setting should match the BERT model in use)Perform lowercasing
--xlnet-modelPre-trained XLNet model to use if --use-xlnet is passedxlnet-large-cased
--no-xlnet-do-lower-caseInstructs the XLNet tokenizer to retain case information (setting should match the XLNet model in use)Perform uppercasing
--const-ladaLambda weight0.5
--model-nameName of modeltest
--embedding-pathPath to pre-trained embeddingN/A
--embedding-typePre-trained embedding typeglove
--datasetDataset typeptb

Additional arguments are available for other hyperparameters; see make_hparams() in src/main.py. These can be specified on the command line, such as --num-layers 2 (for numerical parameters), --use-tags (for boolean parameters that default to False), or --no-partitioned (for boolean parameters that default to True).

For each development evaluation, the best_dev_score is the sum of F-score and LAS on the development set and compared to the previous best. If the current model is better, the previous model will be deleted and the current model will be saved. The new filename will be derived from the provided model path base and the development best_dev_score.

As an example, after setting the paths for data and embeddings, to train a Joint-Span parser, simply run:

sh run_single.sh

to train a Joint-Span parser with BERT, simply run:

sh run_bert.sh

to train a Joint-Span parser with XLNet, simply run:

sh run_xlnet.sh

Evaluation Instructions

A saved model can be evaluated on a test corpus using the command python src/main.py test ... with the following arguments:

ArgumentDescriptionDefault
--model-path-basePath base of saved modelN/A
--evalb-dirPath to EVALB directoryEVALB/
--test-ptb-pathPath to test constituent parsingdata/23.auto.clean
--dep-test-ptb-pathPath to test dependency parsingdata/ptb_test_3.3.0.sd
--embedding-pathPath to pre-trained embeddingdata/glove.6B.100d.txt.gz
--eval-batch-sizeNumber of examples to process in parallel when evaluating on the test set100
--datasetDataset typeptb

As an example, after extracting the pre-trained model, you can evaluate it on the test set using the following command:

sh test.sh

If you want to parse the sentences, after setting the input file and pre-trained model, run following command:

sh parse.sh

Citation

If you use this software for research, please cite our paper as follows:

@inproceedings{zhou-zhao-2019-head,
    title = "Head-Driven Phrase Structure Grammar Parsing on {P}enn Treebank",
    author = "Zhou, Junru  and Zhao, Hai",
    booktitle = "Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics",
    month = jul,
    year = "2019",
    address = "Florence, Italy",
    publisher = "Association for Computational Linguistics",
}

Credits

The code in this repository and portions of this README are based on https://github.com/nikitakit/self-attentive-parser