Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TranslationOption.cpp « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7cc40940183f40583f121f1c70b8db8d022ba6a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// $Id$

/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
***********************************************************************/

#include "TranslationOption.h"
#include "WordsBitmap.h"
#include "PhraseDictionary.h"
#include "GenerationDictionary.h"
#include "LMList.h"

using namespace std;


TranslationOption::TranslationOption(const WordsRange &wordsRange, const TargetPhrase &targetPhrase)
: m_phrase(targetPhrase)
,m_sourceWordsRange	(wordsRange)
{	// used by initial translation step

	// set score
	m_scoreGen		= 0;
	m_scoreTrans	= targetPhrase.GetTranslationScore();
	m_scoreBreakdown.PlusEquals(targetPhrase.GetScoreBreakdown());
}

TranslationOption::TranslationOption(const TranslationOption &copy, const TargetPhrase &targetPhrase)
: m_phrase(targetPhrase)
,m_sourceWordsRange	(copy.m_sourceWordsRange)
,m_scoreBreakdown(copy.m_scoreBreakdown)
{ // used in creating the next translation step
	m_scoreGen		= copy.GetGenerationScore();
	m_scoreTrans	= copy.GetTranslationScore() + targetPhrase.GetTranslationScore();
	
	m_scoreBreakdown.PlusEquals(targetPhrase.GetScoreBreakdown());
}

TranslationOption::TranslationOption(const TranslationOption &copy
																		, const Phrase &inputPhrase
																		, const GenerationDictionary *generationDictionary
																		, float generationScore
																		, float weight)
: m_phrase						(inputPhrase)
, m_sourceWordsRange	(copy.m_sourceWordsRange)
, m_scoreBreakdown(copy.m_scoreBreakdown)
{ // used in creating the next generation step

	m_scoreTrans	= copy.GetTranslationScore();
	m_scoreGen	= copy.GetGenerationScore() + generationScore * weight;

	m_scoreBreakdown.PlusEquals(generationDictionary, generationScore);
}

TranslationOption::TranslationOption(const WordsRange &wordsRange, const TargetPhrase &targetPhrase, int /*whatever*/)
: m_phrase(targetPhrase)
,m_sourceWordsRange	(wordsRange)
,m_scoreTrans(0)
,m_scoreGen(0)
,m_futureScore(0)
,m_ngramScore(0)
{ // used to create trans opt from unknown word
}

TranslationOption *TranslationOption::MergeTranslation(const TargetPhrase &targetPhrase) const
{
	if (m_phrase.IsCompatible(targetPhrase))
	{
		TargetPhrase mergePhrase(targetPhrase);
		mergePhrase.MergeFactors(m_phrase);
		TranslationOption *newTransOpt = new TranslationOption(*this, mergePhrase);
		return newTransOpt;
	}
	else
	{
		return NULL;
	}
}

TranslationOption *TranslationOption::MergeGeneration(const Phrase &inputPhrase
																	, const GenerationDictionary *generationDictionary
																	, float generationScore
																	, float weight) const
{
	if (m_phrase.IsCompatible(inputPhrase))
	{
		Phrase mergePhrase(inputPhrase);
		mergePhrase.MergeFactors(m_phrase);
		TranslationOption *newTransOpt = new TranslationOption(*this, mergePhrase, generationDictionary, generationScore, weight);
		return newTransOpt;
	}
	else
		return NULL;
}

bool TranslationOption::Overlap(const Hypothesis &hypothesis) const
{
	const WordsBitmap &bitmap = hypothesis.GetWordsBitmap();
	return bitmap.Overlap(GetSourceWordsRange());
}

void TranslationOption::CalcScore(const LMList &allLM, float weightWordPenalty)
{
	// LM scores
	m_ngramScore = 0;
	float retFullScore = 0;

	allLM.CalcScore(GetTargetPhrase(), retFullScore, m_ngramScore, &m_scoreBreakdown);
	// future score
	m_futureScore = retFullScore;

	size_t phraseSize = GetTargetPhrase().GetSize();
	m_futureScore += m_scoreTrans - phraseSize * weightWordPenalty;
}

TO_STRING_BODY(TranslationOption);

// friend
ostream& operator<<(ostream& out, const TranslationOption& possibleTranslation)
{
	out << possibleTranslation.GetTargetPhrase() 
			<< ", pC=" << possibleTranslation.GetTranslationScore()
			<< ", c=" << possibleTranslation.GetFutureScore()
			<< " [" << possibleTranslation.GetSourceWordsRange() << "]"
			<< possibleTranslation.GetScoreBreakdown();
	return out;
}