0% found this document useful (0 votes)
117 views2 pages

Import From Import: NLTK NLTK CFG

The document shows an example of using a Context Free Grammar (CFG) to parse sentences in Python using NLTK. It defines a CFG with production rules and lexical categories. Sample sentences are parsed using the CFG and represented as trees. One tree is printed and also converted to a .ps file for visualization.

Uploaded by

Virgi Al-fiansah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views2 pages

Import From Import: NLTK NLTK CFG

The document shows an example of using a Context Free Grammar (CFG) to parse sentences in Python using NLTK. It defines a CFG with production rules and lexical categories. Sample sentences are parsed using the CFG and represented as trees. One tree is printed and also converted to a .ps file for visualization.

Uploaded by

Virgi Al-fiansah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 = [Link]("""

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]: [Link]() #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 = [Link](grammar1) #proses parser

trees = list([Link](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

[Link] 1/2
4/9/22, 11:37 AM CFG

### CREATE VIRTUAL DISPLAY ###

!apt-get install -y xvfb # Install X Virtual Frame Buffer

import os

[Link]('Xvfb :1 -screen 0 1600x1200x16 &') # create virtual display with size


[Link]['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 [Link] import Tree

from [Link] import TreeView

t = [Link]('(S(NP Bob)(VP(V walked)(NP(Det a)(N dog))(PP(P by)(NP(Det the)(


TreeView(t)._cframe.print_to_file('[Link]') #buka .ps menggunakan ps viewer untuk

[Link] 2/2

You might also like