Constituency Parsing

This tutorial is available as an IPython notebook at Malaya/example/constituency.


import malaya
CPU times: user 5.34 s, sys: 1.01 s, total: 6.34 s
Wall time: 7.21 s

what is constituency parsing

Assign a sentence into its own syntactic structure, defined by certain standardization. For example,

from IPython.core.display import Image, display

display(Image('constituency.png', width=500))

Read more at Stanford notes,

The context free grammar totally depends on language, so for Bahasa, we follow

List available transformer Constituency models

Size (MB) Recall Precision FScore CompleteMatch TaggingAccuracy
bert 470.0 78.96 81.78 80.35 10.37 91.59
tiny-bert 125.0 74.89 78.79 76.79 9.01 91.17
albert 180.0 77.57 80.50 79.01 5.77 90.30
tiny-albert 56.7 67.21 74.89 70.84 2.11 87.75
xlnet 498.0 80.65 82.22 81.43 11.08 92.12

Make sure you can check accuracy chart from here first before select a model,

The best model in term of accuracy is XLNET.

string = 'Dr Mahathir menasihati mereka supaya berhenti berehat dan tidur sebentar sekiranya mengantuk ketika memandu.'

Load xlnet constituency model

model = malaya.constituency.transformer(model = 'xlnet')
WARNING:tensorflow:From /Users/huseinzolkepli/Documents/Malaya/malaya/function/ The name tf.gfile.GFile is deprecated. Please use instead.

WARNING:tensorflow:From /Users/huseinzolkepli/Documents/Malaya/malaya/function/ The name tf.GraphDef is deprecated. Please use tf.compat.v1.GraphDef instead.

WARNING:tensorflow:From /Users/huseinzolkepli/Documents/Malaya/malaya/function/ The name tf.InteractiveSession is deprecated. Please use tf.compat.v1.InteractiveSession instead.

Parse into NLTK Tree

Make sure you already installed nltk, if not, simply,

pip install nltk

We preferred to parse into NLTK tree, so we can play around with children / subtrees.

tree = model.parse_nltk_tree(string)

Parse into Tree

This is a simple Tree object defined at malaya.text.trees.

tree = model.parse_tree(string)