Skip to content

Instantly share code, notes, and snippets.

@yedhink
Created March 25, 2019 07:13
Show Gist options
  • Save yedhink/2fe0d04052535a8134b4caf904f02f4c to your computer and use it in GitHub Desktop.
Save yedhink/2fe0d04052535a8134b4caf904f02f4c to your computer and use it in GitHub Desktop.
nlp assignment 2
import nltk
from nltk import PCFG, CFG
from nltk.draw.tree import draw_trees
# out cfg
cfg_grammar = CFG.fromstring("""
S -> NP VP
NP -> ART N | N N | N | NP PP
VP -> V | V NP | V NP PP
PP -> P NP
ART -> 'a'
N -> 'flower' | 'a' | 'blooms'
V -> 'blooms' | 'flower'
""")
# out pcfg
pcfg_grammar = PCFG.fromstring("""
S -> NP VP [1.0]
NP -> ART N [0.53] | N N [0.09] | N [0.14] | NP PP [0.24]
VP -> V [0.386] | V NP [0.393] | V NP PP [0.22]
PP -> P NP [1.0]
ART -> 'a' [1.0]
N -> 'flower' [0.8] | 'a' [0.1] | 'blooms' [0.1]
V -> 'blooms' [0.8] | 'flower' [0.2]
""")
# sentence to be parsed
sentence = ['a','flower','blooms']
def question1():
bu_chart_parser2 = nltk.BottomUpChartParser(cfg_grammar)
trees = bu_chart_parser2.parse(sentence)
for item in trees:
print(item)
draw_trees(item)
def question2():
parser = nltk.ViterbiParser(pcfg_grammar)
trees = parser.parse(sentence)
for item in trees:
print(item)
draw_trees(item)
def question3():
parser = nltk.InsideChartParser(pcfg_grammar)
trees = parser.parse(sentence)
total = 0.0
for item in trees:
tree_prob = item.__dict__['_ProbabilisticMixIn__prob']
print("probability of each tree = {}".format(tree_prob))
total += item.__dict__['_ProbabilisticMixIn__prob']
draw_trees(item)
print("\nTotal probability = {}".format(total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment