00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "rule_collection.h"
00021
00022 #include "syntax-common/pcfg.h"
00023
00024 #include <cmath>
00025
00026 namespace MosesTraining
00027 {
00028 namespace Syntax
00029 {
00030 namespace PCFG
00031 {
00032
00033 void RuleCollection::Add(std::size_t lhs, const std::vector<std::size_t> &rhs)
00034 {
00035 ++collection_[lhs][rhs];
00036 }
00037
00038 void RuleCollection::CreatePcfg(Pcfg &pcfg)
00039 {
00040 std::vector<std::size_t> key;
00041 for (const_iterator p = begin(); p != end(); ++p) {
00042 std::size_t lhs = p->first;
00043 const RhsCountMap &rhs_counts = p->second;
00044 std::size_t total = 0;
00045 for (RhsCountMap::const_iterator q = rhs_counts.begin();
00046 q != rhs_counts.end(); ++q) {
00047 total += q->second;
00048 }
00049 for (RhsCountMap::const_iterator q = rhs_counts.begin();
00050 q != rhs_counts.end(); ++q) {
00051 const std::vector<std::size_t> &rhs = q->first;
00052 std::size_t count = q->second;
00053 double score = std::log(static_cast<double>(count) /
00054 static_cast<double>(total));
00055 key.clear();
00056 key.push_back(lhs);
00057 key.insert(key.end(), rhs.begin(), rhs.end());
00058 pcfg.Add(key, score);
00059 }
00060 }
00061 }
00062
00063 }
00064 }
00065 }