diff options
author | Hieu Hoang <hieu@hoang.co.uk> | 2014-06-06 13:40:40 +0400 |
---|---|---|
committer | Hieu Hoang <hieu@hoang.co.uk> | 2014-06-06 13:40:40 +0400 |
commit | e25e7b69be120c104bde9454a1b90bbfeb2059f6 (patch) | |
tree | d2e247972b5e839ba00c7cbc9bb14ac6fdf3c166 /moses/PP | |
parent | d95bd5bdaed04d4a7cb554d3786208273172e56a (diff) |
span length
Diffstat (limited to 'moses/PP')
-rw-r--r-- | moses/PP/SpanLengthPhraseProperty.cpp | 43 | ||||
-rw-r--r-- | moses/PP/SpanLengthPhraseProperty.h | 4 |
2 files changed, 44 insertions, 3 deletions
diff --git a/moses/PP/SpanLengthPhraseProperty.cpp b/moses/PP/SpanLengthPhraseProperty.cpp index 273637170..20acb17e4 100644 --- a/moses/PP/SpanLengthPhraseProperty.cpp +++ b/moses/PP/SpanLengthPhraseProperty.cpp @@ -18,6 +18,10 @@ SpanLengthPhraseProperty::SpanLengthPhraseProperty(const std::string &value) float count = Scan<float>(toks[i + 1]); Populate(span, count); } + + // totals + CalcTotals(m_source); + CalcTotals(m_target); } void SpanLengthPhraseProperty::Populate(const string &span, float count) @@ -37,9 +41,44 @@ void SpanLengthPhraseProperty::Populate(const std::vector<size_t> &toks, float c m_source.resize(ntInd + 1); m_target.resize(ntInd + 1); } - m_source[ntInd][sourceLength] = count; - m_target[ntInd][targetLength] = count; + m_source[ntInd].first[sourceLength] = count; + m_target[ntInd].first[targetLength] = count; + +} + +void SpanLengthPhraseProperty::CalcTotals(Vec &vec) +{ + for (size_t i = 0; i < vec.size(); i = i + 2) { + float total = 0; + + const Map &map = vec[i].first; + Map::const_iterator iter; + for (iter = map.begin(); iter != map.end(); ++iter) { + float count = iter->second; + total += count; + } + + vec[i].second = total; + } +} + +float SpanLengthPhraseProperty::GetProb(size_t ntInd, size_t sourceWidth, float smoothing) const +{ + float count; + + const std::pair<Map, float> &data = m_source[ntInd]; + const Map &map = data.first; + Map::const_iterator iter = map.find(sourceWidth); + if (iter == map.end()) { + count = 0; + } + else { + count = iter->second; + } + count += smoothing; + float ret = count / (data.second + smoothing * map.size()); + return ret; } } diff --git a/moses/PP/SpanLengthPhraseProperty.h b/moses/PP/SpanLengthPhraseProperty.h index 94e7ab391..a45974802 100644 --- a/moses/PP/SpanLengthPhraseProperty.h +++ b/moses/PP/SpanLengthPhraseProperty.h @@ -14,14 +14,16 @@ class SpanLengthPhraseProperty : public PhraseProperty public: SpanLengthPhraseProperty(const std::string &value); + float GetProb(size_t ntInd, size_t sourceWidth, float smoothing) const; protected: // fractional counts typedef std::map<size_t, float> Map; - typedef std::vector<Map> Vec; + typedef std::vector<std::pair<Map, float> > Vec; Vec m_source, m_target; void Populate(const std::string &span, float count); void Populate(const std::vector<size_t> &toks, float count); + void CalcTotals(Vec &vec); }; } // namespace Moses |