def test_ideal_framework(self): sceptically_preferred_sets = Semantics.sceptically_preferred( ExampleFrameworks.ideal_framework()) self.assertIn(Semantics.SemanticSet([a]), sceptically_preferred_sets) self.assertEqual(1, len(sceptically_preferred_sets)) ideal_sets = Semantics.ideal(ExampleFrameworks.ideal_framework()) self.assertIn(Semantics.SemanticSet([]), ideal_sets) self.assertEqual(1, len(ideal_sets))
def test_admissible_larger_framework(self): baba = ExampleFrameworks.larger_framework() self.assertTrue(Semantics.is_admissible(baba, [b, e, g, h])) self.assertTrue(Semantics.is_admissible(baba, [b, e, i])) self.assertTrue(Semantics.is_admissible(baba, [b, f, h, g, i])) self.assertFalse(Semantics.is_admissible(baba, [b, e, f, g, h])) self.assertFalse(Semantics.is_admissible(baba, [b, e, f, i]))
def test_conflict_free_trivially(self): baba = ExampleFrameworks.valid_BABA_framework() self.assertTrue(Semantics.conflict_free(baba, [])) self.assertTrue(Semantics.conflict_free(baba, [a])) self.assertTrue(Semantics.conflict_free(baba, [a, b, c])) self.assertFalse(Semantics.conflict_free(baba, [b, d])) self.assertFalse(Semantics.conflict_free(baba, [a, b, c, d, e]))
def test_r_grounded_semantics(self): baba = ExampleFrameworks.r_framework() grounded = Semantics.grounded(baba) self.assertEqual(1, len(grounded)) grounded_set = grounded.pop() self.assertEqual(3, len(grounded_set.elements)) self.assertTrue( all([elem in grounded_set.elements for elem in [a, b, c]]))
def test_derivable(self): baba = ExampleFrameworks.with_chaining() self.assertTrue(Semantics.derivable(baba, a, [b, d, f])) self.assertTrue(Semantics.derivable(baba, a, [b, d, f, g])) self.assertTrue(Semantics.derivable(baba, g, [c])) self.assertTrue(Semantics.derivable(baba, g, [d, f])) self.assertFalse(Semantics.derivable(baba, g, [d])) self.assertFalse(Semantics.derivable(baba, a, [b, d])) self.assertFalse(Semantics.derivable(baba, a, [b, f, g]))
def test_valid_cow_framework_probabilities(self): baba = ExampleFrameworks.cow_framework() self.assertAlmostEqual( 0.18, Semantics.semantic_probability(Semantics.GROUNDED, baba, [ExampleFrameworks.HP])) baba = ExampleFrameworks.conditional_cow_framework() self.assertAlmostEqual( 0.02, baba.BN.p_world([ ExampleFrameworks.cond_JN, ExampleFrameworks.JF, ExampleFrameworks.CM ])) self.assertAlmostEqual( 0.72, Semantics.semantic_probability(Semantics.GROUNDED, baba, [ExampleFrameworks.HP]))
def test_r_grounded_semantics_with_random_variable_world(self): baba = ExampleFrameworks.r_framework() baba.rv_world = [Semantics.Sentence('s', random_variable=True)] grounded = Semantics.grounded(baba) self.assertEqual(1, len(grounded)) grounded_set = grounded.pop() self.assertEqual(2, len(grounded_set.elements)) self.assertTrue(all([elem in grounded_set.elements for elem in [b, c]]))
def test_derivable_with_empty_body_rules(self): baba = ExampleFrameworks.cow_framework() self.assertTrue(Semantics.derivable(baba, ExampleFrameworks.HOC, [])) self.assertFalse( Semantics.derivable(baba, ExampleFrameworks.not_HOC, [])) self.assertTrue( Semantics.derivable(baba, ExampleFrameworks.not_FM, [ExampleFrameworks.not_FM])) self.assertFalse( Semantics.derivable(baba, ExampleFrameworks.FM, [ExampleFrameworks.not_FM]))
def test_compute_semantic_probabilities(self): g, s, i = Semantics.compute_semantic_probabilities( ExampleFrameworks.r_framework()) self.assertEqual(g, [('_a', '0.360'), ('_b', '0.400'), ('_c', '0.000'), ('a', '0.640'), ('b', '0.600'), ('c', '1.000'), ('j', '0.640'), ('s', '0.600'), ('t', '0.400')]) self.assertEqual(s, [('_a', '0.360'), ('_b', '0.400'), ('_c', '0.000'), ('a', '0.640'), ('b', '0.600'), ('c', '1.000'), ('j', '0.640'), ('s', '0.600'), ('t', '0.400')]) self.assertEqual(i, [('_a', '0.360'), ('_b', '0.400'), ('_c', '0.000'), ('a', '0.640'), ('b', '0.600'), ('c', '1.000'), ('j', '0.640'), ('s', '0.600'), ('t', '0.400')])
def test_conflict_free_venice_framework(self): baba = ExampleFrameworks.venice_framework() self.assertTrue(Semantics.conflict_free(baba, [a]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [b]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [c]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [d]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [e]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [a, c]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [a, d]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [a, e]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [b, d]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [b, e]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [c, e]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [a, c, e]), "Expected: conflict free") self.assertFalse(Semantics.conflict_free(baba, [b, c]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [c, d]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [a, b, c]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [a, b, d]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [a, b, e]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [b, c, d]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [b, c, e]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [c, d, e]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [a, b, c, d]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [a, b, c, e]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [a, b, d, e]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [a, c, d, e]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [a, b, c, d, e]), "Expected: NOT conflict free")
def test_invalid_semantic_probability(self): baba = ExampleFrameworks.r_framework() def invalid_semantics(): return Semantics.semantic_probability(-1, baba, [a]) self.assertRaises(Semantics.InvalidSemanticsException, invalid_semantics) def invalid_sentences(): return Semantics.semantic_probability(Semantics.GROUNDED, baba, [f]) self.assertRaises(Semantics.InvalidBABAException, invalid_sentences)
def test_conflict_free(self): baba = ExampleFrameworks.with_contraries() self.assertTrue(Semantics.conflict_free(baba, []), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [d, e]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [d, e, f, h]), "Expected: conflict free") self.assertFalse(Semantics.conflict_free(baba, [d, e, f, g]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [d, e, h, i]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [d, e, f, g, h, i]), "Expected: NOT conflict free")
def test_grounded_probability(self): baba = ExampleFrameworks.r_framework() self.assertEqual( 0.64, Semantics.semantic_probability(Semantics.GROUNDED, baba, [j])) self.assertEqual( 0.64, Semantics.semantic_probability(Semantics.GROUNDED, baba, [a])) self.assertEqual( 0.64, Semantics.semantic_probability(Semantics.GROUNDED, baba, [a, j])) self.assertEqual( 0.6, Semantics.semantic_probability(Semantics.GROUNDED, baba, [b])) self.assertEqual( 1.0, Semantics.semantic_probability(Semantics.GROUNDED, baba, [c]))
def test_compute_grounded_probability(self): baba = ExampleFrameworks.r_framework() grounded_probabilities = Semantics.compute_semantic_probability( Semantics.GROUNDED, baba) self.assertEqual(0.64, grounded_probabilities[ExampleFrameworks.a.symbol]) self.assertEqual(0.36, grounded_probabilities[ExampleFrameworks._a.symbol]) self.assertEqual(0.6, grounded_probabilities[ExampleFrameworks.b.symbol]) self.assertEqual(0.4, grounded_probabilities[ExampleFrameworks._b.symbol]) self.assertEqual(1.0, grounded_probabilities[ExampleFrameworks.c.symbol]) self.assertEqual(0.0, grounded_probabilities[ExampleFrameworks._c.symbol]) self.assertEqual(0.64, grounded_probabilities[ExampleFrameworks.j.symbol])
def test_conflict_free_larger_framework(self): baba = ExampleFrameworks.larger_framework() self.assertTrue(Semantics.conflict_free(baba, []), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [b, e, f]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [b, b, e, f]), "Expected: conflict free") self.assertTrue(Semantics.conflict_free(baba, [b, b, i, f, g, h]), "Expected: conflict free") self.assertFalse(Semantics.conflict_free(baba, [b, d, e, f]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [b, e, f, g, h]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [b, e, f, i]), "Expected: NOT conflict free") self.assertFalse(Semantics.conflict_free(baba, [b, d, e, f, g, h, i]), "Expected: NOT conflict free")
def test_compute_ideal_probability(self): ideal_probabilities = Semantics.compute_semantic_probability( Semantics.IDEAL, ExampleFrameworks.r_framework()) self.assertAlmostEqual(0.64, ideal_probabilities[ExampleFrameworks.a.symbol]) self.assertAlmostEqual( 0.36, ideal_probabilities[ExampleFrameworks._a.symbol]) self.assertAlmostEqual(0.60, ideal_probabilities[ExampleFrameworks.b.symbol]) self.assertAlmostEqual( 0.40, ideal_probabilities[ExampleFrameworks._b.symbol]) self.assertAlmostEqual(1.0, ideal_probabilities[ExampleFrameworks.c.symbol]) self.assertAlmostEqual( 0.0, ideal_probabilities[ExampleFrameworks._c.symbol]) self.assertAlmostEqual(0.64, ideal_probabilities[ExampleFrameworks.j.symbol]) self.assertAlmostEqual(0.6, ideal_probabilities[ExampleFrameworks.s.symbol]) self.assertAlmostEqual(0.4, ideal_probabilities[ExampleFrameworks.t.symbol])
def test_required_to_derive(self): baba = ExampleFrameworks.larger_framework() required_to_derive_a = baba.compute_required_to_derive(a) self.assertIn([b], required_to_derive_a) self.assertIn([e, f], required_to_derive_a) self.assertEqual(3, len(required_to_derive_a)) self.assertEqual(1, len(baba.compute_required_to_derive(j))) required_to_derive_c = baba.compute_required_to_derive(c) self.assertEqual(5, len(required_to_derive_c)) first_derivation = required_to_derive_c[0] second_derivation = required_to_derive_c[1] all( self.assertIn(element, first_derivation) or self.assertIn(element, second_derivation) for element in [e, f, g, h]) all( self.assertIn(element, first_derivation) or self.assertIn(element, second_derivation) for element in [e, f, i])
def test_derivable_set(self): baba = ExampleFrameworks.with_chaining() self.assertTrue( all([ element in Semantics.derivable_set(baba, [b, c]) for element in [a, b, c, g] ])) self.assertEqual(4, len(Semantics.derivable_set(baba, [b, c]))) self.assertTrue( all([ element in Semantics.derivable_set(baba, [b, d, f]) for element in [a, b, c, d, e, f, g] ])) self.assertEqual(7, len(Semantics.derivable_set(baba, [b, d, f]))) self.assertTrue( all([ element in Semantics.derivable_set(baba, [a, d]) for element in [a, d] ])) self.assertEqual(2, len(Semantics.derivable_set(baba, [a, d])))
def test_BABA_framework_is_flat(self): ExampleFrameworks.valid_BABA_framework() self.assertRaises(Semantics.InvalidBABAException, ExampleFrameworks.invalid_non_flat_framework)
def test_BABA_language_covers_all_sentences(self): ExampleFrameworks.valid_BABA_framework() self.assertRaises(Semantics.InvalidBABAException, ExampleFrameworks.invalid_BABA_framework)
def test_empty_contraries(self): baba = ExampleFrameworks.with_no_contraries() contraries = Semantics.contraries(baba, [a, b, c, d, e, f]) self.assertEqual(0, len(contraries))
def test_preferred_larger_framework(self): baba = ExampleFrameworks.larger_framework() preferred_sets = Semantics.preferred(baba) self.assertIn(Semantics.SemanticSet([b, f, g, h, i]), preferred_sets)
def test_generate_admissible_larger_framework(self): baba = ExampleFrameworks.larger_framework() admissible_sets = Semantics.admissible(baba) self.assertIn(Semantics.SemanticSet([b, e, i]), admissible_sets) self.assertIn(Semantics.SemanticSet([b, e, g, h]), admissible_sets) self.assertIn(Semantics.SemanticSet([b, f, g, h, i]), admissible_sets)
def test_admissible_simple_framework(self): baba = ExampleFrameworks.valid_BABA_framework() self.assertTrue(Semantics.is_admissible(baba, [b, c]))
def test_contraries(self): baba = ExampleFrameworks.valid_BABA_framework() contraries = Semantics.contraries(baba, [b, c]) self.assertIn(d, contraries) self.assertIn(e, contraries) self.assertEqual(2, len(contraries))
def test_generate_attacks(self): baba = ExampleFrameworks.larger_framework() attacks = Semantics.get_attacks(baba, [b]) self.assertIn(Semantics.Attack(b, set([e, f, g, h])), attacks) self.assertIn(Semantics.Attack(b, set([e, f, i])), attacks) self.assertEqual(4, len(attacks))
def test_derivable_trivially(self): baba = ExampleFrameworks.valid_BABA_framework() self.assertTrue(Semantics.derivable(baba, b, [b])) self.assertTrue(Semantics.derivable(baba, c, [c])) self.assertFalse(Semantics.derivable(baba, b, [])) self.assertFalse(Semantics.derivable(baba, c, []))
def test_defends_with_empty_body_rules(self): baba = ExampleFrameworks.cow_framework() self.assertFalse(Semantics.defends(baba, [], ExampleFrameworks.not_HOC)) self.assertTrue(Semantics.defends(baba, [], ExampleFrameworks.not_FM))
def test_derivable_with_random_variables(self): baba = ExampleFrameworks.r_framework() self.assertTrue(Semantics.derivable(baba, a, [a, b])) self.assertFalse(Semantics.derivable(baba, a, [])) self.assertFalse(Semantics.derivable(baba, ExampleFrameworks._c, []))
import unittest from PythonSemantics import ExampleFrameworks, Semantics a = Semantics.Sentence('a') b = Semantics.Sentence('b') c = Semantics.Sentence('c') d = Semantics.Sentence('d') e = Semantics.Sentence('e') f = Semantics.Sentence('f') g = Semantics.Sentence('g') h = Semantics.Sentence('h') i = Semantics.Sentence('i') j = Semantics.Sentence('j') larger_baba = ExampleFrameworks.larger_framework() venice_baba = ExampleFrameworks.venice_framework() s_baba = ExampleFrameworks.s_framework() class TestSemantics(unittest.TestCase): def test_rule_initialises_with_no_body(self): head = 0 rule = Semantics.Rule(head) self.assertEqual(head, rule.head) self.assertEqual([], rule.body, []) def test_rule_initialises_with_body(self): head = 0 body = [1, 2, 3]