2011-03-22 17:33:16 +03:00
|
|
|
|
#ifndef moses_PhrasePairFeature_h
|
|
|
|
|
#define moses_PhrasePairFeature_h
|
|
|
|
|
|
|
|
|
|
#include "Factor.h"
|
|
|
|
|
#include "FeatureFunction.h"
|
2012-03-19 06:45:59 +04:00
|
|
|
|
#include "Sentence.h"
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_THREADS
|
|
|
|
|
#include <boost/thread/tss.hpp>
|
|
|
|
|
#endif
|
2011-03-22 17:33:16 +03:00
|
|
|
|
|
|
|
|
|
namespace Moses {
|
|
|
|
|
|
|
|
|
|
/**
|
2012-03-07 15:48:58 +04:00
|
|
|
|
* Phrase pair feature: complete source/target phrase pair
|
2011-03-22 17:33:16 +03:00
|
|
|
|
**/
|
|
|
|
|
class PhrasePairFeature: public StatelessFeatureFunction {
|
2012-03-19 06:45:59 +04:00
|
|
|
|
|
2012-03-20 17:45:25 +04:00
|
|
|
|
typedef std::map< char, short > CharHash;
|
|
|
|
|
|
2012-03-19 06:45:59 +04:00
|
|
|
|
struct ThreadLocalStorage
|
|
|
|
|
{
|
|
|
|
|
const Sentence *input;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private:
|
2012-03-20 17:45:25 +04:00
|
|
|
|
#ifdef WITH_THREADS
|
2012-03-19 06:45:59 +04:00
|
|
|
|
boost::thread_specific_ptr<ThreadLocalStorage> m_local;
|
2012-03-20 17:45:25 +04:00
|
|
|
|
#else
|
2012-03-19 06:45:59 +04:00
|
|
|
|
std::auto_ptr<ThreadLocalStorage> m_local;
|
2012-03-20 17:45:25 +04:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
FactorType m_sourceFactorId;
|
|
|
|
|
FactorType m_targetFactorId;
|
|
|
|
|
bool m_unrestricted;
|
|
|
|
|
bool m_simple;
|
|
|
|
|
bool m_sourceContext;
|
|
|
|
|
float m_sparseProducerWeight;
|
|
|
|
|
bool m_ignorePunctuation;
|
|
|
|
|
CharHash m_punctuationHash;
|
2012-03-19 06:45:59 +04:00
|
|
|
|
|
2012-03-22 19:04:18 +04:00
|
|
|
|
std::set<std::string> m_limitedFeatures;
|
|
|
|
|
|
2011-03-22 17:33:16 +03:00
|
|
|
|
public:
|
2012-03-19 06:45:59 +04:00
|
|
|
|
PhrasePairFeature (FactorType sourceFactorId, FactorType targetFactorId,
|
2012-03-22 19:04:18 +04:00
|
|
|
|
bool simple, bool sourceContext, bool ignorePunctuation, std::string filePath) :
|
2012-03-20 17:45:25 +04:00
|
|
|
|
StatelessFeatureFunction("pp", ScoreProducer::unlimited),
|
2012-03-19 06:45:59 +04:00
|
|
|
|
m_sourceFactorId(sourceFactorId),
|
|
|
|
|
m_targetFactorId(targetFactorId),
|
|
|
|
|
m_unrestricted(true),
|
|
|
|
|
m_simple(simple),
|
|
|
|
|
m_sourceContext(sourceContext),
|
2012-03-20 17:45:25 +04:00
|
|
|
|
m_sparseProducerWeight(1),
|
|
|
|
|
m_ignorePunctuation(ignorePunctuation) {
|
2012-03-19 06:45:59 +04:00
|
|
|
|
std::cerr << "Creating phrase pair feature.. " << std::endl;
|
|
|
|
|
if (m_simple == 1) std::cerr << "using simple phrase pairs.. ";
|
|
|
|
|
if (m_sourceContext == 1) std::cerr << "using source context.. ";
|
2012-03-20 17:45:25 +04:00
|
|
|
|
|
|
|
|
|
// compile a list of punctuation characters
|
|
|
|
|
if (m_ignorePunctuation) {
|
|
|
|
|
std::cerr << "ignoring punctuation for triggers.. ";
|
2012-03-22 19:04:18 +04:00
|
|
|
|
char punctuation[] = "\"'!?¿·()#_,.:;•&@‑/\\0123456789~=";
|
2012-03-20 17:45:25 +04:00
|
|
|
|
for (size_t i=0; i < sizeof(punctuation)-1; ++i)
|
|
|
|
|
m_punctuationHash[punctuation[i]] = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-19 06:45:59 +04:00
|
|
|
|
std::cerr << "done." << std::endl;
|
2012-03-22 19:04:18 +04:00
|
|
|
|
|
|
|
|
|
// only temporary: features for restricted training
|
|
|
|
|
if (!filePath.empty()) {
|
|
|
|
|
std::ifstream inFile(filePath.c_str());
|
|
|
|
|
if (!inFile)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "could not open file " << filePath << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string line;
|
|
|
|
|
while (getline(inFile, line)) {
|
|
|
|
|
m_limitedFeatures.insert(line);
|
|
|
|
|
}
|
|
|
|
|
inFile.close();
|
|
|
|
|
m_unrestricted = false;
|
|
|
|
|
}
|
2012-03-19 06:45:59 +04:00
|
|
|
|
}
|
2011-03-22 17:33:16 +03:00
|
|
|
|
|
|
|
|
|
virtual void Evaluate(
|
|
|
|
|
const TargetPhrase& cur_hypo,
|
|
|
|
|
ScoreComponentCollection* accumulator) const;
|
|
|
|
|
|
|
|
|
|
//NB: Should really precompute this feature, but don't have
|
|
|
|
|
//good hooks to do this.
|
|
|
|
|
virtual bool ComputeValueInTranslationOption() const;
|
|
|
|
|
|
2011-09-20 14:23:38 +04:00
|
|
|
|
std::string GetScoreProducerWeightShortName(unsigned) const;
|
2011-03-22 17:33:16 +03:00
|
|
|
|
size_t GetNumInputScores() const;
|
|
|
|
|
|
2012-03-19 06:45:59 +04:00
|
|
|
|
void InitializeForInput( Sentence const& in );
|
|
|
|
|
|
2012-03-15 04:32:27 +04:00
|
|
|
|
void SetSparseProducerWeight(float weight) { m_sparseProducerWeight = weight; }
|
|
|
|
|
float GetSparseProducerWeight() const { return m_sparseProducerWeight; }
|
2011-03-22 17:33:16 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|