Package: punctuator
PunctuatorThis is a fork ofOttokar Tilk's punctuator2cleaned up into a formal Python3 package with testing.DEMOandDEMO2A bidirectional recurrent neural network model with attention mechanism for restoring missing inter-word punctuation in unsegmented text.The model can be trained in two stages (second stage is optional):First stage is trained on punctuation annotated text. Here the model learns to restore puncutation based on textual features only.Optional second stage can be trained on punctuationandpause annotated text. In this stage the model learns to combine pause durations with textual features and adapts to the target domain. If pauses are omitted then only adaptation is performed. Second stage with pause durations can be used for example for restoring punctuation in automatic speech recognition system output.InstallationTo install:virtualenv -p python3.7 .env
. .env/bin/activate
pip install punctuatorAdditionally, you'll need a trained model. You can create your own following the instructions below, or you can use a pre-trained model fromhere.Place these models inPUNCTUATOR_DATA_DIRdirectory, which defaults to~/.punctuator.For example, to downloadDemo-Europarl-EN.pcl, activate your virtual environment and run:. .env/bin/activate
mkdir -p ~/.punctuator
cd ~/.punctuator
gdown https://drive.google.com/uc?id=0B7BsN5f2F1fZd1Q0aXlrUDhDbnMTo download other model files, find the Google Drive id via the share link, and substitute that in the command above.UsageTo use from the command line:cat input.txt | python punctuator.py model.pcl output.txtTo use from Python:from punctuator import Punctuator
p = Punctuator('model.pcl')
print(p.punctuate('some text'))How well does it work?A working demo can be seen here:http://bark.phon.ioc.ee/punctuatorYou can try to compete with this model here:http://bark.phon.ioc.ee/punctuator/gameRemember that all the scores given below are onunsegmentedtext and we did not use prosodic features, so, among other things, the model has to detect sentence boundaries in addition to the boundary type (?QUESTIONMARK, .PERIOD or !EXCLAMATIONMARK) based entirely on textual features. The scores are computed on the test set.Training speed with default settings, an optimal Theano installation and a modern GPU should be around 10000 words per second.Pretrained models can be downloadedhere(Demo + 2 models from the Interspeech paper).English TED talksTraining set size: 2.1M words. First stage only. More details can be found inthis paper.
For comparison, ourprevious modelgot an overall F1-score of 50.8.PUNCTUATIONPRECISIONRECALLF-SCORE,COMMA64.445.253.1?QUESTIONMARK67.558.762.8.PERIOD72.371.571.9Overall68.958.163.1English Europarl v7Training set size: 40M words. First stage only. Details in./example.You can try to compete with this modelhere.PUNCTUATIONPRECISIONRECALLF-SCORE?QUESTIONMARK77.773.275.4!EXCLAMATIONMARK50.00.10.1,COMMA68.972.070.4-DASH55.98.815.2:COLON60.923.834.2;SEMICOLON44.71.12.2.PERIOD84.784.184.4Overall75.773.974.8RequirementsPython 2.7NumpyTheanoRequirements for data:Cleaned text files for training and validation of the first phase model. Each punctuation symbol token must be surrounded by spaces.Example:to be ,COMMA or not to be ,COMMA that is the question .PERIOD(Optional)Pause annotated text files for training and validation of the second phase model. These should be cleaned in the same way as the first phase data. Pause durations in seconds should be marked after each word with a special tag<sil=0.200>. Punctuation mark, if any, must come after the pause tag.Example:to <sil=0.000> be <sil=0.100> ,COMMA or <sil=0.000> not <sil=0.000> to <sil=0.000> be <sil=0.150> ,COMMA that <sil=0.000> is <sil=0.000> the <sil=0.000> question <sil=1.000> .PERIODSecond phase data can also be without pause annotations to do just target domain adaptation.Make sure that first words of sentences don't have capitalized first letters. This would give the model unfair hints about period locations. Also, the text files you use for training and validation must be large enough (at least minibatch_size x sequence_length of words, which is 128x50=6400 words with default settings), otherwise you might get an error.ConfigurationVocabulary size, punctuation tokens and their mappings, and converted data location can be configured in the header of data.py.
Some model hyperparameters can be configured in the headings of main.py and main2.py. Learning rate and hidden layer size can be passed as arguments.UsageFirst step is data conversion. Assuming that preprocessed and cleaned *.train.txt, *.dev.txt and *.test.txt files are located in<data_dir>, the conversion can be initiated with:python data.py <data_dir>If you have second stage data as well, then:python data.py <data_dir> <second_stage_data_dir>The first stage can be trained with:python main.py <model_name> <hidden_layer_size> <learning_rate>e.gpython main.py <model_name> 256 0.02works well.Second stage can be trained with:python main2.py <model_name> <hidden_layer_size> <learning_rate> <first_stage_model_path>Preprocessed text can be punctuated with e.g:cat data.dev.txt | python punctuator.py <model_path> <model_output_path>or, if pause annotations are present in data.dev.txt and you have a second stage model trained on pause annotated data, then:cat data.dev.txt | python punctuator.py <model_path> <model_output_path> 1Punctuation tokens in data.dev.txt don't have to be removed - the punctuator.py script ignores them.Error statistics in this example can be computed with:python error_calculator.py data.dev.txt <model_output_path>You can play with a trained model with (assumes the input text is similarly preprocessed as the training data):python play_with_model.py <model_path>or with:python play_with_model.py <model_path> 1if you want to see, which words the model sees as UNKs (OOVs).DevelopmentRun all tests with:export TESTNAME=; toxRun a specific test in a specific environment with:export TESTNAME=.test_punctuate; tox -e py37