//-------------------------------------------------------------------------------
int JMess::parseXML(QString xmlInFile)
{
  mPortsToConnect.clear();
  QString errorStr;
  int errorLine;
  int errorColumn;
  
  QFile file(xmlInFile);
  if (!file.open(QIODevice::ReadOnly)) {
    cerr << "Cannot open file for reading: "
	 << qPrintable(file.errorString()) << endl;
    return 1;
  }

  QDomDocument doc;
  if (!doc.setContent(&file, true, &errorStr, &errorLine,
		      &errorColumn)) {
    cerr << "===================================================\n" 
	 << "Error parsing XML input file:\n"
	 << "Parse error at line " << errorLine
	 << ", column " << errorColumn << "\n"
	 << qPrintable(errorStr) << "\n"
	 << "===================================================\n";
    return 1;
  }
  
  QDomElement jmess = doc.documentElement();
  if (jmess.tagName() != "jmess") {
    cerr << "Error: Root tag should be <jmess>: "
	 << qPrintable(jmess.tagName()) << endl;
    return 1;
  }


  QVector<QString> OutputInput(2);
  //First check for <connection> tag
  for(QDomNode n_cntn = jmess.firstChild(); 
      !n_cntn.isNull(); n_cntn = n_cntn.nextSibling()) {
    QDomElement cntn = n_cntn.toElement();
    if (cntn.tagName() == "connection") {
      //Now check for ouput & input tag
      for(QDomNode n_sck = cntn.firstChild(); 
	  !n_sck.isNull(); n_sck = n_sck.nextSibling()) {
	QDomElement sck = n_sck.toElement();
	//cout << qPrintable(sck.tagName()) << endl;
	//cout << qPrintable(sck.text()) << endl;
	if (sck.tagName() == "output") {
	  OutputInput[0] = sck.text();
	}
	else if (sck.tagName() == "input") {
	  OutputInput[1] = sck.text();
	}
      }
      mPortsToConnect.append(OutputInput);
    }
  }

  return 0;
  
}
Exemplo n.º 2
0
void IOWrapper::OutputBestHypo(const Hypothesis *hypo, long /*translationId*/, char reportSegmentation, bool reportAllFactors)
{
  if (hypo != NULL) {
    VERBOSE(1,"BEST TRANSLATION: " << *hypo << endl);
    VERBOSE(3,"Best path: ");
    Backtrack(hypo);
    VERBOSE(3,"0" << std::endl);
    if (!m_surpressSingleBestOutput) {
      if (StaticData::Instance().GetOutputHypoScore()) {
        cout << hypo->GetTotalScore() << " ";
      }

      if (StaticData::Instance().IsPathRecoveryEnabled()) {
        OutputInput(cout, hypo);
        cout << "||| ";
      }
      OutputBestSurface(cout, hypo, m_outputFactorOrder, reportSegmentation, reportAllFactors);
      cout << endl;
    }
  } else {
    VERBOSE(1, "NO BEST TRANSLATION" << endl);
    if (!m_surpressSingleBestOutput) {
      cout << endl;
    }
  }
}
Exemplo n.º 3
0
void OutputInput(std::vector<const Phrase*>& map, const Hypothesis* hypo)
{
  if (hypo->GetPrevHypo()) {
    OutputInput(map, hypo->GetPrevHypo());
    map[hypo->GetCurrSourceWordsRange().GetStartPos()] = &hypo->GetTranslationOption().GetInputPath().GetPhrase();
  }
}
//-------------------------------------------------------------------------------
void JMess::setConnectedPorts()
{
  mConnectedPorts.clear();

  const char **ports, **connections; //vector of ports and connections
  QVector<QString> OutputInput(2); //helper variable

  //Get active output ports.
  ports = jack_get_ports (mClient, NULL, NULL, JackPortIsOutput);
  
  for (unsigned int out_i = 0; ports[out_i]; ++out_i) {
    if ((connections = jack_port_get_all_connections 
	 (mClient, jack_port_by_name(mClient, ports[out_i]))) != 0) {
      for (unsigned int in_i = 0; connections[in_i]; ++in_i) {
	OutputInput[0] = ports[out_i];
	//cout << "Output ===> " <<qPrintable(OutputInput[0]) << endl;
	OutputInput[1] = connections[in_i];
	//cout << "Input ===> " << qPrintable(OutputInput[1]) << endl;
	mConnectedPorts.append(OutputInput);
      }
    }
  }

  free(ports);
}
Exemplo n.º 5
0
void OutputInput(std::ostream& os, const Hypothesis* hypo)
{
  size_t len = hypo->GetInput().GetSize();
  std::vector<const Phrase*> inp_phrases(len, 0);
  OutputInput(inp_phrases, hypo);
  for (size_t i=0; i<len; ++i)
    if (inp_phrases[i]) os << *inp_phrases[i];
}
//-------------------------------------------------------------------------------
void JMess::writeOutput(QString xmlOutFile)
{
  QDomDocument jmess_xml;   QDomElement root;
  QDomElement connection;   QDomElement output;
  QDomElement input;        QDomText output_name;
  QDomText input_name;
  
  QVector<QString> OutputInput(2);

  this->setConnectedPorts();

  root = jmess_xml.createElement("jmess");
  for (QVector<QVector<QString> >::iterator it = mConnectedPorts.begin();
       it != mConnectedPorts.end(); ++it) {
    OutputInput = *it;
    //cout << "Output ===> " <<qPrintable(OutputInput[0]) << endl;
    //cout << "Input ===> " <<qPrintable(OutputInput[1]) << endl;

    //Initialize XML elements
    connection = jmess_xml.createElement("connection");
    output = jmess_xml.createElement("output");
    input = jmess_xml.createElement("input");
    output_name = jmess_xml.createTextNode(OutputInput[0]);
    input_name = jmess_xml.createTextNode(OutputInput[1]);

    jmess_xml.appendChild(root);      root.appendChild(connection);
    connection.appendChild(output);   connection.appendChild(input);
    output.appendChild(output_name);  input.appendChild(input_name);
  }

  //Write output file
  QFile file(xmlOutFile);
  string answer = "";
  //Check for existing file first, and confirm before overwriting
//  if (file.exists()) {
//    while ((answer != "yes") && (answer != "no")) {
//      cout << "WARNING: The File " <<qPrintable(xmlOutFile)
//	   << " exists. Do you want to overwrite it? (yes/no): ";
//      cin >> answer;
//    }
//  }
//  else {
//    answer = "yes";
//  }
  answer = "y"
  if (answer == "y") {
    if (!file.open(QIODevice::WriteOnly)) {
      cerr << "Cannot open file for writing: "
	   << qPrintable(file.errorString()) << endl;
      exit(1);
    }
    
    QTextStream out(&file);
    jmess_xml.save(out, Indent);
    cout << qPrintable(xmlOutFile) << " written." << endl;
  }
}
//-------------------------------------------------------------------------------
void JMess::disconnectAll()
{
  QVector<QString> OutputInput(2);
  
  this->setConnectedPorts();
  
  for (QVector<QVector<QString> >::iterator it = mConnectedPorts.begin();
       it != mConnectedPorts.end(); ++it) {
    OutputInput = *it;
    
    if (jack_disconnect(mClient, OutputInput[0].toAscii(), OutputInput[1].toAscii())) {
      cerr << "WARNING: port: " << qPrintable(OutputInput[0])
	   << "and port: " << qPrintable(OutputInput[1])
	   << " could not be disconnected.\n";
    }
  }

}
//-------------------------------------------------------------------------------
void JMess::connectPorts(QString xmlInFile)
{
  QVector<QString> OutputInput(2);

  if ( !(this->parseXML(xmlInFile)) ) {  
    for (QVector<QVector<QString> >::iterator it = mPortsToConnect.begin();
	 it != mPortsToConnect.end(); ++it) {
      OutputInput = *it;

      if (jack_connect(mClient, OutputInput[0].toAscii(), OutputInput[1].toAscii())) {
	//Display a warining only if the error is not because the ports are already
	//connected, in case the program doesn't display anyting.
	if (EEXIST != 
	    jack_connect(mClient, OutputInput[0].toAscii(), OutputInput[1].toAscii())) {
	  cerr << "WARNING: port: " << qPrintable(OutputInput[0])
	       << "and port: " << qPrintable(OutputInput[1])
	       << " could not be connected.\n";
	}
      }
    }
  }

}
Exemplo n.º 9
0
void OutputNBest(std::ostream& out
                 , const Moses::TrellisPathList &nBestList
                 , const std::vector<Moses::FactorType>& outputFactorOrder
                 , long translationId
                 , char reportSegmentation)
{
  const StaticData &staticData = StaticData::Instance();
  bool reportAllFactors = staticData.GetReportAllFactorsNBest();
  bool includeSegmentation = staticData.NBestIncludesSegmentation();
  bool includeWordAlignment = staticData.PrintAlignmentInfoInNbest();

  TrellisPathList::const_iterator iter;
  for (iter = nBestList.begin() ; iter != nBestList.end() ; ++iter) {
    const TrellisPath &path = **iter;
    const std::vector<const Hypothesis *> &edges = path.GetEdges();

    // print the surface factor of the translation
    out << translationId << " ||| ";
    for (int currEdge = (int)edges.size() - 1 ; currEdge >= 0 ; currEdge--) {
      const Hypothesis &edge = *edges[currEdge];
      OutputSurface(out, edge, outputFactorOrder, reportSegmentation, reportAllFactors);
    }
    out << " |||";

    // print scores with feature names
    OutputAllFeatureScores(path.GetScoreBreakdown(), out );

    // total
    out << " ||| " << path.GetTotalScore();

    //phrase-to-phrase segmentation
    if (includeSegmentation) {
      out << " |||";
      for (int currEdge = (int)edges.size() - 2 ; currEdge >= 0 ; currEdge--) {
        const Hypothesis &edge = *edges[currEdge];
        const WordsRange &sourceRange = edge.GetCurrSourceWordsRange();
        WordsRange targetRange = path.GetTargetWordsRange(edge);
        out << " " << sourceRange.GetStartPos();
        if (sourceRange.GetStartPos() < sourceRange.GetEndPos()) {
          out << "-" << sourceRange.GetEndPos();
        }
        out<< "=" << targetRange.GetStartPos();
        if (targetRange.GetStartPos() < targetRange.GetEndPos()) {
          out<< "-" << targetRange.GetEndPos();
        }
      }
    }

    if (includeWordAlignment) {
      out << " ||| ";
      for (int currEdge = (int)edges.size() - 2 ; currEdge >= 0 ; currEdge--) {
        const Hypothesis &edge = *edges[currEdge];
        const WordsRange &sourceRange = edge.GetCurrSourceWordsRange();
        WordsRange targetRange = path.GetTargetWordsRange(edge);
        const int sourceOffset = sourceRange.GetStartPos();
        const int targetOffset = targetRange.GetStartPos();
        const AlignmentInfo &ai = edge.GetCurrTargetPhrase().GetAlignTerm();

        OutputAlignment(out, ai, sourceOffset, targetOffset);

      }
    }

    if (StaticData::Instance().IsPathRecoveryEnabled()) {
      out << " ||| ";
      OutputInput(out, edges[0]);
    }

    out << endl;
  }

  out << std::flush;
}
Exemplo n.º 10
0
void OutputNBest(std::ostream& out, const Moses::TrellisPathList &nBestList, const std::vector<Moses::FactorType>& outputFactorOrder, const TranslationSystem* system, long translationId, bool reportSegmentation)
{
  const StaticData &staticData = StaticData::Instance();
  bool labeledOutput = staticData.IsLabeledNBestList();
  bool reportAllFactors = staticData.GetReportAllFactorsNBest();
  bool includeSegmentation = staticData.NBestIncludesSegmentation();
  bool includeWordAlignment = staticData.PrintAlignmentInfoInNbest();

  TrellisPathList::const_iterator iter;
  for (iter = nBestList.begin() ; iter != nBestList.end() ; ++iter) {
    const TrellisPath &path = **iter;
    const std::vector<const Hypothesis *> &edges = path.GetEdges();

    // print the surface factor of the translation
    out << translationId << " ||| ";
    for (int currEdge = (int)edges.size() - 1 ; currEdge >= 0 ; currEdge--) {
      const Hypothesis &edge = *edges[currEdge];
      OutputSurface(out, edge, outputFactorOrder, reportSegmentation, reportAllFactors);
    }
    out << " |||";

    // print scores with feature names
    OutputAllFeatureScores( out, system, path );
    string lastName;

    // translation components
    const vector<PhraseDictionaryFeature*>& pds = system->GetPhraseDictionaries();
    if (pds.size() > 0) {

      for( size_t i=0; i<pds.size(); i++ ) {
	size_t pd_numinputscore = pds[i]->GetNumInputScores();
	vector<float> scores = path.GetScoreBreakdown().GetScoresForProducer( pds[i] );
	for (size_t j = 0; j<scores.size(); ++j){

	  if (labeledOutput && (i == 0) ){
	    if ((j == 0) || (j == pd_numinputscore)){
	      lastName =  pds[i]->GetScoreProducerWeightShortName(j);
	      out << " " << lastName << ":";
	    }
	  }
	  out << " " << scores[j];
	}
      }
    }

    // generation
    const vector<GenerationDictionary*>& gds = system->GetGenerationDictionaries();
    if (gds.size() > 0) {

      for( size_t i=0; i<gds.size(); i++ ) {
	size_t pd_numinputscore = gds[i]->GetNumInputScores();
	vector<float> scores = path.GetScoreBreakdown().GetScoresForProducer( gds[i] );
	for (size_t j = 0; j<scores.size(); ++j){

	  if (labeledOutput && (i == 0) ){
	    if ((j == 0) || (j == pd_numinputscore)){
	      lastName =  gds[i]->GetScoreProducerWeightShortName(j);
	      out << " " << lastName << ":";
	    }
	  }
	  out << " " << scores[j];
	}
      }
    }

    // total
    out << " ||| " << path.GetTotalScore();

    //phrase-to-phrase segmentation
    if (includeSegmentation) {
      out << " |||";
      for (int currEdge = (int)edges.size() - 2 ; currEdge >= 0 ; currEdge--) {
        const Hypothesis &edge = *edges[currEdge];
        const WordsRange &sourceRange = edge.GetCurrSourceWordsRange();
        WordsRange targetRange = path.GetTargetWordsRange(edge);
        out << " " << sourceRange.GetStartPos();
        if (sourceRange.GetStartPos() < sourceRange.GetEndPos()) {
          out << "-" << sourceRange.GetEndPos();
        }
        out<< "=" << targetRange.GetStartPos();
        if (targetRange.GetStartPos() < targetRange.GetEndPos()) {
          out<< "-" << targetRange.GetEndPos();
        }
      }
    }

    if (includeWordAlignment) {
      out << " ||| ";
      for (int currEdge = (int)edges.size() - 2 ; currEdge >= 0 ; currEdge--) {
        const Hypothesis &edge = *edges[currEdge];
        const WordsRange &sourceRange = edge.GetCurrSourceWordsRange();
        WordsRange targetRange = path.GetTargetWordsRange(edge);
        const int sourceOffset = sourceRange.GetStartPos();
        const int targetOffset = targetRange.GetStartPos();
        const AlignmentInfo &ai = edge.GetCurrTargetPhrase().GetAlignTerm();
        
        OutputAlignment(out, ai, sourceOffset, targetOffset);

      }
    }

    if (StaticData::Instance().IsPathRecoveryEnabled()) {
      out << "|||";
      OutputInput(out, edges[0]);
    }

    out << endl;
  }

  out << std::flush;
}