4/9/22, 11:37 AM CFG
In [1]: import nltk
from nltk import CFG
In [2]: #buat terlebih dahulu aturan CFG yang sesuai
grammar1 = CFG.fromstring("""
S -> NP VP
VP -> V NP | V NP PP
PP -> P NP
V -> "saw" | "ate" | "walked"
NP -> "John" | "Mary" | "Bob" | Det N | Det N PP
Det -> "a" | "an" | "the" | "my"
N -> "man" | "dog" | "cat" | "telescope" | "park"
P -> "in" | "on" | "by" | "with"
""")
In [3]: grammar1.productions() #jika ingin melihat aturan CFGnya
Out[3]: [S -> NP VP,
VP -> V NP,
VP -> V NP PP,
PP -> P NP,
V -> 'saw',
V -> 'ate',
V -> 'walked',
NP -> 'John',
NP -> 'Mary',
NP -> 'Bob',
NP -> Det N,
NP -> Det N PP,
Det -> 'a',
Det -> 'an',
Det -> 'the',
Det -> 'my',
N -> 'man',
N -> 'dog',
N -> 'cat',
N -> 'telescope',
N -> 'park',
P -> 'in',
P -> 'on',
P -> 'by',
P -> 'with']
In [7]: #input kalimat berupa list, jika ingin mengecek satu dokumen, untuk mengecek per kal
#pecah dokumen menjadi per kalimat
#per kalimat lakukan tokenisasi simpan setiap tokenisasi kalimat dalam satu list
#loop kumpulan list untuk melakukan parser pada setiap kalimat
#jika tidak bisa diparser maka kalimat tidak sah secara grammar CFG yang dibuat
sent = ['Bob', 'walked', 'a', 'dog', 'by', 'the', 'park']
parser = nltk.ChartParser(grammar1) #proses parser
trees = list(parser.parse(sent)) #proses membuat tree
In [8]: print(trees[0]) #direpresentasikan dalam notasi braket
(S
(NP Bob)
(VP
(V walked)
(NP (Det a) (N dog))
(PP (P by) (NP (Det the) (N park)))))
In [9]: #khusus colab jika ingin mengubah notasi braket menjadi gambar berekstensi .ps
#jika pakai jupyter notebook, bisa diskip
file:///C:/Users/retna/Downloads/CFG.html 1/2
4/9/22, 11:37 AM CFG
### CREATE VIRTUAL DISPLAY ###
!apt-get install -y xvfb # Install X Virtual Frame Buffer
import os
os.system('Xvfb :1 -screen 0 1600x1200x16 &') # create virtual display with size
os.environ['DISPLAY']=':1.0' # tell X clients to use our virtual DISPLAY :1.0.
Reading package lists... Done
Building dependency tree
Reading state information... Done
xvfb is already the newest version (2:1.19.6-1ubuntu4.10).
0 upgraded, 0 newly installed, 0 to remove and 39 not upgraded.
In [10]: from nltk.tree import Tree
from nltk.draw.tree import TreeView
t = Tree.fromstring('(S(NP Bob)(VP(V walked)(NP(Det a)(N dog))(PP(P by)(NP(Det the)(
TreeView(t)._cframe.print_to_file('output.ps') #buka .ps menggunakan ps viewer untuk
file:///C:/Users/retna/Downloads/CFG.html 2/2