00001 #include "mmsapt.h"
00002 #include "moses/TranslationModel/PhraseDictionaryTreeAdaptor.h"
00003 #include "moses/TranslationTask.h"
00004 #include <boost/foreach.hpp>
00005 #include <boost/format.hpp>
00006 #include <boost/tokenizer.hpp>
00007 #include <boost/shared_ptr.hpp>
00008 #include <algorithm>
00009 #include <iostream>
00010
00011 using namespace Moses;
00012 using namespace sapt;
00013 using namespace std;
00014 using namespace boost;
00015
00016 vector<FactorType> fo(1,FactorType(0));
00017
00018 class SimplePhrase : public Moses::Phrase
00019 {
00020 vector<FactorType> const m_fo;
00021 public:
00022 SimplePhrase(): m_fo(1,FactorType(0)) {}
00023
00024 void init(string const& s)
00025 {
00026 istringstream buf(s); string w;
00027 while (buf >> w)
00028 {
00029 Word wrd;
00030 this->AddWord().CreateFromString(Input,m_fo,StringPiece(w),false,false);
00031 }
00032 }
00033 };
00034
00035 class TargetPhraseIndexSorter
00036 {
00037 TargetPhraseCollection const& my_tpc;
00038 CompareTargetPhrase cmp;
00039 public:
00040 TargetPhraseIndexSorter(TargetPhraseCollection const& tpc) : my_tpc(tpc) {}
00041 bool operator()(size_t a, size_t b) const
00042 {
00043 return cmp(*my_tpc[a], *my_tpc[b]);
00044 }
00045 };
00046
00047 int main(int argc, char const* argv[])
00048 {
00049 Parameter params;
00050 if (!params.LoadParam(argc,argv) || !StaticData::LoadDataStatic(¶ms, argv[0]))
00051 exit(1);
00052
00053 StaticData const& global = StaticData::Instance();
00054 global.SetVerboseLevel(0);
00055 vector<FactorType> ifo = global.options()->input.factor_order;
00056
00057 PhraseDictionary* PT = PhraseDictionary::GetColl()[0];
00058 Mmsapt* mmsapt; PhraseDictionaryTreeAdaptor* pdta;
00059 mmsapt = dynamic_cast<Mmsapt*>(PT);
00060 pdta = dynamic_cast<PhraseDictionaryTreeAdaptor*>(PT);
00061
00062 if (!mmsapt && !pdta)
00063 {
00064 cerr << "Phrase table implementation not supported by this utility." << endl;
00065 exit(1);
00066 }
00067
00068 string line;
00069 while (true)
00070 {
00071 boost::shared_ptr<Sentence> phrase(new Sentence(global.options()));
00072 if (!phrase->Read(cin)) break;
00073 boost::shared_ptr<TranslationTask> ttask;
00074 ttask = TranslationTask::create(phrase);
00075 PT->InitializeForInput(ttask);
00076
00077 Phrase const& p = *phrase;
00078 cout << p << endl;
00079 TargetPhraseCollection::shared_ptr trg
00080 = PT->GetTargetPhraseCollectionLEGACY(ttask, p);
00081 if (!trg) continue;
00082 vector<size_t> order(trg->GetSize());
00083 for (size_t i = 0; i < order.size(); ++i) order[i] = i;
00084 sort(order.begin(),order.end(),TargetPhraseIndexSorter(*trg));
00085 size_t k = 0;
00086
00087 cout.precision(2);
00088
00089 vector<string> fname;
00090 if (mmsapt)
00091 {
00092 fname = mmsapt->GetFeatureNames();
00093 cout << " ";
00094 BOOST_FOREACH(string const& fn, fname)
00095 cout << " " << format("%10.10s") % fn;
00096 cout << endl;
00097 }
00098
00099 BOOST_FOREACH(size_t i, order)
00100 {
00101 Phrase const& phr = static_cast<Phrase const&>(*(*trg)[i]);
00102 cout << setw(3) << ++k << " " << phr << endl;
00103 ScoreComponentCollection const& scc = (*trg)[i]->GetScoreBreakdown();
00104 size_t start = PT->GetIndex();
00105 size_t stop = start + PT->GetNumScoreComponents();
00106 FVector const& scores = scc.GetScoresVector();
00107 cout << " ";
00108 for (size_t k = start; k < stop; ++k)
00109 {
00110 size_t j = k-start;
00111 float f = (mmsapt ? mmsapt->isLogVal(j) ? exp(scores[k]) : scores[k]
00112 : scores[k] < 0 ? exp(scores[k]) : scores[k]);
00113 string fmt = (mmsapt && mmsapt->isInteger(j)) ? "%10d" : "%10.8f";
00114 cout << " " << format(fmt) % f;
00115 }
00116 cout << endl;
00117 }
00118 }
00119 exit(0);
00120 }
00121
00122
00123