bracket_notation 1.0.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. data/CHANGELOG +8 -0
  2. data/Manifest +17 -1
  3. data/README.rdoc +31 -7
  4. data/Rakefile +2 -1
  5. data/bracket_notation.gemspec +9 -6
  6. data/init.rb +1 -1
  7. data/lib/bracket_notation.rb +3 -1
  8. data/lib/bracket_notation/evaluator.rb +2 -1
  9. data/lib/bracket_notation/expressions.rb +1 -1
  10. data/lib/bracket_notation/expressions/expression.rb +1 -1
  11. data/lib/bracket_notation/expressions/identifier.rb +1 -1
  12. data/lib/bracket_notation/expressions/terminal.rb +1 -1
  13. data/lib/bracket_notation/geometry.rb +31 -0
  14. data/lib/bracket_notation/geometry/point.rb +69 -0
  15. data/lib/bracket_notation/geometry/rect.rb +64 -0
  16. data/lib/bracket_notation/geometry/size.rb +69 -0
  17. data/lib/bracket_notation/parser.rb +1 -1
  18. data/lib/bracket_notation/scanner.rb +1 -1
  19. data/lib/bracket_notation/token.rb +1 -1
  20. data/lib/bracket_notation/version.rb +3 -3
  21. data/lib/bracket_notation/views.rb +33 -0
  22. data/lib/bracket_notation/views/branch.rb +40 -0
  23. data/lib/bracket_notation/views/leaf.rb +34 -0
  24. data/lib/bracket_notation/views/node.rb +152 -0
  25. data/lib/bracket_notation/views/tree.rb +181 -0
  26. data/test/functional/evaluator_test.rb +1 -1
  27. data/test/functional/node_test.rb +132 -0
  28. data/test/functional/parser_test.rb +1 -1
  29. data/test/functional/scanner_test.rb +1 -1
  30. data/test/functional/tree_test.rb +67 -0
  31. data/test/integration/layout_test.rb +151 -0
  32. data/test/integration/parsing_test.rb +1 -1
  33. data/test/test_helper.rb +1 -1
  34. data/test/unit/expression_test.rb +1 -1
  35. data/test/unit/point_test.rb +69 -0
  36. data/test/unit/rect_test.rb +58 -0
  37. data/test/unit/size_test.rb +69 -0
  38. data/test/unit/token_test.rb +1 -1
  39. metadata +48 -7
@@ -23,7 +23,7 @@
23
23
  # and small portions of his code have been incorporated in the parser.
24
24
  #
25
25
  # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
- # Copyright:: Copyright (c) 2010 Cody Brimhall
26
+ # Copyright:: Copyright (c) 2010-2011 Cody Brimhall
27
27
  # License:: Distributed under the terms of the GNU General Public License, v. 3
28
28
 
29
29
  require 'test_helper'
@@ -0,0 +1,132 @@
1
+ #--
2
+ # This file is part of BracketNotation.
3
+ #
4
+ # BracketNotation is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # BracketNotation is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with BracketNotation. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+ # BracketNotation is a parser for generating syntax trees from sentences
18
+ # annotated with the kind of bracket notation that is commonly used by
19
+ # linguists. The result is a tree structure with nodes that describe the phrases
20
+ # and constituents of the sentence.
21
+ #
22
+ # BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
23
+ # and small portions of his code have been incorporated in the parser.
24
+ #
25
+ # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
+ # Copyright:: Copyright (c) 2010-2011 Cody Brimhall
27
+ # License:: Distributed under the terms of the GNU General Public License, v. 3
28
+
29
+ require 'test_helper'
30
+
31
+ class NodeTest < Test::Unit::TestCase
32
+ context "any node" do
33
+ setup do
34
+ @tree = BracketNotation::View::Tree.new("[A [B [E] [F]] [C] [D]]")
35
+ @root = BracketNotation::View::Node.new(@tree, "A")
36
+ @child1 = BracketNotation::View::Node.new(@tree, "B")
37
+ @child2 = BracketNotation::View::Node.new(@tree, "C")
38
+ @child3 = BracketNotation::View::Node.new(@tree, "D")
39
+ @child4 = BracketNotation::View::Node.new(@tree, "E")
40
+ @child5 = BracketNotation::View::Node.new(@tree, "F")
41
+
42
+ @child1.children += [@child4, @child5]
43
+ @child4.parent = @child1
44
+ @child5.parent = @child1
45
+
46
+ @root.children += [@child1, @child2, @child3]
47
+ @child1.parent = @root
48
+ @child2.parent = @root
49
+ @child3.parent = @root
50
+
51
+ @tree.root = @root
52
+
53
+ @root.rect = BracketNotation::Geometry::Rect.new(BracketNotation::Geometry::Point.new(0,0), BracketNotation::Geometry::Size.new(42,42))
54
+ end
55
+
56
+ should "know its parent" do
57
+ assert_same @child1.parent, @root
58
+ end
59
+
60
+ should "know its children" do
61
+ assert_equal @root.children, [@child1, @child2, @child3]
62
+ assert_equal @child1.children, [@child4, @child5]
63
+ assert_equal @child4.children, []
64
+ end
65
+
66
+ should "know its ancestors" do
67
+ assert_equal @root.ancestors, []
68
+ assert_equal @child1.ancestors, [@root]
69
+ assert_equal @child4.ancestors, [@child1, @root]
70
+ end
71
+
72
+ should "know its left sibling" do
73
+ assert_same @child2.left_sibling, @child1
74
+ assert_nil @child1.left_sibling
75
+ end
76
+
77
+ should "know its right sibling" do
78
+ assert_same @child2.right_sibling, @child3
79
+ assert_nil @child3.right_sibling
80
+ end
81
+
82
+ should "know its tree" do
83
+ assert_same @root.tree, @tree
84
+ assert_same @child1.tree, @tree
85
+ assert_same @child4.tree, @tree
86
+ end
87
+
88
+ should "know its corners" do
89
+ assert_equal @root.corner_top_left, BracketNotation::Geometry::Point.new(0,0)
90
+ assert_equal @root.corner_top_right, BracketNotation::Geometry::Point.new(42,0)
91
+ assert_equal @root.corner_bottom_right, BracketNotation::Geometry::Point.new(42,42)
92
+ assert_equal @root.corner_bottom_left, BracketNotation::Geometry::Point.new(0,42)
93
+ end
94
+
95
+ should "know its side midpoints" do
96
+ assert_equal @root.side_middle_top, BracketNotation::Geometry::Point.new(21,0)
97
+ assert_equal @root.side_middle_right, BracketNotation::Geometry::Point.new(42, 21)
98
+ assert_equal @root.side_middle_bottom, BracketNotation::Geometry::Point.new(21,42)
99
+ assert_equal @root.side_middle_left, BracketNotation::Geometry::Point.new(0, 21)
100
+ end
101
+ end
102
+
103
+ context "a branch node" do
104
+ setup do
105
+ @tree = BracketNotation::View::Tree.new("[A [B [E] [F]] [C] [D]]")
106
+ @root = BracketNotation::View::Branch.new(@tree, "A")
107
+ @child1 = BracketNotation::View::Branch.new(@tree, "B")
108
+ @child2 = BracketNotation::View::Leaf.new(@tree, "C")
109
+ @child3 = BracketNotation::View::Leaf.new(@tree, "D")
110
+ @child4 = BracketNotation::View::Leaf.new(@tree, "E")
111
+ @child5 = BracketNotation::View::Leaf.new(@tree, "F")
112
+
113
+ @child1.children += [@child4, @child5]
114
+ @child4.parent = @child1
115
+ @child5.parent = @child1
116
+
117
+ @root.children += [@child1, @child2, @child3]
118
+ @child1.parent = @root
119
+ @child2.parent = @root
120
+ @child3.parent = @root
121
+
122
+ @tree.root = @root
123
+
124
+ @root.rect = BracketNotation::Geometry::Rect.new(BracketNotation::Geometry::Point.new(0,0), BracketNotation::Geometry::Size.new(42,42))
125
+ end
126
+
127
+ should "start out unrolled" do
128
+ assert !@root.roll_up
129
+ assert !@child1.roll_up
130
+ end
131
+ end
132
+ end
@@ -23,7 +23,7 @@
23
23
  # and small portions of his code have been incorporated in the parser.
24
24
  #
25
25
  # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
- # Copyright:: Copyright (c) 2010 Cody Brimhall
26
+ # Copyright:: Copyright (c) 2010-2011 Cody Brimhall
27
27
  # License:: Distributed under the terms of the GNU General Public License, v. 3
28
28
 
29
29
  require 'test_helper'
@@ -23,7 +23,7 @@
23
23
  # and small portions of his code have been incorporated in the parser.
24
24
  #
25
25
  # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
- # Copyright:: Copyright (c) 2010 Cody Brimhall
26
+ # Copyright:: Copyright (c) 2010-2011 Cody Brimhall
27
27
  # License:: Distributed under the terms of the GNU General Public License, v. 3
28
28
 
29
29
  require 'test_helper'
@@ -0,0 +1,67 @@
1
+ #--
2
+ # This file is part of BracketNotation.
3
+ #
4
+ # BracketNotation is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # BracketNotation is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with BracketNotation. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+ # BracketNotation is a parser for generating syntax trees from sentences
18
+ # annotated with the kind of bracket notation that is commonly used by
19
+ # linguists. The result is a tree structure with nodes that describe the phrases
20
+ # and constituents of the sentence.
21
+ #
22
+ # BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
23
+ # and small portions of his code have been incorporated in the parser.
24
+ #
25
+ # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
+ # Copyright:: Copyright (c) 2010-2011 Cody Brimhall
27
+ # License:: Distributed under the terms of the GNU General Public License, v. 3
28
+
29
+ require 'test_helper'
30
+
31
+ class TreeTest < Test::Unit::TestCase
32
+ include BracketNotation::View
33
+
34
+ context "a tree" do
35
+ setup do
36
+ @tree = Tree.new("[A [B [E] [F]] [C] [D]]")
37
+ @root = Node.new(@tree, "A")
38
+ @child1 = Node.new(@tree, "B")
39
+ @child2 = Node.new(@tree, "C")
40
+ @child3 = Node.new(@tree, "D")
41
+ @child4 = Node.new(@tree, "E")
42
+ @child5 = Node.new(@tree, "F")
43
+
44
+ @child1.children += [@child4, @child5]
45
+ @root.children += [@child1, @child2, @child3]
46
+ @tree.root = @root
47
+ end
48
+
49
+ should "traverse preorder" do
50
+ buffer = ""
51
+ @tree.traverse(:order => :preorder) {|node, depth| buffer << node.content }
52
+ assert_equal buffer, "ABEFCD"
53
+ end
54
+
55
+ should "traverse postorder" do
56
+ buffer = ""
57
+ @tree.traverse(:order => :postorder) {|node, depth| buffer << node.content }
58
+ assert_equal buffer, "EFBCDA"
59
+ end
60
+
61
+ should "traverse breadthfirst" do
62
+ buffer = ""
63
+ @tree.traverse(:order => :breadthfirst) {|node, depth| buffer << node.content }
64
+ assert_equal buffer, "ABCDEF"
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,151 @@
1
+ #--
2
+ # This file is part of BracketNotation.
3
+ #
4
+ # BracketNotation is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # BracketNotation is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with BracketNotation. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+ # BracketNotation is a parser for generating syntax trees from sentences
18
+ # annotated with the kind of bracket notation that is commonly used by
19
+ # linguists. The result is a tree structure with nodes that describe the phrases
20
+ # and constituents of the sentence.
21
+ #
22
+ # BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
23
+ # and small portions of his code have been incorporated in the parser.
24
+ #
25
+ # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
+ # Copyright:: Copyright (c) 2010-2011 Cody Brimhall
27
+ # License:: Distributed under the terms of the GNU General Public License, v. 3
28
+
29
+ require 'test_helper'
30
+
31
+ class LayoutTest < Test::Unit::TestCase
32
+ include BracketNotation
33
+ include BracketNotation::View
34
+ include BracketNotation::Geometry
35
+
36
+ SANS_FONT_NAME = "Helvetica"
37
+ SERIF_FONT_NAME = "Georgia"
38
+ DEFAULT_METRICS = {:large => {:font_point_size => 40,
39
+ :tree_padding => 20,
40
+ :node_h_margin => 50,
41
+ :node_v_margin => 30,
42
+ :node_padding => 10},
43
+
44
+ :small => {:font_point_size => 12,
45
+ :tree_padding => 20,
46
+ :node_h_margin => 20,
47
+ :node_v_margin => 15,
48
+ :node_padding => 10}}
49
+
50
+ context "a single-node tree" do
51
+ setup do
52
+ @input = "[S]"
53
+ @tree = Tree.new(@input)
54
+ @root = Branch.new(@tree, "S")
55
+ @tree.root = @root
56
+ end
57
+
58
+ should "layout correctly at large point sizes" do
59
+ configure_tree(@tree, :size => :large)
60
+ @tree.compute_layout
61
+
62
+ assert_equal Rect.new(20, 20, 24, 60), @root.rect
63
+ end
64
+
65
+ should "layout correctly at small point sizes" do
66
+ configure_tree(@tree, :size => :small)
67
+ @tree.compute_layout
68
+
69
+ assert_equal Rect.new(20, 20, 7, 32), @root.rect, "The root rect is wrong"
70
+ end
71
+ end
72
+
73
+ context "a two-node tree" do
74
+ setup do
75
+ @input = "[S [NP]]"
76
+ @tree = Tree.new(@input)
77
+ @root = Branch.new(@tree, "S")
78
+ @child = Branch.new(@tree, "NP")
79
+
80
+ @child.parent = @root
81
+ @root.children << @child
82
+ @tree.root = @root
83
+ end
84
+
85
+ should "layout correctly at large point sizes" do
86
+ configure_tree(@tree, :size => :large)
87
+ @tree.compute_layout
88
+
89
+ assert_equal Rect.new(34, 20, 24, 60), @root.rect, "The root rect is wrong"
90
+ assert_equal Rect.new(20, 110, 52, 60), @child.rect, "The child rect is wrong"
91
+ end
92
+
93
+ should "layout correctly at small point sizes" do
94
+ configure_tree(@tree, :size => :small)
95
+ @tree.compute_layout
96
+
97
+ assert_equal Rect.new(25, 20, 7, 32), @root.rect, "The root rect is wrong"
98
+ assert_equal Rect.new(20, 67, 16, 32), @child.rect, "The child rect is wrong"
99
+ end
100
+ end
101
+
102
+ context "a three-node tree" do
103
+ setup do
104
+ @input = "[S [NP] [VP]]"
105
+ @tree = Tree.new(@input)
106
+ @root = Branch.new(@tree, "S")
107
+ @child1 = Branch.new(@tree, "NP")
108
+ @child2 = Branch.new(@tree, "VP")
109
+
110
+ @child1.parent = @root
111
+ @child2.parent = @root
112
+ @root.children += [@child1, @child2]
113
+ @tree.root = @root
114
+ end
115
+
116
+ should "layout correctly at large point sizes" do
117
+ configure_tree(@tree, :size => :large)
118
+ @tree.compute_layout
119
+
120
+ assert_equal Rect.new(85, 20, 24, 60), @root.rect, "The root rect is wrong"
121
+ assert_equal Rect.new(20, 110, 52, 60), @child1.rect, "The first child's rect is wrong"
122
+ assert_equal Rect.new(122, 110, 52, 60), @child2.rect, "The second child's rect is wrong"
123
+ end
124
+
125
+ should "layout correctly at small point sizes" do
126
+ configure_tree(@tree, :size => :small)
127
+ @tree.compute_layout
128
+
129
+ assert_equal Rect.new(43, 20, 7, 32), @root.rect, "The root rect is wrong"
130
+ assert_equal Rect.new(20, 67, 16, 32), @child1.rect, "The first child's rect is wrong"
131
+ assert_equal Rect.new(56, 67, 16, 32), @child2.rect, "The second child's rect is wrong"
132
+ end
133
+ end
134
+
135
+ private
136
+
137
+ def configure_tree(tree, options)
138
+ font_name = options[:font_name] || SANS_FONT_NAME
139
+ size = options[:size] || :small
140
+ metrics = DEFAULT_METRICS[size]
141
+
142
+ tree.font_name = font_name
143
+ tree.font_point_size = metrics[:font_point_size]
144
+ tree.tree_padding = metrics[:tree_padding]
145
+ tree.node_h_margin = metrics[:node_h_margin]
146
+ tree.node_v_margin = metrics[:node_v_margin]
147
+ tree.node_padding = metrics[:node_padding]
148
+
149
+ return tree
150
+ end
151
+ end
@@ -23,7 +23,7 @@
23
23
  # and small portions of his code have been incorporated in the parser.
24
24
  #
25
25
  # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
- # Copyright:: Copyright (c) 2010 Cody Brimhall
26
+ # Copyright:: Copyright (c) 2010-2011 Cody Brimhall
27
27
  # License:: Distributed under the terms of the GNU General Public License, v. 3
28
28
 
29
29
  require 'test_helper'
@@ -23,7 +23,7 @@
23
23
  # and small portions of his code have been incorporated in the parser.
24
24
  #
25
25
  # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
- # Copyright:: Copyright (c) 2010 Cody Brimhall
26
+ # Copyright:: Copyright (c) 2010-2011 Cody Brimhall
27
27
  # License:: Distributed under the terms of the GNU General Public License, v. 3
28
28
 
29
29
  require 'test/unit'
@@ -23,7 +23,7 @@
23
23
  # and small portions of his code have been incorporated in the parser.
24
24
  #
25
25
  # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
- # Copyright:: Copyright (c) 2010 Cody Brimhall
26
+ # Copyright:: Copyright (c) 2010-2011 Cody Brimhall
27
27
  # License:: Distributed under the terms of the GNU General Public License, v. 3
28
28
 
29
29
  require 'test_helper'
@@ -0,0 +1,69 @@
1
+ #--
2
+ # This file is part of BracketNotation.
3
+ #
4
+ # BracketNotation is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # BracketNotation is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with BracketNotation. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+ # BracketNotation is a parser for generating syntax trees from sentences
18
+ # annotated with the kind of bracket notation that is commonly used by
19
+ # linguists. The result is a tree structure with nodes that describe the phrases
20
+ # and constituents of the sentence.
21
+ #
22
+ # BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
23
+ # and small portions of his code have been incorporated in the parser.
24
+ #
25
+ # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
+ # Copyright:: Copyright (c) 2010-2011 Cody Brimhall
27
+ # License:: Distributed under the terms of the GNU General Public License, v. 3
28
+
29
+ require 'test_helper'
30
+
31
+ class PointTest < Test::Unit::TestCase
32
+ include BracketNotation::Geometry
33
+
34
+ context "a point" do
35
+ setup do
36
+ @point1 = Point.new
37
+ @point2 = Point.new(42, 42)
38
+ @point3 = Point.new(42, 42)
39
+ @point4 = Point.new(42, 0)
40
+ @point5 = Point.new(0, 42)
41
+ end
42
+
43
+ should "initiaize to {0,0}" do
44
+ assert @point1.x == 0 and @point1.y == 0
45
+ end
46
+
47
+ should "be immutable" do
48
+ assert_raise(NoMethodError) { @point1.x = 0 }
49
+ assert_raise(NoMethodError) { @point1.y = 0 }
50
+ end
51
+
52
+ should "base equality on coordinates" do
53
+ assert_not_equal @point1, @point2
54
+ assert_equal @point2, @point3
55
+ end
56
+
57
+ should "create new point by adding to X" do
58
+ assert_equal @point1.point_by_adding_to_x(42), @point4
59
+ end
60
+
61
+ should "create new point by adding to y" do
62
+ assert_equal @point1.point_by_adding_to_y(42), @point5
63
+ end
64
+
65
+ should "create new point by adding to x and y" do
66
+ assert_equal @point1.point_by_adding_to_x_and_y(42, 42), @point2
67
+ end
68
+ end
69
+ end