Commits

David McClosky  committed d4785fd

Merge in Matt Gerber's new options for best-parses.
"...changes for writing out head indexes (best-parses.cc and tree.h). I added two additional printing modes ("-m" switch), one for printing syntactic heads and one for printing semantic heads."

  • Participants
  • Parent commits 13e16d3

Comments (0)

Files changed (2)

File second-stage/programs/features/best-parses.cc

   "    0 print 1-best tree,\n"
   "    1 print ranked parses,\n"
   "    2 print feature counts,\n"
+  "    3 print 1-best tree with syntactic heads,\n"
+  "    4 print 1-best tree with semantic heads,\n"
   "\n"
   " feat-defs.bz2 is a feature definition file produced by extract-spfeatures, and\n"
   " feat-weights.bz2 is a feature weight file\n"
     case 2:
       fcps.write_features_debug(s, weights, std::cout);
       break;
+    case 3:
+      write_tree_noquote_root_with_heads(std::cout, fcps.best_parse(s, weights), true);
+      std::cout << std::endl;
+      break;
+    case 4:
+      write_tree_noquote_root_with_heads(std::cout, fcps.best_parse(s, weights), false);
+      std::cout << std::endl;
+      break;
     default:
       std::cerr << "## Error: unknown mode = " << mode << std::endl;
       exit(EXIT_FAILURE);

File second-stage/programs/features/tree.h

     s << t->label.cat.string_reference();
 }
 
+template <typename label_type>
+int get_head_num(const tree_node<label_type>* t, bool syntactic_head)
+{
+  assert(t);
+
+  if(t->is_preterminal())
+    return 0;
+
+  const tree_node<label_type>* head_child = syntactic_head ? tree_syntacticHeadChild(t): tree_semanticHeadChild(t);
+
+  int head_num = 0;
+  tree_node<label_type> *p = t->child;
+  while(p != head_child)
+    {
+      p = p->next;
+      ++head_num;
+    }
+
+  return head_num;
+}
+
+template <typename label_type>
+void write_tree_noquote_root_with_heads(std::ostream& s, const tree_node<label_type>* t, bool syntactic_heads)
+{
+  assert(t);
+  assert(t->label.is_root());
+
+  if (t->child) {
+
+    s << '(' << t->label.cat.string_reference() << " H:" << get_head_num(t, syntactic_heads);
+    
+    for (tree_node<label_type> *p = t->child; p; p = p->next) {
+      s << ' ';
+      write_tree_with_heads(s, p, syntactic_heads);
+    }
+
+    s << ')';
+  }
+  else 
+    s << t->label.cat.string_reference();
+}
+
+template <typename label_type>
+static void write_tree_with_heads(std::ostream& s, const tree_node<label_type>* t, bool syntactic_heads)
+{
+  assert(t);
+
+  if (t->child) {
+
+    s << '(' << t->label << " H:" << get_head_num(t, syntactic_heads);
+    
+    for (tree_node<label_type> *p = t->child; p; p = p->next) {
+      s << ' ';
+      write_tree_with_heads(s, p, syntactic_heads);
+    }
+
+    s << ')';
+  }
+  else 
+    s << t->label;
+}
 
 template <typename label_type>
 void write_prolog_label(std::ostream& os, const label_type& l)