📄 hypotheses.cpp
字号:
/** Hypotheses.cpp - English sentence is created by hypotheses, which stores* translation informations such as feature functions, Chinese words covered so far, etc.** Copyright (C) 2006 by Zhongjun He <zjhe@ict.ac.cn> Multilingual Interaction Technology and Evaluation Laboratory, ICT, CAS* Begin : 04/17/2006* Last Change : 04/17/2006** This program 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 program 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 General Public License for more details.** You should have received a copy of the GNU Lesser General Public* License along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/#include "Hypotheses.h"
/************************************************************************/
/* AddArc */
/************************************************************************/
AddArc::AddArc()
{
}
AddArc::AddArc(const Hypotheses &hp)
{
PreStack = hp.PreStack;
PreStackNum = hp.PreStackNum;
feat = hp.feat;
english = hp.CurEnglishTranslation;
}
AddArc::AddArc(const int p1,const int p2, const Feature &f1, const string &e1)
{
PreStack = p1;
PreStackNum = p2;
feat = f1;
english = e1;
}
/************************************************************************/
/*overload */
/************************************************************************/
AddArc& AddArc::operator =(const AddArc &right)
{
PreStack = right.PreStack;
PreStackNum = right.PreStackNum;
feat = right.feat;
english = right.english;
return *this;
}
bool AddArc::operator <(const AddArc &right)const
{
return feat < right.feat;
}
bool AddArc::operator >(const AddArc &right)const
{
return feat > right.feat;
}
bool AddArc::operator ==(const AddArc &right)const
{
return feat == right.feat;
}
/************************************************************************/
/* show */
/************************************************************************/
void AddArc::Show(ostream &out)
{
out<<"("<<PreStack<<","<<PreStackNum<<")"<<english<<endl;
feat.Show(out);
out<<endl;
}/************************************************************************/
/* construction function */
/************************************************************************/
Hypotheses::Hypotheses()
{
}
//n is the length of Chinese sentence
Hypotheses::Hypotheses(int n)
{
PreStack = PreStackNum = -1;
CoveredWord.resize(n);
fill(CoveredWord.begin(),CoveredWord.end(),0);
CoveredNumber = 0;
LastEnglishWord = "";
EndPosOfLastPhrase = -1;
CurBegEnd = make_pair(-1,-1);
CurEnglishTranslation = "";
FutureCost = 0.0;
EstimateProb = 0.0;
}
Hypotheses::Hypotheses(int Pstack, int Pnum,const pair<int,int> Covered, const string &PhraseTrans,
const Feature &ft, const Hypotheses &Father,int lmngram)
{
PreStack = Pstack;
PreStackNum = Pnum;
CoveredWord =Father.CoveredWord;
int i;
for (i=Covered.first; i<=Covered.second; i++)
CoveredWord[i] = 1;
CoveredNumber = Father.CoveredNumber + Covered.second - Covered.first + 1;
EndPosOfLastPhrase = Father.CurBegEnd.second;
CurBegEnd = Covered;
CurEnglishTranslation = PhraseTrans;
feat = ft;
LastEnglishWord = Father.LastEnglishWord + " " + CurEnglishTranslation;
vector<string> temp;
string x;
istrstream b(LastEnglishWord.c_str());
while (b>>x)
temp.push_back(x);
int n = lmngram-1;
if (temp.size() >= n)
{
LastEnglishWord="";
for (i=temp.size()-n; i<temp.size();i++)
LastEnglishWord = LastEnglishWord + temp[i] + " ";
if (LastEnglishWord.size()>0)
LastEnglishWord.erase(LastEnglishWord.size()-1,1);
}
}
/************************************************************************/
/* get Future Cost */
/************************************************************************/
void Hypotheses::GetFutureCost(map<pair<int,int>,double> &FutureCostTable)
{
FutureCost = 0.0;
int i=0, j=0;
while (j < CoveredWord.size())
{
if (CoveredWord[j] == 0)
{
i = j;
while (j < CoveredWord.size() && CoveredWord[j] == 0)
j++;
pair<int,int> begend = make_pair(i,j-1);
FutureCost += FutureCostTable[begend];
}
else
j++;
}
}
/************************************************************************/
/* compute feature function */
/************************************************************************/
void Hypotheses::ComputeFeature(const Feature &FatherFeat, const vector<double>& lamda)
{
feat.p = 0.0;
feat.sigfunc = 0.0;
for(int i=0; i<lamda.size(); i++)
{
feat.sigfunc += lamda[i] * feat.featfunc[i];
}
feat.p = FatherFeat.p + feat.sigfunc;
EstimateProb = feat.p + FutureCost;
}
/************************************************************************/
/* overload == */
/************************************************************************/
bool Hypotheses::operator ==(const Hypotheses &right)const
{
if((CoveredWord == right.CoveredWord)
&& (LastEnglishWord == right.LastEnglishWord)
&& (CurBegEnd.second == right.CurBegEnd.second) )
return true;
else
return false;
}
/************************************************************************/
/* overload < */
/************************************************************************/
bool Hypotheses::operator <(const Hypotheses &right)const
{
return EstimateProb < right.EstimateProb;
}
/************************************************************************/
/* overload > */
/************************************************************************/
bool Hypotheses::operator >(const Hypotheses &right)const
{
return EstimateProb > right.EstimateProb;
}
/************************************************************************/
/* overload = */
/************************************************************************/
Hypotheses& Hypotheses::operator =(const Hypotheses &right)
{
PreStack = right.PreStack;
PreStackNum = right.PreStackNum;
CoveredWord = right.CoveredWord;
CoveredNumber = right.CoveredNumber;
EndPosOfLastPhrase = right.EndPosOfLastPhrase;
CurBegEnd = right.CurBegEnd;
LastEnglishWord = right.LastEnglishWord;
CurEnglishTranslation = right.CurEnglishTranslation;
feat = right.feat;
FutureCost = right.FutureCost;
EstimateProb = right.EstimateProb;
AdditionalArcs = right.AdditionalArcs;
return *this;
}
/************************************************************************/
/* Print */
/************************************************************************/
void Hypotheses::ShowHP(ostream &out)
{
out << "<Hypothesis Father=\"(" << PreStack << "," << PreStackNum << ")\">" << endl;
out << "<Covered num=\"" << CoveredNumber <<"\">";
copy(CoveredWord.begin(),CoveredWord.end(),ostream_iterator<int>(out," "));
out << "</Covered>" << endl;
out << "<CurrentTranslation> " << CurEnglishTranslation << " </CurrentTranslation>"<<endl;
out << "<LastEnglishWord> " << LastEnglishWord << " </LastEnglishWord>"<<endl;
out << "<CoveredWordPosition LastEnd=\"" << EndPosOfLastPhrase <<"\"> ";
out << "(" << CurBegEnd.first << ", " << CurBegEnd.second << ") </CoveredWordPosition>" << endl;
out << "<Feature FutureCost=\"" << FutureCost << "\" EstimateProb=\"" << EstimateProb << "\"> ";
feat.Show(out);
out << " </Feature>" << endl;
out << "<AddArc num=\"" << AdditionalArcs.size() << "\">" << endl;
int i;
for (i=0; i<AdditionalArcs.size(); i++)
{
out << "<Arc No=\"" << i << "\">" << "(" << AdditionalArcs[i].PreStack << "," << AdditionalArcs[i].PreStackNum << ") | ";
out << AdditionalArcs[i].english << " | ";
AdditionalArcs[i].feat.Show(out);
out << "</Arc>" << endl;
}
out << "</AddArc>" << endl;
out << "</Hypothesis>" << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -