Class: ASTDistance
- Inherits:
-
Object
- Object
- ASTDistance
- Defined in:
- lib/ast_distance.rb,
lib/ast_distance/node.rb,
lib/ast_distance/forest.rb,
lib/ast_distance/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
'0.1.0'
Instance Attribute Summary collapse
-
#left_ast ⇒ Object
readonly
Returns the value of attribute left_ast.
-
#right_ast ⇒ Object
readonly
Returns the value of attribute right_ast.
Class Method Summary collapse
Instance Method Summary collapse
- #forest_dist(f1, f2) ⇒ Object
-
#initialize(ast1, ast2) ⇒ ASTDistance
constructor
A new instance of ASTDistance.
- #tree_edit_distance ⇒ Object
Constructor Details
#initialize(ast1, ast2) ⇒ ASTDistance
Returns a new instance of ASTDistance.
12 13 14 15 16 17 18 |
# File 'lib/ast_distance.rb', line 12 def initialize(ast1, ast2) @left_ast = ast1 @right_ast = ast2 @f1 = Forest.ast_to_forest(ast: ast1) @f2 = Forest.ast_to_forest(ast: ast2) end |
Instance Attribute Details
#left_ast ⇒ Object (readonly)
Returns the value of attribute left_ast.
6 7 8 |
# File 'lib/ast_distance.rb', line 6 def left_ast @left_ast end |
#right_ast ⇒ Object (readonly)
Returns the value of attribute right_ast.
6 7 8 |
# File 'lib/ast_distance.rb', line 6 def right_ast @right_ast end |
Class Method Details
.tree_edit_distance(ast1, ast2) ⇒ Object
8 9 10 |
# File 'lib/ast_distance.rb', line 8 def self.tree_edit_distance(ast1, ast2) new(ast1, ast2).tree_edit_distance end |
Instance Method Details
#forest_dist(f1, f2) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ast_distance.rb', line 25 def forest_dist(f1, f2) return @memo[f1.id][f2.id] if @memo[f1.id][f2.id] return f2.node_count if f1.empty? return f1.node_count if f2.empty? f1_rmf = f1.rightmost_forest f2_rmf = f2.rightmost_forest a = forest_dist( Forest.new( left: f1_rmf.left, right: f1_rmf.right, node_index_tbl: f1.node_index_tbl ), Forest.new( left: f2_rmf.left, right: f2_rmf.right, node_index_tbl: f2_rmf.node_index_tbl ) ) b = forest_dist( Forest.new( left: f1.left, right: f1_rmf.left, node_index_tbl: f1.node_index_tbl ), Forest.new( left: f2.left, right: f2_rmf.left, node_index_tbl: f2.node_index_tbl ) ) c = f1.root_node.type == f2.root_node.type ? 0 : 1 d1 = a + b + c d2 = forest_dist( f1, Forest.new( left: f2.left, right: f2_rmf.right, node_index_tbl: f2.node_index_tbl ) ) + 1 d3 = forest_dist( Forest.new( left: f1.left, right: f1_rmf.right, node_index_tbl: f1.node_index_tbl ), f2 ) + 1 @memo[f1.id][f2.id] = [d1, d2, d3].min end |
#tree_edit_distance ⇒ Object
20 21 22 23 |
# File 'lib/ast_distance.rb', line 20 def tree_edit_distance pre_proc forest_dist(@f1, @f2) end |