Skip to content

Commit 3e790e9

Browse files
vaish-sharmanorvig
authored andcommitted
Added test case for CYK_parse (aimacode#816)
* added test case for CYK_parse * added testcase for CYK_parse * corrected spacing * fixed issues like alignment, missing comma etc. * test case for double tennis problem * Update planning.py removed commented print statements.
1 parent 8a26b28 commit 3e790e9

File tree

5 files changed

+58
-16
lines changed

5 files changed

+58
-16
lines changed

nlp.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,17 @@ def __repr__(self):
239239

240240

241241
E_Chomsky = Grammar('E_Prob_Chomsky', # A Grammar in Chomsky Normal Form
242-
Rules(
243-
S='NP VP',
244-
NP='Article Noun | Adjective Noun',
245-
VP='Verb NP | Verb Adjective',
246-
),
247-
Lexicon(
248-
Article='the | a | an',
249-
Noun='robot | sheep | fence',
250-
Adjective='good | new | sad',
251-
Verb='is | say | are'
252-
))
242+
Rules(
243+
S='NP VP',
244+
NP='Article Noun | Adjective Noun',
245+
VP='Verb NP | Verb Adjective',
246+
),
247+
Lexicon(
248+
Article='the | a | an',
249+
Noun='robot | sheep | fence',
250+
Adjective='good | new | sad',
251+
Verb='is | say | are'
252+
))
253253

254254
E_Prob_Chomsky = ProbGrammar('E_Prob_Chomsky', # A Probabilistic Grammar in CNF
255255
ProbRules(
@@ -263,7 +263,18 @@ def __repr__(self):
263263
Adjective='good [0.5] | new [0.2] | sad [0.3]',
264264
Verb='is [0.5] | say [0.3] | are [0.2]'
265265
))
266-
266+
E_Prob_Chomsky_ = ProbGrammar('E_Prob_Chomsky_',
267+
ProbRules(
268+
S='NP VP [1]',
269+
NP='NP PP [0.4] | Noun Verb [0.6]',
270+
PP='Preposition NP [1]',
271+
VP='Verb NP [0.7] | VP PP [0.3]',
272+
),
273+
ProbLexicon(
274+
Noun='astronomers [0.18] | eyes [0.32] | stars [0.32] | telescopes [0.18]',
275+
Verb='saw [0.5] | \'\' [0.5]',
276+
Preposition='with [1]'
277+
))
267278

268279
# ______________________________________________________________________________
269280
# Chart Parsing

planning.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def act(self, action):
2626
"""
2727
Performs the action given as argument.
2828
Note that action is an Expr like expr('Remove(Glass, Table)') or expr('Eat(Sandwich)')
29-
"""
29+
"""
3030
action_name = action.op
3131
args = action.args
3232
list_action = first(a for a in self.actions if a.name == action_name)
@@ -536,7 +536,7 @@ def double_tennis_problem():
536536
expr('Partner(B, A)')]
537537

538538
def goal_test(kb):
539-
required = [expr('Goal(Returned(Ball))'), expr('At(a, RightNet)'), expr('At(a, LeftNet)')]
539+
required = [expr('Returned(Ball)'), expr('At(a, LeftNet)'), expr('At(a, RightNet)')]
540540
return all(kb.ask(q) is not False for q in required)
541541

542542
# Actions
@@ -546,14 +546,14 @@ def goal_test(kb):
546546
precond_neg = []
547547
effect_add = [expr("Returned(Ball)")]
548548
effect_rem = []
549-
hit = Action(expr("Hit(actor, Ball)"), [precond_pos, precond_neg], [effect_add, effect_rem])
549+
hit = Action(expr("Hit(actor, Ball, loc)"), [precond_pos, precond_neg], [effect_add, effect_rem])
550550

551551
# Go
552552
precond_pos = [expr("At(actor, loc)")]
553553
precond_neg = []
554554
effect_add = [expr("At(actor, to)")]
555555
effect_rem = [expr("At(actor, loc)")]
556-
go = Action(expr("Go(actor, to)"), [precond_pos, precond_neg], [effect_add, effect_rem])
556+
go = Action(expr("Go(actor, to, loc)"), [precond_pos, precond_neg], [effect_add, effect_rem])
557557

558558
return PDDL(init, [hit, go], goal_test)
559559

@@ -864,3 +864,17 @@ def goal_test(kb):
864864

865865
return Problem(init, [add_engine1, add_engine2, add_wheels1, add_wheels2, inspect1, inspect2],
866866
goal_test, [job_group1, job_group2], resources)
867+
868+
869+
def test_three_block_tower():
870+
p = three_block_tower()
871+
assert p.goal_test() is False
872+
solution = [expr("MoveToTable(C, A)"),
873+
expr("Move(B, Table, C)"),
874+
expr("Move(A, Table, B)")]
875+
876+
for action in solution:
877+
p.act(action)
878+
879+
assert p.goal_test()
880+

tests/.pytest_cache/v/cache/lastfailed

Whitespace-only changes.

tests/test_nlp.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ def test_CYK_parse():
113113
P = CYK_parse(words, grammar)
114114
assert len(P) == 52
115115

116+
grammar = nlp.E_Prob_Chomsky_
117+
words = ['astronomers', 'saw', 'stars']
118+
P = CYK_parse(words, grammar)
119+
assert len(P) == 32
120+
116121

117122
# ______________________________________________________________________________
118123
# Data Setup

tests/test_planning.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,19 @@ def test_spare_tire():
6262

6363
assert p.goal_test()
6464

65+
def test_double_tennis():
66+
p = double_tennis_problem()
67+
assert p.goal_test() is False
68+
69+
solution = [expr("Go(A, RightBaseLine, LeftBaseLine)"),
70+
expr("Hit(A, Ball, RightBaseLine)"),
71+
expr("Go(A, LeftNet, RightBaseLine)")]
72+
73+
for action in solution:
74+
p.act(action)
6575

76+
assert p.goal_test()
77+
6678
def test_three_block_tower():
6779
p = three_block_tower()
6880
assert p.goal_test() is False

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy