Package jptests :: Package formalisms :: Package music_halfspan :: Module harmstruct
[hide private]
[frames] | no frames]

Source Code for Module jptests.formalisms.music_halfspan.harmstruct

  1  """Unit tests for jazzparser.formalisms.music_halfspan.harmstruct. 
  2   
  3  """ 
  4  """ 
  5  ============================== License ======================================== 
  6   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
  7    
  8   This file is part of The Jazz Parser. 
  9    
 10   The Jazz Parser is free software: you can redistribute it and/or modify 
 11   it under the terms of the GNU General Public License as published by 
 12   the Free Software Foundation, either version 3 of the License, or 
 13   (at your option) any later version. 
 14    
 15   The Jazz Parser is distributed in the hope that it will be useful, 
 16   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 18   GNU General Public License for more details. 
 19    
 20   You should have received a copy of the GNU General Public License 
 21   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
 22   
 23  ============================ End license ====================================== 
 24   
 25  """ 
 26  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
 27   
 28  import unittest, os 
 29  from jazzparser import settings 
 30   
 31  from jazzparser.formalisms.music_halfspan.harmstruct import semantics_to_dependency_trees 
 32  from jazzparser.formalisms.music_halfspan.semantics import semantics_from_string 
 33   
34 -class TestTreeBuilder(unittest.TestCase):
35 """ 36 Tests for building trees from semantics using 37 C{semantics_to_dependency_trees}. 38 39 """
40 - def test_coordinate(self):
41 # Build a semantics of a single point 42 sem = semantics_from_string("[<3,1>]") 43 # Build a tree for this 44 trees = semantics_to_dependency_trees(sem) 45 # Should be only one tree 46 self.assertEqual(len(trees), 1) 47 # Should just contain the root node corresponding to the point 48 self.assertEqual(len(trees[0]), 1)
49
50 - def test_coordinates(self):
51 # Build a semantics of two points 52 sem = semantics_from_string("[<3,1>, <2,2>]") 53 # Build a tree for this 54 trees = semantics_to_dependency_trees(sem) 55 # Should be two trees 56 self.assertEqual(len(trees), 2) 57 # Should just contain the root node each corresponding to the points 58 self.assertEqual(len(trees[0]), 1) 59 self.assertEqual(len(trees[1]), 1)
60
61 - def test_predicates(self):
62 # Build a semantics of a leftonto/rightonto with resolution 63 sem = semantics_from_string("[leftonto(<0,0>)]") 64 # Build a tree for this 65 trees = semantics_to_dependency_trees(sem) 66 # Should have two nodes, leaf should be leftonto 67 self.assertEqual(len(trees[0]), 2) 68 self.assertEqual(trees[0][0].label, "leftonto") 69 70 sem = semantics_from_string("[rightonto(<0,0>)]") 71 trees = semantics_to_dependency_trees(sem) 72 # Should have two nodes, leaf should be rightonto 73 self.assertEqual(len(trees[0]), 2) 74 self.assertEqual(trees[0][0].label, "rightonto") 75 76 # Try multiple applications 77 sem = semantics_from_string("[leftonto(leftonto(leftonto(<0,0>)))]") 78 trees = semantics_to_dependency_trees(sem) 79 # Should have 4 nodes 80 self.assertEqual(len(trees[0]), 4)
81
82 - def test_coordination(self):
83 # Build a semantics of a coordination 84 sem = semantics_from_string(r"[((\$x.leftonto($x)) & (\$y.leftonto($y)) leftonto(<0,0>))]") 85 trees = semantics_to_dependency_trees(sem) 86 # Should look like this: 87 # <(0,0)/(0,0)>(leftonto(leftonto leftonto)) 88 self.assertEqual(len(trees), 1) 89 tree = trees[0] 90 self.assertEqual(tree.root.label, (0,0)) 91 self.assertEqual(tree[0].label, "leftonto") 92 self.assertEqual(tree[0][0].label, "leftonto") 93 self.assertEqual(tree[0][1].label, "leftonto")
94
96 # Call Me Irresponsible cadence 97 sem = semantics_from_string(r"[<0,0>, "\ 98 r"(((\$y.(((\$x.leftonto(leftonto(leftonto(leftonto(leftonto($x)))))) "\ 99 r"& (\$x.leftonto(leftonto($x)))) leftonto($y))) "\ 100 r"& (\$x.leftonto(leftonto($x)))) <0,0>)]") 101 trees = semantics_to_dependency_trees(sem) 102 self.assertEqual(len(trees), 2) 103 # One node in first tree 104 self.assertEqual(len(trees[0]), 1) 105 # Second tree is the hard one! 106 # Should look like this: 107 # <(0,0)/(0,0)> 108 # (leftonto( 109 # leftonto(leftonto(leftonto(leftonto(leftonto)))) 110 # leftonto(leftonto)) 111 # leftonto(leftonto)) 112 # Check some things about the structure 113 tree = trees[1].root 114 # First split (just after root) 115 self.assertEqual(len(tree), 2) 116 # Other split 117 self.assertEqual(len(tree[0]), 2) 118 # Others should not split 119 self.assertEqual(len(tree[0][0]), 1) 120 self.assertEqual(len(tree[0][0][0]), 1) 121 self.assertEqual(len(tree[0][0][0][0]), 1) 122 self.assertEqual(len(tree[0][0][0][0][0]), 1) 123 # This is the lowest leaf 124 self.assertTrue(tree[0][0][0][0][0][0].is_leaf())
125