Constituency Parsing

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

[1]:
%%time

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,

[2]:
from IPython.core.display import Image, display

display(Image('constituency.png', width=500))
_images/load-constituency_4_0.png

Read more at Stanford notes, https://web.stanford.edu/~jurafsky/slp3/13.pdf

The context free grammar totally depends on language, so for Bahasa, we follow https://github.com/famrashel/idn-treebank

List available transformer Constituency models

[2]:
malaya.constituency.available_transformer()
[2]:
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, https://malaya.readthedocs.io/en/latest/Accuracy.html#constituency-parsing

The best model in term of accuracy is XLNET.

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

Load xlnet constituency model

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

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

WARNING:tensorflow:From /Users/huseinzolkepli/Documents/Malaya/malaya/function/__init__.py:68: 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.

[10]:
tree = model.parse_nltk_tree(string)
[11]:
tree
[11]:
_images/load-constituency_14_0.png

Parse into Tree

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

[6]:
tree = model.parse_tree(string)