| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <algorithm> |
| #include "ChartCell.h" |
| #include "ChartCellCollection.h" |
| #include "HypergraphOutput.h" |
| #include "RuleCubeQueue.h" |
| #include "RuleCube.h" |
| #include "Range.h" |
| #include "Util.h" |
| #include "ChartTranslationOptions.h" |
| #include "ChartTranslationOptionList.h" |
| #include "ChartManager.h" |
| #include "util/exception.hh" |
|
|
| using namespace std; |
|
|
| namespace Moses |
| { |
|
|
| ChartCellBase::ChartCellBase(size_t startPos, size_t endPos) : |
| m_coverage(startPos, endPos), |
| m_targetLabelSet(m_coverage) {} |
|
|
| ChartCellBase::~ChartCellBase() {} |
|
|
| |
| |
| |
| |
| ChartCell::ChartCell(size_t startPos, size_t endPos, ChartManager &manager) : |
| ChartCellBase(startPos, endPos), m_manager(manager) |
| { |
| m_nBestIsEnabled = manager.options()->nbest.enabled; |
| } |
|
|
| ChartCell::~ChartCell() {} |
|
|
| |
| |
| |
| |
| |
| bool ChartCell::AddHypothesis(ChartHypothesis *hypo) |
| { |
| const Word &targetLHS = hypo->GetTargetLHS(); |
| MapType::iterator m = m_hypoColl.find(targetLHS); |
| if (m == m_hypoColl.end()) { |
| std::pair<Word, ChartHypothesisCollection> |
| e(targetLHS, ChartHypothesisCollection(*m_manager.options())); |
| m = m_hypoColl.insert(e).first; |
| } |
| return m->second.AddHypothesis(hypo, m_manager); |
| } |
|
|
| |
| void ChartCell::PruneToSize() |
| { |
| MapType::iterator iter; |
| for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) { |
| ChartHypothesisCollection &coll = iter->second; |
| coll.PruneToSize(m_manager); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| void ChartCell::Decode(const ChartTranslationOptionList &transOptList |
| , const ChartCellCollection &allChartCells) |
| { |
| |
| RuleCubeQueue queue(m_manager); |
|
|
| |
| for (size_t i = 0; i < transOptList.GetSize(); ++i) { |
| const ChartTranslationOptions &transOpt = transOptList.Get(i); |
| RuleCube *ruleCube = new RuleCube(transOpt, allChartCells, m_manager); |
| queue.Add(ruleCube); |
| } |
|
|
| |
| const size_t popLimit = m_manager.options()->cube.pop_limit; |
| for (size_t numPops = 0; numPops < popLimit && !queue.IsEmpty(); ++numPops) { |
| ChartHypothesis *hypo = queue.Pop(); |
| AddHypothesis(hypo); |
| } |
| } |
|
|
| |
| void ChartCell::SortHypotheses() |
| { |
| UTIL_THROW_IF2(!m_targetLabelSet.Empty(), "Already sorted"); |
|
|
| MapType::iterator iter; |
| for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) { |
| ChartHypothesisCollection &coll = iter->second; |
|
|
| if (coll.GetSize()) { |
| coll.SortHypotheses(); |
| m_targetLabelSet.AddConstituent(iter->first, &coll.GetSortedHypotheses()); |
| } |
| } |
| } |
|
|
| |
| const ChartHypothesis *ChartCell::GetBestHypothesis() const |
| { |
| const ChartHypothesis *ret = NULL; |
| float bestScore = -std::numeric_limits<float>::infinity(); |
|
|
| MapType::const_iterator iter; |
| for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) { |
| const HypoList &sortedList = iter->second.GetSortedHypotheses(); |
| if (sortedList.size() > 0) { |
| const ChartHypothesis *hypo = sortedList[0]; |
| if (hypo->GetFutureScore() > bestScore) { |
| bestScore = hypo->GetFutureScore(); |
| ret = hypo; |
| } |
| } |
| } |
|
|
| return ret; |
| } |
|
|
| |
| void ChartCell::CleanupArcList() |
| { |
| |
| if (!m_nBestIsEnabled) return; |
|
|
| MapType::iterator iter; |
| for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) { |
| ChartHypothesisCollection &coll = iter->second; |
| coll.CleanupArcList(); |
| } |
| } |
|
|
| |
| void ChartCell::OutputSizes(std::ostream &out) const |
| { |
| MapType::const_iterator iter; |
| for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) { |
| const Word &targetLHS = iter->first; |
| const ChartHypothesisCollection &coll = iter->second; |
|
|
| out << targetLHS << "=" << coll.GetSize() << " "; |
| } |
| } |
|
|
| |
| size_t ChartCell::GetSize() const |
| { |
| size_t ret = 0; |
| MapType::const_iterator iter; |
| for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) { |
| const ChartHypothesisCollection &coll = iter->second; |
|
|
| ret += coll.GetSize(); |
| } |
|
|
| return ret; |
| } |
|
|
| const HypoList *ChartCell::GetAllSortedHypotheses() const |
| { |
| HypoList *ret = new HypoList(); |
|
|
| MapType::const_iterator iter; |
| for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) { |
| const ChartHypothesisCollection &coll = iter->second; |
| const HypoList &list = coll.GetSortedHypotheses(); |
| std::copy(list.begin(), list.end(), std::inserter(*ret, ret->end())); |
| } |
| return ret; |
| } |
|
|
| |
| void ChartCell::WriteSearchGraph(const ChartSearchGraphWriter& writer, const std::map<unsigned, bool> &reachable) const |
| { |
| MapType::const_iterator iterOutside; |
| for (iterOutside = m_hypoColl.begin(); iterOutside != m_hypoColl.end(); ++iterOutside) { |
| const ChartHypothesisCollection &coll = iterOutside->second; |
| coll.WriteSearchGraph(writer, reachable); |
| } |
| } |
|
|
| std::ostream& operator<<(std::ostream &out, const ChartCell &cell) |
| { |
| ChartCell::MapType::const_iterator iterOutside; |
| for (iterOutside = cell.m_hypoColl.begin(); iterOutside != cell.m_hypoColl.end(); ++iterOutside) { |
| const Word &targetLHS = iterOutside->first; |
| cerr << targetLHS << ":" << endl; |
|
|
| const ChartHypothesisCollection &coll = iterOutside->second; |
| cerr << coll; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| return out; |
| } |
|
|
| } |
|
|