00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "rule_extractor.h"
00021
00022 namespace MosesTraining
00023 {
00024 namespace Syntax
00025 {
00026 namespace PCFG
00027 {
00028
00029 RuleExtractor::RuleExtractor(Vocabulary &non_term_vocab)
00030 : non_term_vocab_(non_term_vocab)
00031 {
00032 }
00033
00034 void RuleExtractor::Extract(const SyntaxTree &tree, RuleCollection &rc) const
00035 {
00036 if (tree.IsLeaf() || tree.children()[0]->IsLeaf()) {
00037 return;
00038 }
00039
00040 std::size_t lhs = non_term_vocab_.Insert(tree.value().label);
00041 std::vector<std::size_t> rhs;
00042
00043 const std::vector<SyntaxTree *> &children = tree.children();
00044 rhs.reserve(children.size());
00045 for (std::vector<SyntaxTree *>::const_iterator p(children.begin());
00046 p != children.end(); ++p) {
00047 const SyntaxTree &child = **p;
00048 rhs.push_back(non_term_vocab_.Insert(child.value().label));
00049 Extract(child, rc);
00050 }
00051 rc.Add(lhs, rhs);
00052 }
00053
00054 }
00055 }
00056 }