void PhraseDictionaryFuzzyMatch::InitializeForInput(ttasksptr const& ttask)
{
  InputType const& inputSentence = *ttask->GetSource();
#if defined __MINGW32__
  char dirName[] = "moses.XXXXXX";
#else
  char dirName[] = "/tmp/moses.XXXXXX";
#endif // defined
  char *temp = mkdtemp(dirName);
  UTIL_THROW_IF2(temp == NULL,
                 "Couldn't create temporary directory " << dirName);

  string dirNameStr(dirName);

  string inFileName(dirNameStr + "/in");

  ofstream inFile(inFileName.c_str());

  for (size_t i = 1; i < inputSentence.GetSize() - 1; ++i) {
    inFile << inputSentence.GetWord(i);
  }
  inFile << endl;
  inFile.close();

  long translationId = inputSentence.GetTranslationId();
  string ptFileName = m_FuzzyMatchWrapper->Extract(translationId, dirNameStr);

  // populate with rules for this sentence
  PhraseDictionaryNodeMemory &rootNode = m_collection[translationId];
  FormatType format = MosesFormat;

  // data from file
  InputFileStream inStream(ptFileName);

  // copied from class LoaderStandard
  PrintUserTime("Start loading fuzzy-match phrase model");

  const StaticData &staticData = StaticData::Instance();


  string lineOrig;
  size_t count = 0;

  while(getline(inStream, lineOrig)) {
    const string *line;
    if (format == HieroFormat) { // reformat line
      UTIL_THROW(util::Exception, "Cannot be Hiero format");
      //line = ReformatHieroRule(lineOrig);
    } else {
      // do nothing to format of line
      line = &lineOrig;
    }

    vector<string> tokens;
    vector<float> scoreVector;

    TokenizeMultiCharSeparator(tokens, *line , "|||" );

    if (tokens.size() != 4 && tokens.size() != 5) {
      UTIL_THROW2("Syntax error at " << ptFileName << ":" << count);
    }

    const string &sourcePhraseString = tokens[0]
                                       , &targetPhraseString = tokens[1]
                                           , &scoreString        = tokens[2]
                                               , &alignString        = tokens[3];

    bool isLHSEmpty = (sourcePhraseString.find_first_not_of(" \t", 0) == string::npos);
    if (isLHSEmpty && !staticData.IsWordDeletionEnabled()) {
      TRACE_ERR( ptFileName << ":" << count << ": pt entry contains empty target, skipping\n");
      continue;
    }

    Tokenize<float>(scoreVector, scoreString);
    const size_t numScoreComponents = GetNumScoreComponents();
    if (scoreVector.size() != numScoreComponents) {
      UTIL_THROW2("Size of scoreVector != number (" << scoreVector.size() << "!="
                  << numScoreComponents << ") of score components on line " << count);
    }

    UTIL_THROW_IF2(scoreVector.size() != numScoreComponents,
                   "Number of scores incorrectly specified");

    // parse source & find pt node

    // constituent labels
    Word *sourceLHS;
    Word *targetLHS;

    // source
    Phrase sourcePhrase( 0);
    sourcePhrase.CreateFromString(Input, m_input, sourcePhraseString, &sourceLHS);

    // create target phrase obj
    TargetPhrase *targetPhrase = new TargetPhrase(this);
    targetPhrase->CreateFromString(Output, m_output, targetPhraseString, &targetLHS);

    // rest of target phrase
    targetPhrase->SetAlignmentInfo(alignString);
    targetPhrase->SetTargetLHS(targetLHS);
    //targetPhrase->SetDebugOutput(string("New Format pt ") + line);

    // component score, for n-best output
    std::transform(scoreVector.begin(),scoreVector.end(),scoreVector.begin(),TransformScore);
    std::transform(scoreVector.begin(),scoreVector.end(),scoreVector.begin(),FloorScore);

    targetPhrase->GetScoreBreakdown().Assign(this, scoreVector);
    targetPhrase->EvaluateInIsolation(sourcePhrase, GetFeaturesToApply());

    TargetPhraseCollection::shared_ptr phraseColl
    = GetOrCreateTargetPhraseCollection(rootNode, sourcePhrase,
                                        *targetPhrase, sourceLHS);
    phraseColl->Add(targetPhrase);

    count++;

    if (format == HieroFormat) { // reformat line
      delete line;
    } else {
      // do nothing
    }

  }

  // sort and prune each target phrase collection
  SortAndPrune(rootNode);

  //removedirectoryrecursively(dirName);
}
Ejemplo n.º 2
0
////////////////////////////////////////////////////////////
//        NAME: translateStart
// DESCRIPTION: translate language.
//   ARGUMENTS: None
//     RETURNS: bool
//      AUTHOR: Chensi
//        DATA: 2012-3-9
////////////////////////////////////////////////////////////
bool QMultiLauguage::readLanFile(const QString &path)
{
    int nCount = 0;
    QString LanId, LanStr;
    QString tempLine;
    QStringList tempList;

    m_StoreLan.clear();

    //========check file============
    if(!QFile::exists(path))
    {
        return false;
    }


    //==========read file===========
    QFile inFile(path);
    if(!inFile.open(QIODevice::ReadOnly))
    {
        return false;
    }
    QTextStream ins(&inFile);

    do
    {
        //        ins >> LanId >> LanStr;
        tempLine = ins.readLine();
        tempList = tempLine.split("=");
        int nIdx = tempLine.indexOf("=");


        if(tempList.count() > 1)
        {
            LanId = tempList.value(0);
            LanId = LanId.trimmed();
//            LanStr = tempList.value(1);
            if(tempList.count() > 2 && nIdx >= 0)
            {
                int nLen = tempLine.length();
                nLen = nLen - nIdx;
                LanStr = tempLine.right(nLen - 1);
            }
            else
            {
                LanStr = tempList.value(1);
            }
            LanStr = LanStr.trimmed();
        }

        if(nCount < MAX_STORE_COUNT)
        {
            m_StoreLan.insert(LanId, LanStr);
            nCount++;

            continue;
        }

        //        return false;
    }while(!tempLine.isNull());

    inFile.close();


    //==========end================
    if(m_StoreLan.isEmpty())
    {
        return false;
    }
    return true;
}
int
main(int argc, char* argv[])
{
  // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
  CommandLine cmd;
  cmd.Parse(argc, argv);

  NS_LOG_UNCOND("NUMBER OF NODES = "<<ns3::N_NODES<<" , nConsumers = "<<ns3::N_TotalClients<<" , nProducers = "<<ns3::N_PRODUCERS);

  double l = 0;
  double s = ns3::N_GEANT_ROUTERS-1;
  Ptr<UniformRandomVariable> uniVar;
  uniVar=CreateObject<UniformRandomVariable> ();
  uniVar->SetAttribute ("Min", DoubleValue (l));
  uniVar->SetAttribute ("Max", DoubleValue (s));

  // setting default parameters for PointToPoint links and channels
  //this is for 56 clients to be attached
  Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue(ns3::CONS_LINK_DATA_RATE));
  Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue(ns3::CONS_LINK_DELAY));
  Config::SetDefault("ns3::DropTailQueue::MaxPackets", StringValue("20"));
  /////////////////////////////////////////////////////////////////////////
  //***IDs 0-55 are consumers, the next "N_PRODUCERS" IDs are producers, //
  //***and the IDs after them are Geant routers                          //
  /////////////////////////////////////////////////////////////////////////
  NodeContainer nodes;
  nodes.Create(ns3::N_TotalClients + ns3::N_PRODUCERS);
  PointToPointHelper p2p;

  //let's first read the Geant topology
  AnnotatedTopologyReader topologyReader("", 1);
  topologyReader.SetFileName("/home/ali/ndnSIM/ns-3/src/ndnSIM/examples/topologies/Geant2012.txt");
  topologyReader.Read();
  //  topologyReader.ApplySettings();
  topologyReader.SaveGraphviz("src/ndnSIM/examples/topologies/GeantGraph.dot");

   RandomizeConsumers (uniVar, "/home/ali/ndnSIM/ns-3/src/ndnSIM/examples/topologies/Geant2012.txt",
                         topologyReader ,nodes, p2p);
  RandomizeProducers(uniVar, "/home/ali/GitHub/Datasets/test/fifty/producers/server_dataset/large_scale/", nodes, p2p);

  MyResultApp resApp;
  resApp.InstallEndpointApp(nodes);
  resApp.InstallRouterApp(topologyReader);

    // Install NDN stack on all nodes
    ndn::StackHelper ndnHelper;
    ndnHelper.disableRibManager();
    //ndnHelper.disableFaceManager();


    //set cache store size
    ndnHelper.setCsSize(ns3::CACHE_SIZE);
    ndnHelper.InstallAll();

    ndn::GlobalRoutingHelper ndnGlobalRoutingHelper;
    ndnGlobalRoutingHelper.Install(nodes);
    for (size_t i = 0; i < ns3::N_PRODUCERS; i++) {
      ifstream inFile ((SERVERS_DATASETS_PATH + "server_dataset_"+std::to_string(i)).c_str());
      if(inFile.is_open()){
        std::string line;
        while (getline(inFile, line)){
          ndnGlobalRoutingHelper.AddOrigins (line, nodes.Get (i+ns3::N_TotalClients));
        }
      }
      else{
        NS_LOG_UNCOND("ERROR!!! UNABLE TO OPEN FILE "<<inFile);
      }

    }
    ndn::GlobalRoutingHelper::CalculateRoutes();


    // Choosing forwarding strategy for all
    ndn::StrategyChoiceHelper::InstallAll("/", "/localhost/nfd/strategy/best-route");


    // Installing applications

    ////////////////////////////////////////////////////////////////
    //from 5 file logs, i assign to five consumer each ONE file//
    ///////////////////////////////////////////////////////////////


    NS_LOG_UNCOND("now we've 56 clients attached to some of routers (randomly chosen) and "<<ns3::N_PRODUCERS<<" producers");

    ndn::AppHelper consumerHelper("ns3::ndn::ConsumerZipfMandelbrot");

    consumerHelper.SetAttribute("Frequency", StringValue(ns3::CONSUMER_FREQ));
    consumerHelper.SetAttribute("NumberOfContents", UintegerValue(CONTENT_UNIVERSE));
  //  consumerHelper.SetAttribute("MaxSeq",IntegerValue(MAX_SEQ_NUMBER));
    consumerHelper.SetAttribute("Randomize", StringValue(RANDOMIZE_ATTRIBUTE));
    consumerHelper.SetAttribute("q", DoubleValue(Q));
    consumerHelper.SetAttribute("s", DoubleValue(S));

  //  std:: string consumerPath= "/home/ali/GitHub/Datasets/test/fifty/producers/server_dataset/5F-5C-3P/content_universe_165";

      for(size_t consIndex=0; consIndex<ns3::N_TotalClients;consIndex++){
              consumerHelper.SetPrefix("/");
              consumerHelper.Install(nodes.Get(consIndex))
              .Start (Seconds(CONS_START + uniVar->GetValue(0.0, 0.01)));
      }//for

  // Schedule simulation time and run the simulation
  Simulator::Stop(Seconds(ns3::SIMULATION_TIME));

	ndn::L3RateTracer::InstallAll("rate-trace.txt", Seconds(1.0));
	L2RateTracer::InstallAll("drop-trace.txt", Seconds(0.5));
	//ndn::CsTracer::InstallAll("cs-trace.txt", Seconds(1));
	ndn::AppDelayTracer::InstallAll("app-delays-trace.txt");

  Simulator::Run();

  uint64_t m_totalForwardedInterest=0;
  uint64_t m_totalInterestOverhead=0;
  uint64_t m_totalForwardedData=0;
  uint64_t m_totalDataOverhead=0;
  uint64_t m_totalUnsatisfiedInterests=0;
  uint64_t m_totalSatisfiedInterests=0;
  uint64_t m_totalRetrievedDataAtClients=0;


  ofstream spUnsatisfiedInterestsFile;
  spUnsatisfiedInterestsFile.open("/home/ali/Documents/BFRresults/unsatisfied_Interests/spUnsatisfiedInterestsFile_"+std::to_string(ns3::S)+"_FP = "+std::to_string(ns3::FPP));


  ofstream SPDataOverheadFile;
  SPDataOverheadFile.open("/home/ali/Documents/BFRresults/overhead_files/SPDataOverheadFile_"+std::to_string(ns3::S));
  ofstream SPInterestOverheadFile;
  SPInterestOverheadFile.open("/home/ali/Documents//BFRresults/overhead_files/SPInterestOverheadFile_"+std::to_string(ns3::S)+"_FP = "+std::to_string(ns3::FPP));

  SPDataOverheadFile<<"nodeID"<<"     "<<"lastDataFwd"<<"     "
                  <<"nDataFwd"<<"     "<<"dataOverhead"<<"\n";

  SPInterestOverheadFile<<"nodeID"<<"     "<<"lastInterestFwd"<<"     "
                  <<"nInterestFwd"<<"     "<<"interestOverhead"<<"\n";

  spUnsatisfiedInterestsFile<<"consumerID"<<"     	"<<"NumberOfUnsatisfiedInterests"<<"     	"<<"NumberOfSatisfiedInterests"<<"\n";


  //let's extract results from nodes' apps
  Ptr<MyResultApp> nodeApp;
  for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
    nodeApp = CreateObject<ns3::MyResultApp> ();
    if ((*node)->GetId()>=ns3::N_TotalClients && (*node)->GetId()<=ns3::N_TotalClients+ns3::N_PRODUCERS-1){
      nodeApp = (*node)->GetApplication(1)->GetObject<ns3::MyResultApp>();
    }
    else{
      nodeApp = (*node)->GetApplication(0)->GetObject<ns3::MyResultApp>();
    }

    spUnsatisfiedInterestsFile<<(*node)->GetId()<<"     	"<<nodeApp->m_myNumberOfUnsatisfiedInterests<<"     	"<<nodeApp->m_myNumberOfSatisfiedInterests<<"\n";

    if((*node)->GetId()<ns3::N_TotalClients){
      m_totalUnsatisfiedInterests+= nodeApp->m_myNumberOfUnsatisfiedInterests;
      m_totalSatisfiedInterests+= nodeApp->m_myNumberOfSatisfiedInterests;
      m_totalRetrievedDataAtClients+= nodeApp->m_myNumberOfRetrievedData;
    }

    if((*node)->GetId()>=ns3::N_TotalClients){
      SPDataOverheadFile<<nodeApp->GetNode()->GetId()<<"    "<<nodeApp->m_myLastDataForwardTime
	                    <<"     "<<nodeApp->m_nMyForwardedData<<"     "
	                    <<nodeApp->m_myTotalDataOverhead<<"\n";
      m_totalForwardedData += nodeApp->m_nMyForwardedData;
      m_totalDataOverhead += nodeApp->m_myTotalDataOverhead;

    }
    if((*node)->GetId()<=ns3::N_TotalClients-1 || (*node)->GetId()>=ns3::N_TotalClients+ns3::N_PRODUCERS){
      SPInterestOverheadFile<<nodeApp->GetNode()->GetId()<<"    "<<nodeApp->m_myLastInterestForwardTime
	                    <<"     "<<nodeApp->m_nMyForwardedInterest<<"     "
	                    <<nodeApp->m_myTotalInterestOverhead<<"\n";
      m_totalForwardedInterest += nodeApp->m_nMyForwardedInterest;
      m_totalInterestOverhead += nodeApp->m_myTotalInterestOverhead;
    }
  }//for

  //let's write the totals also in interest file:
  SPInterestOverheadFile<<"nTotalForwardedInterest = "<<m_totalForwardedInterest<<"\n";
  SPInterestOverheadFile<<"totalInterestOverhead = "<<m_totalInterestOverhead<<"\n";

  SPInterestOverheadFile<<"nTotalForwardedData = "<<m_totalForwardedData<<"\n";
  SPInterestOverheadFile<<"totalDataOverhead = "<<m_totalDataOverhead<<"\n";
  SPInterestOverheadFile<<"Normalized overhead over interest count = "
                         <<double(m_totalInterestOverhead+m_totalDataOverhead)/(double)m_totalForwardedInterest<<"\n";
  SPInterestOverheadFile<<"Normalized overhead over data count = "
                        <<double(m_totalInterestOverhead+m_totalDataOverhead)/(double)m_totalForwardedData;

  spUnsatisfiedInterestsFile<<"total number of unsatisfied Interests at all the consumers = "<<m_totalUnsatisfiedInterests<<"\n";
  spUnsatisfiedInterestsFile<<"total number of satisfied Interests at all the consumers = "<<m_totalSatisfiedInterests<<"\n";
  spUnsatisfiedInterestsFile<<"Unsatisfaction due to link failure or false positive (METHOD a) = "<<(double)m_totalUnsatisfiedInterests/(m_totalSatisfiedInterests+m_totalUnsatisfiedInterests)<<"\n";
  spUnsatisfiedInterestsFile<<"total number of retrieved data at consumers =  "<<m_totalRetrievedDataAtClients;

  SPInterestOverheadFile.close();
  SPDataOverheadFile.close();
  spUnsatisfiedInterestsFile.close();


  Simulator::Destroy();

  //std::string hitDistanceCompleteFile = "/home/ali/ndnSIM/ns-3/HitDistanceFile"
  //CreateHitDistanceDataset (hitDistanceCompleteFile);

  return 0;
}//ns3::main
Ejemplo n.º 4
0
bool loadJPEGScaled(QImage& image, const QString& path, int maximumSize)
{
    FileReadLocker lock(path);

    if (!isJpegImage(path))
    {
        return false;
    }

    FILE* const inputFile = fopen(QFile::encodeName(path).constData(), "rb");

    if (!inputFile)
    {
        return false;
    }

    struct jpeg_decompress_struct   cinfo;

    struct jpegutils_jpeg_error_mgr jerr;

    // JPEG error handling - thanks to Marcus Meissner
    cinfo.err                 = jpeg_std_error(&jerr);
    cinfo.err->error_exit     = jpegutils_jpeg_error_exit;
    cinfo.err->emit_message   = jpegutils_jpeg_emit_message;
    cinfo.err->output_message = jpegutils_jpeg_output_message;

    if (setjmp(jerr.setjmp_buffer))
    {
        jpeg_destroy_decompress(&cinfo);
        fclose(inputFile);
        return false;
    }

    jpeg_create_decompress(&cinfo);

#ifdef Q_OS_WIN

    QFile inFile(path);
    QByteArray buffer;

    if (inFile.open(QIODevice::ReadOnly))
    {
        buffer = inFile.readAll();
        inFile.close();
    }

    jpeg_memory_src(&cinfo, (JOCTET*)buffer.data(), buffer.size());

#else  // Q_OS_WIN

    jpeg_stdio_src(&cinfo, inputFile);

#endif // Q_OS_WIN

    jpeg_read_header(&cinfo, true);

    int imgSize = qMax(cinfo.image_width, cinfo.image_height);

    // libjpeg supports 1/1, 1/2, 1/4, 1/8
    int scale=1;

    while(maximumSize*scale*2 <= imgSize)
    {
        scale *= 2;
    }

    if (scale > 8)
    {
        scale = 8;
    }

    //cinfo.scale_num = 1;
    //cinfo.scale_denom = scale;
    cinfo.scale_denom *= scale;

    switch (cinfo.jpeg_color_space)
    {
        case JCS_UNKNOWN:
            break;
        case JCS_GRAYSCALE:
        case JCS_RGB:
        case JCS_YCbCr:
            cinfo.out_color_space = JCS_RGB;
            break;
        case JCS_CMYK:
        case JCS_YCCK:
            cinfo.out_color_space = JCS_CMYK;
            break;
        default:
            break;
    }

    jpeg_start_decompress(&cinfo);

    QImage img;

    // We only take RGB with 1 or 3 components, or CMYK with 4 components
    if (!(
            (cinfo.out_color_space == JCS_RGB  && (cinfo.output_components == 3 || cinfo.output_components == 1)) ||
            (cinfo.out_color_space == JCS_CMYK &&  cinfo.output_components == 4)
        ))
    {
        jpeg_destroy_decompress(&cinfo);
        fclose(inputFile);
        return false;
    }

    switch (cinfo.output_components)
    {
        case 3:
        case 4:
            img = QImage(cinfo.output_width, cinfo.output_height, QImage::Format_RGB32);
            break;
        case 1: // B&W image
            img = QImage(cinfo.output_width, cinfo.output_height, QImage::Format_Indexed8);
            img.setColorCount(256);

            for (int i = 0 ; i < 256 ; ++i)
            {
                img.setColor(i, qRgb(i, i, i));
            }

            break;
    }

    uchar* const data = img.bits();
    int bpl           = img.bytesPerLine();

    while (cinfo.output_scanline < cinfo.output_height)
    {
        uchar* d = data + cinfo.output_scanline * bpl;
        jpeg_read_scanlines(&cinfo, &d, 1);
    }

    jpeg_finish_decompress(&cinfo);

    if (cinfo.output_components == 3)
    {
        // Expand 24->32 bpp.
        for (uint j=0; j<cinfo.output_height; ++j)
        {
            uchar* in       = img.scanLine(j) + cinfo.output_width * 3;
            QRgb* const out = reinterpret_cast<QRgb*>(img.scanLine(j));

            for (uint i = cinfo.output_width; --i; )
            {
                in     -= 3;
                out[i] = qRgb(in[0], in[1], in[2]);
            }
        }
    }
    else if (cinfo.out_color_space == JCS_CMYK)
    {
        for (uint j = 0; j < cinfo.output_height; ++j)
        {
            uchar* in       = img.scanLine(j) + cinfo.output_width * 4;
            QRgb* const out = reinterpret_cast<QRgb*>(img.scanLine(j));

            for (uint i = cinfo.output_width; --i; )
            {
                in     -= 4;
                int k  = in[3];
                out[i] = qRgb(k * in[0] / 255, k * in[1] / 255, k * in[2] / 255);
            }
        }
    }

    if (cinfo.density_unit == 1)
    {
        img.setDotsPerMeterX(int(100. * cinfo.X_density / 2.54));
        img.setDotsPerMeterY(int(100. * cinfo.Y_density / 2.54));
    }
    else if (cinfo.density_unit == 2)
    {
        img.setDotsPerMeterX(int(100. * cinfo.X_density));
        img.setDotsPerMeterY(int(100. * cinfo.Y_density));
    }

    //int newMax = qMax(cinfo.output_width, cinfo.output_height);
    //int newx = maximumSize*cinfo.output_width / newMax;
    //int newy = maximumSize*cinfo.output_height / newMax;

    jpeg_destroy_decompress(&cinfo);
    fclose(inputFile);

    image = img;

    return true;
}
Ejemplo n.º 5
0
void GenerationDictionary::Load()
{
  FactorCollection &factorCollection = FactorCollection::Instance();

  const size_t numFeatureValuesInConfig = this->GetNumScoreComponents();


  // data from file
  InputFileStream inFile(m_filePath);
  UTIL_THROW_IF2(!inFile.good(), "Couldn't read " << m_filePath);

  string line;
  size_t lineNum = 0;
  while(getline(inFile, line)) {
    ++lineNum;
    vector<string> token = Tokenize( line );

    // add each line in generation file into class
    Word *inputWord = new Word();  // deleted in destructor
    Word outputWord;

    // create word with certain factors filled out

    // inputs
    vector<string> factorString = Tokenize( token[0], "|" );
    for (size_t i = 0 ; i < GetInput().size() ; i++) {
      FactorType factorType = GetInput()[i];
      const Factor *factor = factorCollection.AddFactor( Output, factorType, factorString[i]);
      inputWord->SetFactor(factorType, factor);
    }

    factorString = Tokenize( token[1], "|" );
    for (size_t i = 0 ; i < GetOutput().size() ; i++) {
      FactorType factorType = GetOutput()[i];

      const Factor *factor = factorCollection.AddFactor( Output, factorType, factorString[i]);
      outputWord.SetFactor(factorType, factor);
    }

    size_t numFeaturesInFile = token.size() - 2;
    if (numFeaturesInFile < numFeatureValuesInConfig) {
      util::StringStream strme;
      strme << m_filePath << ":" << lineNum << ": expected " << numFeatureValuesInConfig
            << " feature values, but found " << numFeaturesInFile << "\n";
      throw strme.str();
    }
    std::vector<float> scores(numFeatureValuesInConfig, 0.0f);
    for (size_t i = 0; i < numFeatureValuesInConfig; i++)
      scores[i] = FloorScore(TransformScore(Scan<float>(token[2+i])));

    Collection::iterator iterWord = m_collection.find(inputWord);
    if (iterWord == m_collection.end()) {
      m_collection[inputWord][outputWord].Assign(this, scores);
    } else {
      // source word already in there. delete input word to avoid mem leak
      (iterWord->second)[outputWord].Assign(this, scores);
      delete inputWord;
    }
  }

  inFile.Close();
}
Ejemplo n.º 6
0
//----------------------------------------------------------------------------
bool FxCompiler::Parse (const std::string& fileName,
                        const std::string& profileName, Program& program)
{
	std::ifstream inFile(fileName.c_str());
	if (!inFile)
	{
		// If the file does not exist, the assumption is that the profile does
		// not support the shader (in which case, the Cg compiler failed).
		Messages.push_back("Profile " + profileName + " not supported.\n");
		return false;
	}

	program.Text = "";

	while (!inFile.eof())
	{
		std::string line;
		getline(inFile, line);
		if (line.empty())
		{
			continue;
		}

		// Any uncommented lines are part of the program text.
		if (line[0] != '/' && line[0] != '#')
		{
			program.Text += line + "\n";
			continue;
		}

		std::vector<std::string> tokens;
		std::string::size_type begin;

		// Get a variable line from the Cg output file.
		begin = line.find("var", 0);
		if (begin != std::string::npos)
		{
			GetTokens(line, begin, tokens);
			if (tokens.size() >= 2 && tokens[0] == "var")
			{
				std::string used = tokens.back();
				if (used == "0" || used == "1")
				{
					if (used == "1")
					{
						program.Variables.push_back(tokens);
					}
					continue;
				}
			}
			inFile.close();
			ReportError("Invalid variable line", &tokens);
			return false;
		}

		// Get the profile name.
		begin = line.find("profile", 0);
		if (begin != std::string::npos)
		{
			GetTokens(line, begin, tokens);
			if (tokens.size() >= 2 && tokens[0] == "profile")
			{
				// When the user has already compiled the programs, it is
				// because a profile is a special one.  The "!!ARBfp1.0"
				// string and the last token of "#profile specialProfile"
				// most likely do not match, so do not compare them.
				if (mAlreadyCompiled || tokens[1] == profileName)
				{
					continue;
				}
			}
			inFile.close();
			ReportError("Invalid profile line", &tokens);
			return false;
		}

		// Get the program name.
		begin = line.find("program", 0);
		if (begin != std::string::npos)
		{
			GetTokens(line, begin, tokens);
			if (tokens.size() >= 2 && tokens[0] == "program")
			{
				program.Name = tokens[1];
				continue;
			}
			inFile.close();
			ReportError("Invalid program line", &tokens);
			return false;
		}
	}

	inFile.close();
	return true;
}
void TriggerInputsForMuonEventCuts ( TString runListFilename, TString selectedInputs="", TString defaultStorage = "raw://" )
{
  AliCDBManager::Instance()->SetDefaultStorage(defaultStorage.Data());
  TObjArray inputsList;
  inputsList.SetOwner();
  
  TObjArray* selectedInputsList = selectedInputs.Tokenize(",");

  // Read input run list
  ifstream inFile(runListFilename.Data());
  TString srun = "";
  if ( inFile.is_open() ) {
    while ( ! inFile.eof() ) {
      srun.ReadLine(inFile,kFALSE);
      if ( ! srun.IsDigit() ) continue;
      
      // For each run, read trigger inputs from OCDB
      Int_t runNumber = srun.Atoi();
      AliCDBManager::Instance()->SetRun(runNumber);
      
      // Get trigger class configuration
      AliCDBEntry* entry = AliCDBManager::Instance()->Get("GRP/CTP/Config");
      if ( ! entry ) continue;
      
      THashList* runInputs = new THashList();
      runInputs->SetOwner();
      runInputs->SetUniqueID((UInt_t)runNumber);
      AliTriggerConfiguration* trigConf = (AliTriggerConfiguration*)entry->GetObject();
      const TObjArray& trigInputsArray = trigConf->GetInputs();
      AliTriggerInput* trigInput = 0x0;
      TIter next(&trigInputsArray);
      while ( ( trigInput = static_cast<AliTriggerInput*>(next()) ) ) {
        if ( selectedInputsList->GetEntriesFast() > 0 && ! selectedInputsList->FindObject(trigInput->GetName()) ) continue;
        Int_t inputId = (Int_t)TMath::Log2(trigInput->GetMask());
        TObjString* currInput = new TObjString(trigInput->GetName());
        currInput->SetUniqueID(inputId);
        runInputs->Add(currInput);
      }
      inputsList.Add(runInputs);
    }
    inFile.close();
  }
  delete selectedInputsList;
  
  // Loop on the trigger inputs
  // and group runs with an equal list of inputs
  Int_t nentries = inputsList.GetEntries();
  TArrayI checkMask(nentries);
  checkMask.Reset(1);
  for ( Int_t irun=0; irun<nentries; irun++ ) {
    if ( checkMask[irun] == 0 ) continue;
    THashList* currList = static_cast<THashList*>(inputsList.At(irun));
    TString runRange = Form("Run range: %u", currList->GetUniqueID());
    for ( Int_t jrun=irun+1; jrun<nentries; jrun++ ) {
      if ( checkMask[jrun] == 0 ) continue;
      THashList* checkList = static_cast<THashList*>(inputsList.At(jrun));
      Bool_t isDifferent = kFALSE;
      for ( Int_t itrig=0; itrig<currList->GetEntries(); itrig++ ) {
        TObjString* currInput = static_cast<TObjString*>(currList->At(itrig));
        TObject* checkInput = checkList->FindObject(currInput->GetName());
        if ( ! checkInput || checkInput->GetUniqueID() != currInput->GetUniqueID() ) {
          isDifferent = kTRUE;
          break;
        }
      } // loop on trigger inputs
      if ( isDifferent ) continue;
      checkMask[jrun] = 0;
      runRange += Form(",%u", checkList->GetUniqueID());
    } // loop on runs
    
    TString outString = "\nSetTrigInputsMap(\"";
    for ( Int_t itrig=0; itrig<currList->GetEntries(); itrig++ ) {
      TObjString* currInput = static_cast<TObjString*>(currList->At(itrig));
      outString += Form("%s:%u,",currInput->GetString().Data(), currInput->GetUniqueID());
    }
    outString.Append("\");\n");
    outString.ReplaceAll(",\"","\"");
    outString += runRange;
    printf("%s\n", outString.Data());
  } // loop on runs
}
Ejemplo n.º 8
0
int main(int argc, char* argv[])
{
    if (argc < 3) {
        return 1;
    }

    std::string inFile(argv[1]);
    std::string outFile(argv[2]);

    auto reader = (Potree::LASPointReader*)Proxy::createPointReader(inFile);

    if (!reader) {
        // TODO
        return 1;
    }

    auto aabb = reader->getAABB();

    aabb.size = aabb.max - aabb.min;

    aabb.makeCubic();

    uint64_t pointNum = 0;
    uint64_t flushNum = 0;

    FILE* fp = nullptr;
    fopen_s(&fp, outFile.c_str(), "wb");

    fseek(fp, sizeof(TMPHeader), SEEK_SET);

    std::vector<Point> points;
    points.reserve(reader->numPoints());

    static const uint32_t FLUSH_NUM = 10000;

    while (reader->readNextPoint()) {
        auto point = reader->getPoint();

        pointNum++;
        flushNum++;

        Point pt;

        pt.pos[0] = point.position.x;
        pt.pos[1] = point.position.y;
        pt.pos[2] = point.position.z;

        pt.rgba[0] = point.color.x;
        pt.rgba[1] = point.color.y;
        pt.rgba[2] = point.color.z;
        pt.rgba[3] = 0xff;

        points.push_back(pt);

        if (flushNum == FLUSH_NUM) {
            printf("%d\n", pointNum);
            fwrite(&points[0], sizeof(Point), flushNum, fp);
            points.clear();
            flushNum = 0;
        }
    }

    if (flushNum > 0) {
        printf("%d\n", pointNum);
        fwrite(&points[0], sizeof(Point), flushNum, fp);
        points.clear();
        flushNum = 0;
    }

    // Write header.
    {
        TMPHeader header;
        header.fileSize = ftell(fp);
        header.vtxFormat = VtxFormat::Position | VtxFormat::Color;
        header.vtxNum = pointNum;
        
        auto& aabbMin = aabb.min;
        auto& aabbMax = aabb.max;

        header.aabbMin[0] = aabbMin.x;
        header.aabbMin[1] = aabbMin.y;
        header.aabbMin[2] = aabbMin.z;

        header.aabbMax[0] = aabbMax.x;
        header.aabbMax[1] = aabbMax.y;
        header.aabbMax[2] = aabbMax.z;

        fseek(fp, 0, SEEK_SET);
        fwrite(&header, sizeof(header), 1, fp);
    }

    fclose(fp);

	return 0;
}
Ejemplo n.º 9
0
UsersFilms::UsersFilms(){
	ifstream inFile("DataBase.dat", ios::binary);
	CoupleID * tempObj = new CoupleID();
	while (inFile >> *tempObj) listID.add(*tempObj);
	inFile.close();
}
int main(int argc, char **argv) {
    if (argc == 1)
        PrintHelpInfo();
    else {
        if (!ReadParameter(argc, argv)) {
            std::cerr << "Bad Parameters.\n";
            return 1;
        }
        ReadConfig(configFileName);
        if (compress) {
            // Compress
            db_compress::Compressor compressor(outputFileName, schema, config);
            int iter_cnt = 0;
            while (1) {
                std::cout << "Iteration " << ++iter_cnt << " Starts\n";
                std::ifstream inFile(inputFileName);
                std::string str;
                int tuple_cnt = 0;
                while (std::getline(inFile,str)) {
                    std::stringstream sstream(str);
                    std::string item;
                    db_compress::Tuple tuple(schema.attr_type.size());

                    size_t count = 0;
                    while (std::getline(sstream, item, ',')) {
                        AppendAttr(&tuple, item, attr_type[count], count);
                        ++ count;
                    }
                    // The last item might be empty string
                    if (str[str.length() - 1] == ',') {
                        AppendAttr(&tuple, "", attr_type[count], count);
                        ++ count;
                    }
                    if (count != attr_type.size()) {
                        std::cerr << "File Format Error!\n";
                    }
                    compressor.ReadTuple(tuple);
                    if (!compressor.RequireFullPass() && 
                        ++ tuple_cnt >= NonFullPassStopPoint) {
                        break;
                    }
                }
                compressor.EndOfData();
                if (!compressor.RequireMoreIterations()) 
                    break;
            }
        } else {
            // Decompress
            db_compress::Decompressor decompressor(inputFileName, schema);
            std::ofstream outFile(outputFileName);
            decompressor.Init();
            while (decompressor.HasNext()) {
                db_compress::Tuple tuple(attr_type.size());
                decompressor.ReadNextTuple(&tuple);
                for (size_t i = 0; i < attr_type.size(); ++i) {
                    std::string str = ExtractAttr(tuple, attr_type[i], i);
                    outFile << str << (i == attr_type.size() - 1 ? '\n' : ',');
                } 
            }
        }
    }
    return 0;
}
Ejemplo n.º 11
0
void ReadSpecAOD(const char *fileName = "AliMuonAOD.root") {

    gSystem->Load("libTree");
    gSystem->Load("libGeom");
    gSystem->Load("libSTEERBase");
    gSystem->Load("libAOD");
    gSystem->Load("libANALYSIS");			
    gSystem->Load("libPWGHFbase");



  gStyle->SetOptStat(111111);
  gStyle->SetFrameFillColor(10);
  gStyle->SetCanvasColor(10);
  gStyle->SetOptStat(0);
  TH1F *mass=new TH1F("mass","Invariant mass",100,0.,20.);
  TH1F *rap=new TH1F("rap","Rapidity",100,-5.,0.);
  TH1F *cost=new TH1F("cost","Cost_CS",100,-1.,1.);
  TH1F *pt=new TH1F("pt","Pt",100,0.,50.);
  TH1F *ptmuon=new TH1F("ptmuon","single muon Pt",100,0.,50.);

  // open input file and get the TTree
  TFile inFile(fileName, "READ");
  TTree *aodTree = (TTree*)inFile.Get("AOD");

  AliAODEvent *aod = (AliAODEvent*)aodTree->GetUserInfo()->FindObject("AliAODEvent");

  TClonesArray *Dimuons;
  TClonesArray *tracks;
  TClonesArray *vertices;
  AliAODEventInfo *MuonInfos;
  
  aodTree->SetBranchAddress("Dimuons",&Dimuons);
  aodTree->SetBranchAddress("tracks",&tracks);
  aodTree->SetBranchAddress("vertices",&vertices);
  aodTree->SetBranchAddress("MuonInfos",&MuonInfos);

  // loop over events
  Int_t nEvents = aodTree->GetEntries();
  for (Int_t nEv = 0; nEv < nEvents; nEv++) {
    cout << "Event: " << nEv+1 << "/" << nEvents << endl;
    aodTree->GetEntry(nEv);
    // loop over tracks
    Int_t nTracks = tracks->GetEntries();
    for (Int_t nTr = 0; nTr < nTracks; nTr++) {
      AliAODTrack *tr = (AliAODTrack *)tracks->At(nTr);
      ptmuon->Fill(tr->Pt());
      // print track info
      cout << nTr+1 << "/" << nTracks << ": track pt: " << tr->Pt();
      if (tr->GetProdVertex()) {
	cout << ", vertex z of this track: " << tr->GetProdVertex()->GetZ();
      }
      cout << endl;
    }
    // loop over dimuons
    
    Int_t nDimuons = Dimuons->GetEntries();
    cout << nDimuons << " dimuon(s)" << endl;
    for(Int_t nDi=0; nDi < nDimuons; nDi++){
      AliAODDimuon *di=(AliAODDimuon*)Dimuons->At(nDi);
       if((MuonInfos->MUON_Unlike_HPt_L0())){
         mass->Fill(di->M());
         pt->Fill(di->Pt());
         rap->Fill(di->Y());
         cost->Fill(di->CostCS());
         cout << "Dimuon: " << nDi << " q: " << di->Charge() 
	      << " m: " << di->M() << " Y: " << di->Y() << " Pt: " << di->Pt()<< " CostCS: " << di->CostCS() << endl  ;
       }
    }
//     // loop over vertices
//     Int_t nVtxs = vertices->GetEntries();
//     for (Int_t nVtx = 0; nVtx < nVtxs; nVtx++) {
//       
//       // print track info
//       cout << nVtx+1 << "/" << nVtxs << ": vertex z position: " <<vertices->At(nVtx)->GetZ() << endl;
//     }
  }
  inFile.Close();
  TCanvas *c1=new TCanvas();
  c1->Show();
  c1->Divide(2,2);
  c1->ForceUpdate();
  c1->cd(1);
  mass->DrawClone();
  c1->cd(2);
  rap->DrawClone();
  c1->cd(3);
  pt->DrawClone();
  c1->cd(4);
  cost->DrawClone();
  
  TCanvas *c2 = new TCanvas();
  c2->cd();
  ptmuon->DrawClone();
  return;
}
Ejemplo n.º 12
0
bool maxiSample::read()
{
	bool result;
	ifstream inFile( myPath.c_str(), ios::in | ios::binary);
	result = inFile;
	if (inFile) {
		bool datafound = false;
		inFile.seekg(4, ios::beg);
		inFile.read( (char*) &myChunkSize, 4 ); // read the ChunkSize
		
		inFile.seekg(16, ios::beg);
		inFile.read( (char*) &mySubChunk1Size, 4 ); // read the SubChunk1Size
		
		//inFile.seekg(20, ios::beg);
		inFile.read( (char*) &myFormat, sizeof(short) ); // read the file format.  This should be 1 for PCM
		
		//inFile.seekg(22, ios::beg);
		inFile.read( (char*) &myChannels, sizeof(short) ); // read the # of channels (1 or 2)
		
		//inFile.seekg(24, ios::beg);
		inFile.read( (char*) &mySampleRate, sizeof(int) ); // read the samplerate
		
		//inFile.seekg(28, ios::beg);
		inFile.read( (char*) &myByteRate, sizeof(int) ); // read the byterate
		
		//inFile.seekg(32, ios::beg);
		inFile.read( (char*) &myBlockAlign, sizeof(short) ); // read the blockalign
		
		//inFile.seekg(34, ios::beg);
		inFile.read( (char*) &myBitsPerSample, sizeof(short) ); // read the bitspersample
		
		//ignore any extra chunks
		char chunkID[5]="";
		chunkID[4] = 0;
		int filePos = 36;
		while(!datafound && !inFile.eof()) {
			inFile.seekg(filePos, ios::beg);
			inFile.read((char*) &chunkID, sizeof(char) * 4);
			inFile.seekg(filePos + 4, ios::beg);
			inFile.read( (char*) &myDataSize, sizeof(int) ); // read the size of the data
			filePos += 8;
			if (strcmp(chunkID,"data") == 0) {
				datafound = true;
			}else{
				filePos += myDataSize;
			}
		}
		
		// read the data chunk
		char *myData = (char*) malloc(myDataSize * sizeof(char));
		inFile.seekg(filePos, ios::beg);
		inFile.read(myData, myDataSize);
		length=myDataSize*(0.5/myChannels);
		inFile.close(); // close the input file
		
        cout << "Ch: " << myChannels << ", len: " << length << endl;
		if (myChannels>1) {
			int position=0;
			int channel=readChannel*2;
			for (int i=channel;i<myDataSize+6;i+=(myChannels*2)) {
				myData[position]=myData[i];
				myData[position+1]=myData[i+1];
				position+=2;
			}
		}
        temp = (short*) malloc(myDataSize * sizeof(char));
        memcpy(temp, myData, myDataSize * sizeof(char));
        
        free(myData);
		
	}else {
//		cout << "ERROR: Could not load sample: " <<myPath << endl; //This line seems to be hated by windows 
        printf("ERROR: Could not load sample.");

	}
	
	
	return result; // this should probably be something more descriptive
}
Ejemplo n.º 13
0
void MDAL::DriverFlo2D::parseTIMDEPFile( const std::string &datFileName, const std::vector<double> &elevations )
{
  // TIMDEP.OUT
  // this file is optional, so if not present, reading is skipped
  // time (separate line)
  // For every Vertex:
  // FLO2D: ELEM NUMber (indexed from 1), depth, velocity, velocity x, velocity y
  // FLO2DPro: ELEM NUMber (indexed from 1), depth, velocity, velocity x, velocity y, water surface elevation
  std::string inFile( fileNameFromDir( datFileName, "TIMDEP.OUT" ) );
  if ( !MDAL::fileExists( inFile ) )
  {
    return;
  }

  std::ifstream inStream( inFile, std::ifstream::in );
  std::string line;

  size_t nVertexs = mMesh->verticesCount();
  size_t ntimes = 0;

  double time = 0.0;
  size_t face_idx = 0;

  std::shared_ptr<DatasetGroup> depthDsGroup = std::make_shared< DatasetGroup >(
        name(),
        mMesh.get(),
        datFileName,
        "Depth"
      );
  depthDsGroup->setIsOnVertices( false );
  depthDsGroup->setIsScalar( true );


  std::shared_ptr<DatasetGroup> waterLevelDsGroup = std::make_shared< DatasetGroup >(
        name(),
        mMesh.get(),
        datFileName,
        "Water Level"
      );
  waterLevelDsGroup->setIsOnVertices( false );
  waterLevelDsGroup->setIsScalar( true );

  std::shared_ptr<DatasetGroup> flowDsGroup = std::make_shared< DatasetGroup >(
        name(),
        mMesh.get(),
        datFileName,
        "Velocity"
      );
  flowDsGroup->setIsOnVertices( false );
  flowDsGroup->setIsScalar( false );

  std::shared_ptr<MDAL::MemoryDataset> flowDataset;
  std::shared_ptr<MDAL::MemoryDataset> depthDataset;
  std::shared_ptr<MDAL::MemoryDataset> waterLevelDataset;

  while ( std::getline( inStream, line ) )
  {
    line = MDAL::rtrim( line );
    std::vector<std::string> lineParts = MDAL::split( line, ' ' );
    if ( lineParts.size() == 1 )
    {
      time = MDAL::toDouble( line );
      ntimes++;

      if ( depthDataset ) addDatasetToGroup( depthDsGroup, depthDataset );
      if ( flowDataset ) addDatasetToGroup( flowDsGroup, flowDataset );
      if ( waterLevelDataset ) addDatasetToGroup( waterLevelDsGroup, waterLevelDataset );

      depthDataset  = std::make_shared< MemoryDataset >( depthDsGroup.get() );
      flowDataset = std::make_shared< MemoryDataset >( flowDsGroup.get() );
      waterLevelDataset = std::make_shared< MemoryDataset >( waterLevelDsGroup.get() );

      depthDataset->setTime( time );
      flowDataset->setTime( time );
      waterLevelDataset->setTime( time );

      face_idx = 0;

    }
    else if ( ( lineParts.size() == 5 ) || ( lineParts.size() == 6 ) )
    {
      // new Vertex for time
      if ( !depthDataset || !flowDataset || !waterLevelDataset ) throw MDAL_Status::Err_UnknownFormat;
      if ( face_idx == nVertexs ) throw MDAL_Status::Err_IncompatibleMesh;

      // this is magnitude: getDouble(lineParts[2]);
      flowDataset->values()[2 * face_idx] = getDouble( lineParts[3] );
      flowDataset->values()[2 * face_idx + 1] = getDouble( lineParts[4] );

      double depth = getDouble( lineParts[1] );
      depthDataset->values()[face_idx] = depth;

      if ( !is_nodata( depth ) ) depth += elevations[face_idx];
      waterLevelDataset->values()[face_idx] = depth;

      face_idx ++;

    }
    else
    {
      throw MDAL_Status::Err_UnknownFormat;
    }
  }

  if ( depthDataset ) addDatasetToGroup( depthDsGroup, depthDataset );
  if ( flowDataset ) addDatasetToGroup( flowDsGroup, flowDataset );
  if ( waterLevelDataset ) addDatasetToGroup( waterLevelDsGroup, waterLevelDataset );

  depthDsGroup->setStatistics( MDAL::calculateStatistics( depthDsGroup ) );
  flowDsGroup->setStatistics( MDAL::calculateStatistics( flowDsGroup ) );
  waterLevelDsGroup->setStatistics( MDAL::calculateStatistics( waterLevelDsGroup ) );

  mMesh->datasetGroups.push_back( depthDsGroup );
  mMesh->datasetGroups.push_back( flowDsGroup );
  mMesh->datasetGroups.push_back( waterLevelDsGroup );
}
Ejemplo n.º 14
0
int createSimplePoem(const std::string inFileName)
{
  if(!checkOutputTxtFile(inFileName)) return -1;

  std::ifstream inFile(inFileName);
  std::string str;
  std::vector<std::string>* strVect_p = new std::vector<std::string>;

  while(std::getline(inFile, str)){
    strVect_p->push_back(str);
  }
  inFile.close();

  std::srand(unsigned(std::time(NULL)));
  std::random_shuffle(strVect_p->begin(), strVect_p->end(), myrandom);

  std::vector<std::string>* poemVect_p = new std::vector<std::string>;
  
  bool outOfMatches = false;
  while((int)poemVect_p->size() < 10){

    int strSize = (int)strVect_p->size();
    
    bool isMatch = false;
    for(int iter = 0; iter < strSize-1; iter++){
      std::string sentence1 = strVect_p->at(iter);
      std::vector<std::string>* words1_p = new std::vector<std::string>;

      getWordsFromSentence(sentence1, words1_p);

      for(int iter2 = 1; iter2 < strSize; iter2++){
	std::string sentence2 = strVect_p->at(iter2);
	std::vector<std::string>* words2_p = new std::vector<std::string>;

	getWordsFromSentence(sentence2, words2_p);

	for(int wordIter1 = 0; wordIter1 < (int)words1_p->size(); wordIter1++){
	  std::string word1 = words1_p->at(wordIter1);

	  for(int wordIter2 = 0; wordIter2 < (int)words2_p->size(); wordIter2++){
	    std::string word2 = words2_p->at(wordIter2);

	    if(word1.find(word2) != std::string::npos || word2.find(word1) != std::string::npos){
	      isMatch = true;
	      break;
	    }
	  }
	  if(isMatch) break;
	}

	if(isMatch){
	  poemVect_p->push_back(sentence1);
	  poemVect_p->push_back(sentence2);

	  strVect_p->erase(strVect_p->begin()+iter2);
	  strVect_p->erase(strVect_p->begin()+iter);
	}
	else if(iter == strSize - 2) outOfMatches = true;

	words2_p->clear();
	delete words2_p;

	if(isMatch) break;
      }


      words1_p->clear();
      delete words1_p;


      if(isMatch) break;
    }
  
    if(outOfMatches) break;
  }


  std::string outFileName = inFileName;
  outFileName.replace(outFileName.find(".txt"), 4, "");
  outFileName = outFileName + "_SimplePoem.txt";

  std::ofstream outFile(outFileName);
  for(int iter = 0; iter < (int)poemVect_p->size(); iter++){
    outFile << poemVect_p->at(iter) << std::endl;
  }
  outFile.close();

  poemVect_p->clear();
  delete poemVect_p;

  strVect_p->clear();
  delete strVect_p;

  return 0;
}
Ejemplo n.º 15
0
// Compute mean and covariance from a CSV file of features
Mat AudioAnalyzer::CSVToCovariance(string filename)
{

	// CSV Parsing    
    Mat matrice;
    Mat result;
    int nframes = 0, nitems = 0;
    
    // Get the CSV file as a stream
    ifstream inFile (filename.c_str());
     if (inFile.is_open()) {
		 // Retrieve the number of frames of the file (=number of lines - header)
		nframes = count(istreambuf_iterator<char>(inFile), 
                 istreambuf_iterator<char>(), '\n') - 1;
		

		// Go back to the beginning of the file
		inFile.seekg(0);
		int linenum = 0;
		int itemnum = 0;
		string line;
		while (getline (inFile, line))
		{
			//cout << "\nLine #" << linenum << ":" << endl;
			istringstream linestream(line);
			string item;
			itemnum = 0;
			// forget firts two cols
			getline (linestream, item, ';');
			getline (linestream, item, ';');
			while (getline (linestream, item, ';'))
			{
				if (linenum > 0)																		// CHECK WITH NEW CONF FILE !!!!!!!!!
				{
					matrice.at<float>(linenum-1, itemnum) = (float)atof(item.c_str());
					//cout << "Item #" << itemnum << ": " << item << "-"<<matrice.at<float>(linenum-1, itemnum)<< endl;
				}
				itemnum++;
			}
			if (linenum == 0)
			{
				nitems = itemnum;
				matrice.create(nframes, nitems, CV_32F);
			} 
			linenum++;
		}  
		inFile.close();
		//cout << "matrice.cols : " << matrice.cols << " / matrice.rows " << matrice.rows << endl;
    } else  {
		cout << "Error: can't open CSV file." << endl;
    }
    
    // If the matrix has been successfully populated from the CSV file
    if (matrice.rows > 0 && matrice.cols > 0) {
		// Calculate the mean vector the covariance matrix
		Mat covar(0,0,CV_32F);
		Mat mean(0,0,CV_32F);
		calcCovarMatrix(matrice, covar, mean, CV_COVAR_ROWS | CV_COVAR_NORMAL, CV_32F);
		
		//cout << "CSVToCovariance: covariance ("<<covar.cols<< "," << covar.rows << ") computed for "<<nitems<<" features on "<<nframes<<" frames."<<endl;
		
		
		// Init the result matrix :
		// - number of rows : 1
		// - number of columns : nitems (means) + sum(n-i, i=0..n) (variance)
		result.create(1, (int)(nitems*(1 + (float)(nitems+1)/2)), CV_32F);
		int index = 0;
		// Add the mean to the matrix
	
		for(int j=0; j<mean.cols; j++) {
			result.at<float>(0,index) = mean.at<float>(0,j);
			index++;
		}
		
		for(int i=0; i<covar.rows; i++) {
			for(int j=i; j<covar.cols; j++) {
				result.at<float>(0,index) = covar.at<float>(i,j);
				index++;
			}
		}	


    
	}  else {
		cout << "Error while parsing CSV file" << endl;
	}
	
	
	return result;
}
Ejemplo n.º 16
0
int main(int argc, char **argv)
{
	InitializeFast();

	Backend backend;

	ANNEvaluator evaluator;

	ANNMoveEvaluator mevaluator(evaluator);

	// if eval.net exists, use the ANN evaluator
	// if both eval.net and meval.net exist, use the ANN move evaluator

	if (FileReadable(EvalNetFilename))
	{
		backend.SetEvaluator(&evaluator);

		std::cout << "# Using ANN evaluator" << std::endl;

		if (FileReadable(MoveEvalNetFilename))
		{
			std::cout << "# Using ANN move evaluator" << std::endl;
			backend.SetMoveEvaluator(&mevaluator);
		}
		else
		{
			std::cout << "# Using static move evaluator" << std::endl;
			backend.SetMoveEvaluator(&gStaticMoveEvaluator);
		}
	}
	else
	{
		std::cout << "# Using static evaluator" << std::endl;
		std::cout << "# Using static move evaluator" << std::endl;

		backend.SetEvaluator(&Eval::gStaticEvaluator);
		backend.SetMoveEvaluator(&gStaticMoveEvaluator);
	}

	// first we handle special operation modes
	if (argc >= 2 && std::string(argv[1]) == "tdl")
	{
		InitializeSlowBlocking(evaluator, mevaluator);

		if (argc < 3)
		{
			std::cout << "Usage: " << argv[0] << " tdl positions" << std::endl;
			return 0;
		}
        //try 
        //{
	    Learn::TDL(argv[2]);
        //}
        //catch(...){}
		return 0;
	}
	else if (argc >= 2 && std::string(argv[1]) == "conv")
	{
		InitializeSlowBlocking(evaluator, mevaluator);

		if (argc < 3)
		{
			std::cout << "Usage: " << argv[0] << " conv FEN" << std::endl;
			return 0;
		}

		std::stringstream ss;

		for (int i = 2; i < argc; ++i)
		{
			ss << argv[i] << ' ';
		}

		Board b(ss.str());

		std::vector<FeaturesConv::FeatureDescription> ret;
		FeaturesConv::ConvertBoardToNN(b, ret);

		return 0;
	}
    else if(argc >= 2 && std::string(argv[1]) == "train_eval")
    {
        // ./giraffe train_eval <list filename> <epd_data_path> <epd_label_path> <epochs> 

        InitializeSlowBlocking(evaluator, mevaluator);
        std::ifstream epd_file_list(argv[2]);
        std::string epd_data_path = argv[3];
        std::string epd_label_path = argv[4];
        std::string epd_filename;
        std::vector<std::string> filenames;
        while(std::getline(epd_file_list, epd_filename)){
            filenames.push_back(epd_filename);
        }
        int epochs = std::stoi(argv[5]);
        
        for(int i = 0; i < epochs*filenames.size(); i++){
            std::string epd_path_full = epd_data_path + "/" + filenames[i % filenames.size()];
            std::string label_path_full = epd_label_path + "/" + filenames[i % filenames.size()] + ".xie";
            std::cout << label_path_full << std::endl;
            std::ifstream epd_file(epd_path_full);
            std::ifstream label_file(label_path_full);
            std::string fen;
            std::vector<std::string> fens;
            //std::cout << "Reading FENS" << std::endl;
            while(std::getline(epd_file, fen))
            {
                fens.push_back(fen);
            } 
            //std::cout << "Reading labels" << std::endl;
            
            std::string label;
            NNMatrixRM mat_labels = NNMatrixRM(fens.size(), 1);
            //std::vector<int> labels; 
            int idx = 0;
            while(std::getline(label_file, label)){
                //labels.push_back(stoi(label));
                mat_labels(idx, 0) = std::stoi(label);
                idx++; 
            }
            //std::cout << "Getting feature descriptions" << std::endl;
            Board dummy;
            std::vector<FeaturesConv::FeatureDescription> ret;
            FeaturesConv::ConvertBoardToNN(dummy, ret);

            //std::cout << "Starting Training" << std::endl;
            
            std::ofstream outNet(argv[6]);
            //evaluator.Serialize(outNet);
            evaluator.TrainLoop(fens, mat_labels, 10, ret);
            evaluator.Serialize(outNet);
        }
/*
        std::ifstream epd_file(argv[2]);
        std::ifstream label_file(argv[3]);
        std::string fen;
        std::vector<std::string> fens;
        std::cout << "Reading FENS" << std::endl;
        while(std::getline(epd_file, fen))
        {
            fens.push_back(fen);
        } 
        std::cout << "Reading labels" << std::endl;
        
        std::string label;
        NNMatrixRM mat_labels = NNMatrixRM(fens.size(), 1);
        //std::vector<int> labels; 
        int idx = 0;
        while(std::getline(label_file, label)){
            //labels.push_back(stoi(label));
            mat_labels(idx, 0) = std::stoi(label);
            idx++; 
        }
        std::cout << "Getting feature descriptions" << std::endl;
        int epochs = std::stoi(argv[4]);
        Board dummy;
		std::vector<FeaturesConv::FeatureDescription> ret;
		FeaturesConv::ConvertBoardToNN(dummy, ret);

        std::cout << "Starting Training" << std::endl;
        
        std::ofstream outNet(argv[5]);
        evaluator.Serialize(outNet);
        evaluator.TrainLoop(fens, mat_labels, epochs, ret);
        evaluator.Serialize(outNet);
  */      
        return 0;
    }
    else if (argc >= 2 && std::string(argv[1]) == "conv_file")
    {
        InitializeSlowBlocking(evaluator, mevaluator);
        
        std::ifstream inFile(argv[2]);
        std::ofstream outFile(argv[3]);

        std::string fen;
        std::vector<std::string> fens;
        while(std::getline(inFile, fen))
        {
            fens.push_back(fen);
            std::cout << fen << std::endl;
            /*
            Board b(fen);
		    std::vector<FeaturesConv::FeatureDescription> ret;
		    FeaturesConv::ConvertBoardToNN(b, ret);
            std::stringstream ss;
            for (int i = 0; i < ret.size()-1; i++)
            {
                ss << ret[i].XieToString() << " ";

            }
            ss << ret[ret.size() - 1].XieToString();
            outFile << ss.str() << std::endl;
            std::cout << ss.str() << std::endl;
            std::cout << "****" << std::endl;
            */
        }

        std::vector<FeaturesConv::FeatureDescription> dummy(363);

        NNMatrixRM ret = evaluator.BoardsToFeatureRepresentation_(fens, dummy);
        for(int64_t row = 0; row < ret.rows(); ++row)
        {
            for(int64_t col = 0; col < ret.cols(); ++ col)
            {
                outFile << ret(row,col) << ' '; 
            }
            outFile << '\n';
        }


        inFile.close();
        outFile.close();

        return 0;

    }
	else if (argc >= 2 && std::string(argv[1]) == "mconv")
	{
		InitializeSlowBlocking(evaluator, mevaluator);

		if (argc < 3)
		{
			std::cout << "Usage: " << argv[0] << " mconv FEN" << std::endl;
			return 0;
		}

		std::stringstream ss;

		for (int i = 2; i < argc; ++i)
		{
			ss << argv[i] << ' ';
		}

		Board b(ss.str());

		MoveList moves;
		b.GenerateAllLegalMoves<Board::ALL>(moves);

		NNMatrixRM ret;

		FeaturesConv::ConvertMovesInfo convInfo;

		FeaturesConv::ConvertMovesToNN(b, convInfo, moves, ret);

		for (int64_t row = 0; row < ret.rows(); ++row)
		{
			for (int64_t col = 0; col < ret.cols(); ++col)
			{
				std::cout << ret(row, col) << ' ';
			}
			std::cout << std::endl;
		}

		return 0;
	}
	else if (argc >= 2 && std::string(argv[1]) == "bench")
	{
		InitializeSlowBlocking(evaluator, mevaluator);

		double startTime = CurrentTime();

		static const NodeBudget BenchNodeBudget = 64*1024*1024;

		Search::SyncSearchNodeLimited(Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"), BenchNodeBudget, backend.GetEvaluator(), backend.GetMoveEvaluator());
		Search::SyncSearchNodeLimited(Board("2r2rk1/pp3pp1/b2Pp3/P1Q4p/RPqN2n1/8/2P2PPP/2B1R1K1 w - - 0 1"), BenchNodeBudget, backend.GetEvaluator(), backend.GetMoveEvaluator());
		Search::SyncSearchNodeLimited(Board("8/1nr3pk/p3p1r1/4p3/P3P1q1/4PR1N/3Q2PK/5R2 w - - 0 1"), BenchNodeBudget, backend.GetEvaluator(), backend.GetMoveEvaluator());
		Search::SyncSearchNodeLimited(Board("5R2/8/7r/7P/5RPK/1k6/4r3/8 w - - 0 1"), BenchNodeBudget, backend.GetEvaluator(), backend.GetMoveEvaluator());
		Search::SyncSearchNodeLimited(Board("r5k1/2p2pp1/1nppr2p/8/p2PPp2/PPP2P1P/3N2P1/R3RK2 w - - 0 1"), BenchNodeBudget, backend.GetEvaluator(), backend.GetMoveEvaluator());
		Search::SyncSearchNodeLimited(Board("8/R7/8/1k6/1p1Bq3/8/4NK2/8 w - - 0 1"), BenchNodeBudget, backend.GetEvaluator(), backend.GetMoveEvaluator());

		std::cout << "Time: " << (CurrentTime() - startTime) << "s" << std::endl;

		return 0;
	}
	else if (argc >= 2 && std::string(argv[1]) == "check_bounds")
	{
		InitializeSlowBlocking(evaluator, mevaluator);

		if (argc < 3)
		{
			std::cout << "Usage: " << argv[0] << " check_bounds <EPD/FEN file>" << std::endl;
			return 0;
		}

		std::ifstream infile(argv[2]);

		if (!infile)
		{
			std::cerr << "Failed to open " << argv[2] << " for reading" << std::endl;
			return 1;
		}

		uint64_t passes = 0;
		uint64_t total = 0;
		float windowSizeTotal = 0.0f;

		std::string fen;
		std::vector<std::string> fens;
		while (std::getline(infile, fen))
		{
			fens.push_back(fen);
		}

		#pragma omp parallel
		{
			auto evaluatorCopy = evaluator;

			#pragma omp for
			for (size_t i = 0; i < fens.size(); ++i)
			{
				Board b(fens[i]);
				float windowSize = 0.0f;
				bool res = evaluatorCopy.CheckBounds(b, windowSize);

				#pragma omp critical(boundCheckAccum)
				{
					if (res)
					{
						++passes;
					}

					++total;

					windowSizeTotal += windowSize;
				}
			}
		}

		std::cout << passes << "/" << total << std::endl;
		std::cout << "Average window size: " << (windowSizeTotal / total) << std::endl;

		return 0;
	}
	else if (argc >= 2 && std::string(argv[1]) == "train_bounds")
	{
		InitializeSlowBlocking(evaluator, mevaluator);

		if (argc < 4)
		{
			std::cout << "Usage: " << argv[0] << " train_bounds <EPD/FEN file> <output net file>" << std::endl;
			return 0;
		}

		std::ifstream infile(argv[2]);

		if (!infile)
		{
			std::cerr << "Failed to open " << argv[2] << " for reading" << std::endl;
			return 1;
		}

		std::vector<FeaturesConv::FeatureDescription> featureDescriptions;
		Board dummyBoard;
		FeaturesConv::ConvertBoardToNN(dummyBoard, featureDescriptions);

		std::string line;
		std::vector<std::string> fens;
		while (std::getline(infile, line))
		{
			fens.push_back(line);
		}

		const size_t BlockSize = 256;
		const size_t PrintInterval = BlockSize * 100;

		for (size_t i = 0; i < (fens.size() - BlockSize); i += BlockSize)
		{
			if (i % PrintInterval == 0)
			{
				std::cout << i << "/" << fens.size() << std::endl;
			}

			std::vector<std::string> positions;

			for (size_t j = 0; j < BlockSize; ++j)
			{
				positions.push_back(fens[i + j]);
			}

			evaluator.TrainBounds(positions, featureDescriptions, 1.0f);
		}

		std::ofstream outfile(argv[3]);

		if (!outfile)
		{
			std::cerr << "Failed to open " << argv[3] << " for writing" << std::endl;
			return 1;
		}

		evaluator.Serialize(outfile);

		return 0;
	}
	else if (argc >= 2 && std::string(argv[1]) == "sample_internal")
	{
		// MUST UNCOMMENT "#define SAMPLING" in static move evaluator

		InitializeSlowBlocking(evaluator, mevaluator);

		if (argc < 4)
		{
			std::cout << "Usage: " << argv[0] << " sample_internal <EPD/FEN file> <output file>" << std::endl;
			return 0;
		}

		std::ifstream infile(argv[2]);
		std::ofstream outfile(argv[3]);

		if (!infile)
		{
			std::cerr << "Failed to open " << argv[2] << " for reading" << std::endl;
			return 1;
		}

		std::string fen;
		std::vector<std::string> fens;
		static const uint64_t maxPositions = 5000000;
		uint64_t numPositions = 0;
		while (std::getline(infile, fen) && numPositions < maxPositions)
		{
			fens.push_back(fen);
			++numPositions;
		}

		#pragma omp parallel
		{
			auto evaluatorCopy = evaluator;

			#pragma omp for
			for (size_t i = 0; i < fens.size(); ++i)
			{
                std::cout << i << std::endl;
				Board b(fens[i]);

				Search::SyncSearchNodeLimited(b, 1000, &evaluatorCopy, &gStaticMoveEvaluator, nullptr, nullptr);
			}
		}

		for (const auto &pos : gStaticMoveEvaluator.samples)
		{
			outfile << pos << std::endl;
		}

		return 0;
	}
	else if (argc >= 2 && std::string(argv[1]) == "label_bm")
	{
		InitializeSlowBlocking(evaluator, mevaluator);

		if (argc < 4)
		{
			std::cout << "Usage: " << argv[0] << " label_bm <EPD/FEN file> <output file>" << std::endl;
			return 0;
		}

		std::ifstream infile(argv[2]);
		std::ofstream outfile(argv[3]);

		if (!infile)
		{
			std::cerr << "Failed to open " << argv[2] << " for reading" << std::endl;
			return 1;
		}

		std::string fen;
		std::vector<std::string> fens;
		static const uint64_t maxPositions = 5000000;
		uint64_t numPositions = 0;
		while (std::getline(infile, fen) && numPositions < maxPositions)
		{
			Board b(fen);

			if (b.GetGameStatus() != Board::ONGOING)
			{
				continue;
			}

			fens.push_back(fen);
			++numPositions;
		}

		std::vector<std::string> bm(fens.size());

		uint64_t numPositionsDone = 0;

		double lastPrintTime = CurrentTime();
		size_t lastDoneCount = 0;

		#pragma omp parallel
		{
			auto evaluatorCopy = evaluator;

			#pragma omp for schedule(dynamic)
			for (size_t i = 0; i < fens.size(); ++i)
			{
				Board b(fens[i]);

				Search::SearchResult result = Search::SyncSearchNodeLimited(b, 100000, &evaluatorCopy, &gStaticMoveEvaluator, nullptr, nullptr);

				bm[i] = b.MoveToAlg(result.pv[0]);

				#pragma omp critical(numPositionsAndOutputFileUpdate)
				{
					++numPositionsDone;

					outfile << fens[i] << '\n';
					outfile << bm[i] << '\n';

					if (omp_get_thread_num() == 0)
					{
						double currentTime = CurrentTime();
						double timeDiff = currentTime - lastPrintTime;
						if (timeDiff > 1.0)
						{
							std::cout << numPositionsDone << '/' << fens.size() << std::endl;
							std::cout << "Positions per second: " << static_cast<double>(numPositionsDone - lastDoneCount) / timeDiff << std::endl;

							lastPrintTime = currentTime;
							lastDoneCount = numPositionsDone;
						}
					}
				}
			}
		}

		return 0;
	}
	else if (argc >= 2 && std::string(argv[1]) == "train_move_eval")
	{
		InitializeSlowBlocking(evaluator, mevaluator);

		if (argc < 4)
		{
			std::cout << "Usage: " << argv[0] << " train_move_eval <EPD/FEN file> <output file>" << std::endl;
			return 0;
		}

		std::ifstream infile(argv[2]);

		if (!infile)
		{
			std::cerr << "Failed to open " << argv[2] << " for reading" << std::endl;
			return 1;
		}

		std::cout << "Reading positions from " << argv[2] << std::endl;

		std::string fen;
		std::string bestMove;
		std::vector<std::string> fens;
		std::vector<std::string> bestMoves;
		static const uint64_t MaxPositions = 5000000;
		uint64_t numPositions = 0;
		while (std::getline(infile, fen) && std::getline(infile, bestMove) && numPositions < MaxPositions)
		{
			Board b(fen);

			if (b.GetGameStatus() != Board::ONGOING)
			{
				continue;
			}

			fens.push_back(fen);
			bestMoves.push_back(bestMove);

			++numPositions;
		}

		assert(bestMoves.size() == fens.size());

		// now we split a part of it out into a withheld test set
		size_t numTrainExamples = fens.size() * 0.9f;
		std::vector<std::string> fensTest(fens.begin() + numTrainExamples, fens.end());
		std::vector<std::string> bestMovesTest(bestMoves.begin() + numTrainExamples, bestMoves.end());

		static const uint64_t MaxTestingPositions = 10000;

		if (fensTest.size() > MaxTestingPositions)
		{
			fensTest.resize(MaxTestingPositions);
			bestMovesTest.resize(MaxTestingPositions);
		}

		fens.resize(numTrainExamples);
		bestMoves.resize(numTrainExamples);

		std::cout << "Num training examples: " << numTrainExamples << std::endl;
		std::cout << "Num testing examples: " << fensTest.size() << std::endl;

		std::cout << "Starting training" << std::endl;

		ANNMoveEvaluator meval(evaluator);

		meval.Train(fens, bestMoves);

		meval.Test(fensTest, bestMovesTest);

		std::ofstream outfile(argv[3]);

		meval.Serialize(outfile);

		return 0;
	}

	// we need a mutex here because InitializeSlow needs to print, and it may decide to
	// print at the same time as the main command loop (if the command loop isn't waiting)
	std::mutex coutMtx;

	coutMtx.lock();

	// do all the heavy initialization in a thread so we can reply to "protover 2" in time
	std::thread initThread(InitializeSlow, std::ref(evaluator), std::ref(mevaluator), std::ref(coutMtx));

	auto waitForSlowInitFunc = [&initThread, &coutMtx]() { coutMtx.unlock(); initThread.join(); coutMtx.lock(); };

	while (true)
	{
		std::string lineStr;

		coutMtx.unlock();
		std::getline(std::cin, lineStr);
		coutMtx.lock();

		std::stringstream line(lineStr);

		// we set usermove=1, so all commands from xboard start with a unique word
		std::string cmd;
		line >> cmd;

		// this is the list of commands we can process before initialization finished
		if (
			cmd != "xboard" &&
			cmd != "protover" &&
			cmd != "hard" &&
			cmd != "easy" &&
			cmd != "cores" &&
			cmd != "memory" &&
			cmd != "accepted" &&
			cmd != "rejected" &&
			initThread.joinable())
		{
			// wait for initialization to be done
			waitForSlowInitFunc();
		}

		if (cmd == "xboard") {} // ignore since we only support xboard mode anyways
		else if (cmd == "protover")
		{
			int32_t ver;
			line >> ver;

			if (ver >= 2)
			{
				std::string name = "Giraffe";
				if (gVersion != "")
				{
					name += " ";
					name += gVersion;
				}

				std::cout << "feature ping=1 setboard=1 playother=0 san=0 usermove=1 time=1 draw=0 sigint=0 sigterm=0 "
							 "reuse=1 analyze=1 myname=\"" << name << "\" variants=normal colors=0 ics=0 name=0 pause=0 nps=0 "
							 "debug=1 memory=0 smp=0 done=0" << std::endl;

				std::cout << "feature option=\"GaviotaTbPath -path .\"" << std::endl;

				std::cout << "feature done=1" << std::endl;
			}
		}
		else if (cmd == "accepted") {}
void MangaListWidget::updateMangaInfo(QModelIndex index) {
  _editMangaInfoButton->setChecked(false);

  QStandardItem* currentItem = _model->itemFromIndex(index);

  if (!currentItem->parent()) {
    QDir mangaDirectory(_scansDirectory.path()+"/"+currentItem->text());

    if (mangaDirectory.exists("cover.png")) {
      QPixmap pixmap(mangaDirectory.path()+"/cover.png");
      pixmap = pixmap.scaled(_mangaPreviewLabel->width(), _mangaPreviewLabel->height(), Qt::KeepAspectRatio);
      _mangaPreviewLabel->setPixmap(pixmap);
      _coverHasToBeSet = false;
    } else {
      QPixmap pixmap(Utils::getIconsPath()+"/setCover.png");
      pixmap = pixmap.scaled(_mangaPreviewLabel->width(), _mangaPreviewLabel->height(), Qt::KeepAspectRatio);
      _mangaPreviewLabel->setPixmap(pixmap);
      _coverHasToBeSet = true;
    }

    QFile file(mangaDirectory.filePath("mangaInfo.txt"));
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
      QMessageBox::critical(this, "Manga info error", "Can't open "+file.fileName()+" in 'read only' whithin MangaListWidget::updateMangaInfo.");
      _genreLabel->setText("");
      _authorLabel->setText("");
      _artistLabel->setText("");
      _publisherLabel->setText("");
      _magazineLabel->setText("");
      _startDateLabel->setText("");
      return;
    }

    QTextStream inFile(&file);
    inFile.setCodec("UTF-8");

    QStringList infosList = inFile.readAll().split("\n", QString::SkipEmptyParts);
    for (const QString& info: infosList) {
      if (info.isEmpty())
        break;

      QStringList newInfoList = info.split(":");
      if (newInfoList.size() < 2) {
        qDebug() << "Check" << mangaDirectory.filePath("mangaInfo.txt") << "file may be corrupted.";
        break;
      }

      QString newInfo = newInfoList.at(1);

      QStringList resultList;
      if (info.startsWith("Genre")) {
        resultList = newInfo.split(" ");

        while (resultList.size() > 6)
          resultList.removeLast();
        if (resultList.size() > 3) {
          resultList.insert(3, "\n");
          _genreLabel->setText(resultList.join(" "));
        } else {
          _genreLabel->setText(newInfo);
        }
      } else if (info.startsWith("Author"))
        _authorLabel->setText(newInfo);
      else if (info.startsWith("Artist"))
        _artistLabel->setText(newInfo);
      else if (info.startsWith("Publisher"))
        _publisherLabel->setText(newInfo);
      else if (info.startsWith("Magazine")) {
        resultList = newInfo.split(" ");

        if (resultList.size() > 3) {
          resultList.insert(3, "\n");
          _magazineLabel->setText(resultList.join(" "));
        } else {
          _magazineLabel->setText(newInfo);
        }
      } else if (info.startsWith("Start Date"))
        _startDateLabel->setText(newInfo);
    }
  }
}
Ejemplo n.º 18
0
Error::ErrorCode
EvidenceManager::loadEvidenceFromFile_Continuous(const char* inFName)
{
	ifstream inFile(inFName);
	char* buffer=NULL;
	string buffstr;
	int bufflen=0;
	int lineNo=0;

	// skip the first line.
	if(inFile.good())
	{
		getline(inFile,buffstr);
	}

	while(inFile.good())
	{
		getline(inFile,buffstr);

		if(buffstr.length()<=0)
		{
			continue;
		}
		if(bufflen<=buffstr.length())
		{
			if(buffer!=NULL)
			{
				delete[] buffer;
			}
			bufflen=buffstr.length()+1;
			buffer=new char[bufflen];
		}
		strcpy(buffer,buffstr.c_str());

		//All the evidences for each variable are stored in a map, indexed by the varId
		EMAP* evidMap=new EMAP;
		char* tok=strtok(buffer,"\t");
		//The toks take the form of varid and value

		int vId = 0;
		while(tok!=NULL)
		{
			Evidence* evid = new Evidence;
			evid->assocVariable(vId);
			double varVal=log(atof(tok));
			if(isinf(varVal) || isnan(varVal))
			{
				//cout <<"Found nan! " << tok << endl;
				cerr << "Please remove zero's (" << tok << ") from the expression data. " << endl;
				exit(-1);	
			}
			evid->setEvidVal(varVal);
			(*evidMap)[vId]=evid;
			tok=strtok(NULL,"\t");
			vId++;
		}
		evidenceSet.push_back(evidMap);
		lineNo++;
	}

	inFile.close();

	cout <<"Read " << evidenceSet.size() << " different datapoints " << endl;

	return Error::SUCCESS;
}
Ejemplo n.º 19
0
int Console::loadAccelFile(string filename)
{
	keyBinds.clear();
	sTypes.clear();
	names.clear();
	level = 0;
	ifstream inFile (filename.c_str());
	string concatWord;
	
	
	if(inFile.is_open() == false)
		return 1;
	
	
	do
	{
		do
		{
			
			tempChar = (char)inFile.get();
			if((tempChar == ' ') || (tempChar == '\n'))
			{
				break;
			}
			concatWord = concatWord + tempChar;
			if((concatWord == "end") && (level != 2))
				return 0;
				
			
		} while(! inFile.eof());
		
		switch(level)
		{
			case 0:
				if(concatWord.length() == 1)
				{
					const char* key = concatWord.c_str(); 
					keyBinds.push_back(key[0]);
				} else
				{
					inFile.close();
					return 1; //error occured
				}
				break;
			case 1:
			{
				stringstream temp(concatWord);
				int tempInt;
				temp >> tempInt;
				if((tempInt >= 0) && (tempInt <= 5))
				{
					sTypes.push_back(tempInt);
				} else
				{
					//cout << word << " has an error. Types can be 0, 1, 2, 3 or 4." << endl;
					inFile.close();
					
					return 1; //error occured
				}
			}
				break;
			case 2:
				names.push_back(concatWord);
				break;
		}
		level = (level + 1) % 3;
		concatWord = "";
		
		
	} while(! inFile.eof());
	
	
	inFile.close();
	return 0;
}
void
MediaConverterApp::_RunConvert()
{
	bigtime_t start = 0;
	bigtime_t end = 0;
	int32 audioQuality = 75;
	int32 videoQuality = 75;

	if (fWin->Lock()) {
		char *a;
		start = strtoimax(fWin->StartDuration(), &a, 0) * 1000;
		end = strtoimax(fWin->EndDuration(), &a, 0) * 1000;
		audioQuality = fWin->AudioQuality();
		videoQuality = fWin->VideoQuality();
		fWin->Unlock();
	}

	int32 srcIndex = 0;

	BMediaFile *inFile(NULL), *outFile(NULL);
	BEntry outEntry;
	entry_ref inRef;
	entry_ref outRef;
	BPath path;
	BString name;

	while (!fCancel) {
		if (fWin->Lock()) {
			status_t r = fWin->GetSourceFileAt(srcIndex, &inFile, &inRef);
			if (r == B_OK) {
				media_codec_info* audioCodec;
				media_codec_info* videoCodec;
				media_file_format* fileFormat;
				fWin->GetSelectedFormatInfo(&fileFormat, &audioCodec, &videoCodec);
				BDirectory directory = fWin->OutputDirectory();
				fWin->Unlock();
				outEntry = _CreateOutputFile(directory, &inRef, fileFormat);

				// display file name

				outEntry.GetPath(&path);
				name.SetTo(path.Leaf());

				if (outEntry.InitCheck() == B_OK) {
					entry_ref outRef;
					outEntry.GetRef(&outRef);
					outFile = new BMediaFile(&outRef, fileFormat);

					BString tmp(
						B_TRANSLATE("Output file '%filename' created"));
					tmp.ReplaceAll("%filename", name);
					name = tmp;
				} else {
					BString tmp(B_TRANSLATE("Error creating '%filename'"));
					tmp.ReplaceAll("%filename", name);
					name = tmp;
				}

				if (fWin->Lock()) {
					fWin->SetFileMessage(name.String());
					fWin->Unlock();
				}

				if (outFile != NULL) {
					r = _ConvertFile(inFile, outFile, audioCodec, videoCodec,
						audioQuality, videoQuality, start, end);

					// set mime
					update_mime_info(path.Path(), false, false, false);

					fWin->Lock();
					if (r == B_OK) {
						fWin->RemoveSourceFile(srcIndex);
					} else {
						srcIndex++;
						BString error(
							B_TRANSLATE("Error converting '%filename'"));
  						error.ReplaceAll("%filename", inRef.name);
						fWin->SetStatusMessage(error.String());
					}
					fWin->Unlock();
				}


			} else {
				fWin->Unlock();
				break;
			}
		} else {
			break;
		}
	}

	BMessenger(this).SendMessage(CONVERSION_DONE_MESSAGE);
}
Ejemplo n.º 21
0
void MyApplication::RefsReceived(BMessage *message)
{
//	be_app->Lock();

	uint32			ref_num;
	entry_ref		ref;
	BMediaTrack 	*audTrack(NULL);
	media_format	format;
	memset(&format, 0, sizeof(format));
//	media_raw_audio_format *raf(NULL);
//	short			audioFrameSize(1);
//	char			*audioData(NULL);
	int32			frame_size, channels = 1;
	
	Pool.sample_type = NONE;		// for frame moving / resize
	bool temp_pause = play_cookie.pause;;

	ref_num=0;
	if (message->FindRef("refs",ref_num, &ref) == B_OK){

		BMediaFile		inFile(&ref);
		if (inFile.InitCheck() == B_OK){

			char s[B_FILE_NAME_LENGTH +20];
			sprintf(s, "BeAE - %s", ref.name);
			mainWindow->SetTitle(s);

			Pool.sample_view_dirty = true;	// update the sample-view
			Pool.update_index = true;
			Pool.RedrawWindow();
			play_cookie.pause = true;

			// gather the necessary format information
			int32 tracks = inFile.CountTracks();
			for (int32 i = 0; i < tracks; i++) {
				BMediaTrack *inTrack = inFile.TrackAt(i);
				inTrack->EncodedFormat(&format);

				if (format.IsAudio()) {
					audTrack = inTrack;
					inTrack->DecodedFormat(&format);

//					Pool.m_format = format;
					memcpy(&Pool.m_format, &format, sizeof(Pool.m_format));

					Pool.sample_bits = (format.u.raw_audio.format & 0xf)*8;
					Pool.selection = NONE;
					Pool.frequency = format.u.raw_audio.frame_rate;

//					printf("format : %x\n", format.u.raw_audio.format);

					Pool.size = audTrack->CountFrames()-1;
					channels = format.u.raw_audio.channel_count;

					Pool.StartProgress(Language.get("LOADING_FILE"), Pool.size);
					
					frame_size = (format.u.raw_audio.format & 0xf)*channels;

#ifndef __VM_SYSTEM	//RAM
					if (Pool.sample_memory)						// create buffer for sample memory, add an extra frame to be able to do
						free(Pool.sample_memory);				//  32bit to 16 bit conversions
					
					Pool.sample_memory = (float*)malloc(Pool.size * channels *4 +1024);
#endif					
				}else{
					inFile.ReleaseAllTracks();
				}
			}

			int64 frameCount, framesRead;
			status_t err;
			media_header mh;
			int32 lastPercent, currPercent;
			float completePercent;
			BString status;
			char *buffer = (char*)malloc(format.u.raw_audio.buffer_size);		// temp memory
#ifndef __VM_SYSTEM	//RAM
			float *mem = Pool.sample_memory;									// dest memory
			// read audio from source and write to destination, if necessary
			if (mem) {
#else
			VM.Reset();

			float *convert_buffer = (float*)malloc(format.u.raw_audio.buffer_size*4);		// make sure there can be floats in it
			// read audio from source and write to destination, if necessary
			if (convert_buffer) {
				float *mem = NULL;
#endif			
				frameCount = audTrack->CountFrames();
				int64 count =0;
				lastPercent = -1;
				for (int64 i = 0; i < frameCount; i += framesRead) {
				
					#ifdef __VM_SYSTEM	//RAM
					mem = convert_buffer;
					#endif
					
					// clear buffer first
					memset( buffer, 0, format.u.raw_audio.buffer_size);
					if ((err = audTrack->ReadFrames(buffer, &framesRead, &mh)) != B_OK) {
						printf("Error reading audio frames: %s\n", strerror(err));
						break;
					}

					count += framesRead;			// now correct for crashes if bigger than file
					if (count > frameCount)
						framesRead -= (count - frameCount);
		
					switch(format.u.raw_audio.format){
					case 0x24:	// 0 == mid, -1.0 == bottom, 1.0 == top (the preferred format for non-game audio)
					{	float *tmp = (float*)buffer;
						float x;
						for (int32 count = 0; count<framesRead*channels; count++){
							x = *tmp++;
							if (x<-1.0)		x = -1.0;
							else if (x>1.0)	x = 1.0;
							*mem++ = x;
						}
					}	break;
					case 0x4:	// 0 == mid, 0x80000001 == bottom, 0x7fffffff == top (all >16-bit formats, left-adjusted)
					{	int32 *tmp = (int32*)buffer;
						float x;
						for (int32 count = 0; count<framesRead*channels; count++){
							x = *tmp++/0x80000000;
							if (x<-1.0)		x = -1.0;
							else if (x>1.0)	x = 1.0;
							*mem++ = x;
						}
					}	break;
					case 0x2:	// 0 == mid, -32767 == bottom, +32767 == top
					{	int16 *tmp = (int16*)buffer;
						float x;
						for (int32 count = 0; count<framesRead*channels; count++){
							x = *tmp++/32767.0;
							if (x<-1.0)		x = -1.0;
							else if (x>1.0)	x = 1.0;
							*mem++ = x;
						}
					}	break;
					case 0x11:	// 128 == mid, 1 == bottom, 255 == top (discouraged but supported format)
					{	uint8 *tmp = (uint8*)buffer;
						float x;
						for (int32 count = 0; count<framesRead*channels; count++){
							x = *tmp++/127.0 -1.0;
							if (x<-1.0)		x = -1.0;
							else if (x>1.0)	x = 1.0;
							*mem++ = x;
						}
					}	break;
					case 0x1:		// 0 == mid, -127 == bottom, +127 == top (not officially supported format)
					{	int8 *tmp = (int8*)buffer;
						float x;
						for (int32 count = 0; count<framesRead*channels; count++){
							x = *tmp++/127.0;		// xor 128 to invert sign bit
							if (x<-1.0)		x = -1.0;
							else if (x>1.0)	x = 1.0;
							*mem++ = x;
						}
					}	break;
					}
					
					#ifdef __VM_SYSTEM	//RAM
					VM.WriteBlock( convert_buffer, framesRead*channels );
					#endif

					Pool.ProgressUpdate( framesRead );

					completePercent = ((float)i) / ((float)frameCount) * 100;
					currPercent = (int16)floor(completePercent);
					if (currPercent > lastPercent) {
						lastPercent = currPercent;
					}
				}
				inFile.ReleaseAllTracks();
				#ifdef __VM_SYSTEM	//RAM
				free(convert_buffer);
				#endif
			}else{
				Pool.play_mode = NONE;
				Pool.pointer = 0;
				Pool.play_pointer = 0;
				Pool.l_pointer = 0;
				Pool.r_pointer = 0;
				Pool.r_sel_pointer = 0;
				Pool.size = 0;
				Pool.selection = NONE;
				Pool.sample_type = NONE;
				Pool.sample_bits = 16;
				Pool.frequency = 41400.0;

		         (new BAlert(NULL,Language.get("MEM_ERROR"),Language.get("OK")))->Go();

			}
			
			if (channels == 1)
				Pool.sample_type = MONO;
			else
				Pool.sample_type = STEREO;

			Pool.r_pointer = Pool.size;
			Pool.pointer = 0;
			Pool.r_sel_pointer = Pool.pointer;
			Pool.l_pointer = 0;

#ifndef __VM_SYSTEM	//RAM
			play_cookie.mem = Pool.sample_memory;
			play_cookie.start_mem = Pool.sample_memory;
			play_cookie.end_mem = Pool.sample_memory + Pool.size*Pool.sample_type;
			play_cookie.frequency = Pool.frequency;
			play_cookie.add = 0;
#else			
			play_cookie.mem = 0;
			play_cookie.start_mem = 0;
//			play_cookie.end_mem = Pool.size*Pool.sample_type;
			play_cookie.frequency = Pool.frequency;
			play_cookie.add = 0;
#endif
			Pool.changed = false;
			Pool.HideProgress();

			// create the PeakFile
			Pool.ResetIndexView();
			Hist.Reset();				// reset undo class

			if (IsLaunching() && Prefs.play_when_loaded)
				Pool.mainWindow->PostMessage(TRANSPORT_PLAYS);
			
		}else{
			(new BAlert(NULL,Language.get("LOADING_NO_AUDIO"),Language.get("OK")))->Go();
		}
	}
	
	Pool.sample_view_dirty = true;	// update the sample-view
	Pool.update_draw_cache = true;	// update the draw cache
	Pool.update_index = true;		// update the draw cache
	Pool.update_peak = true;
	Pool.RedrawWindow();
	Pool.InitBufferPlayer( Pool.frequency );

	play_cookie.pause = temp_pause;
	Pool.UpdateMenu();
	mainWindow->UpdateRecent();
//	be_app->Unlock();
}

//------------------------------------------------------------------ Save

void MyApplication::Save(BMessage *message){
	// Grab the stuff we know is there .. or should be :P

	entry_ref dir_ref, file_ref;
	const char *name;
	BFile newFile;
	BDirectory dir;
	float t;
	
	if ((message->FindRef("directory", &dir_ref) == B_OK)
		&& (message->FindString("name", &name) == B_OK))
	{
		dir.SetTo(&dir_ref);
		if (dir.InitCheck() != B_OK)
			return;
			
		dir.CreateFile(name, &newFile);
		
		BEntry entry(&dir, name);
		if (entry.InitCheck() != B_OK) {
			(new BAlert(NULL, Language.get("CANT_OVERWRITE_FILE"), Language.get("OK")))->Go();
			return;
		}
		entry.GetRef(&file_ref);

		media_codec_info *audioCodec;
		media_file_format *fileFormat;
		media_raw_audio_format *raf(NULL), *raf_in(NULL);
		media_format format;
		memset(&format, 0, sizeof(format));
		char *buffer(NULL);
		int32 frame_size(1);

		fSavePanel->GetSelectedFormatInfo(&fileFormat, &audioCodec);

		if (audioCodec != NULL){

//			format = Pool.m_format;
			memcpy(&format, &Pool.m_format, sizeof(format));
			raf_in = &(format.u.raw_audio);
			format.type = B_MEDIA_RAW_AUDIO;

			if (raf_in->format == 1)	raf_in->format = 0x11;
			
			// create media file
			BMediaFile file(&file_ref, fileFormat, B_MEDIA_FILE_REPLACE_MODE);
			if (file.InitCheck() != B_OK){
				(new BAlert(NULL, Language.get("CANT_OVERWRITE_FILE"), Language.get("OK")))->Go();
				return;
			}
			
			BMediaTrack *outTrack = file.CreateTrack(&format, audioCodec);

			if (outTrack){
				file.CommitHeader();

				if (save_start == 0){			// save as
					char s[B_FILE_NAME_LENGTH +20];
					sprintf(s, "BeAE - %s", file_ref.name);
					mainWindow->SetTitle(s);
				}

				raf = &(format.u.raw_audio);
				buffer = (char*)malloc(raf->buffer_size);
				int32 channels = raf->channel_count;
				frame_size = (raf->format & 0xf) * raf->channel_count;
				
				int32 buffer_step = raf->buffer_size / frame_size;
#ifndef __VM_SYSTEM	//RAM
				float *mem = Pool.sample_memory + save_start*Pool.sample_type;	// src memory
#else
				float *convert_buffer = (float*)malloc(buffer_step*channels*4);		// make sure there can be floats in it
				// read audio from source and write to destination, if necessary
				if (convert_buffer) {
					VM.ReadBlockAt(save_start, convert_buffer, buffer_step*channels );
					float *mem = convert_buffer;
#endif			

				Pool.StartProgress(Language.get("SAVING_FILE"), save_end-save_start);
				for (int64 i=save_start; i<save_end; i+=buffer_step){

				// fill up the buffer

					int32 block = MIN( (save_end-i) , buffer_step);
					switch(format.u.raw_audio.format){
					case 0x24:	// 0 == mid, -1.0 == bottom, 1.0 == top (the preferred format for non-game audio)
					{	float *tmp = (float*)buffer;
						for (int32 count = 0; count<block*channels; count++){
							*tmp++ = *mem++;
						}
					}	break;
					case 0x4:	// 0 == mid, 0x80000001 == bottom, 0x7fffffff == top (all >16-bit formats, left-adjusted)
					{	int32 *tmp = (int32*)buffer;
						for (int32 count = 0; count<block*channels; count++){
							t = *mem++;
							*tmp++ = ROUND(t*0x7fffffff);
						}
					}	break;
					case 0x2:	// 0 == mid, -32767 == bottom, +32767 == top
					{	int16 *tmp = (int16*)buffer;
						for (int32 count = 0; count<block*channels; count++){
							t = *mem++;
							*tmp++ = ROUND(t*32767.0);
						}
					}	break;
					case 0x11:	// 128 == mid, 1 == bottom, 255 == top (discouraged but supported format)
					{	uint8 *tmp = (uint8*)buffer;
						for (int32 count = 0; count<block*channels; count++){
							t = *mem++;
							*tmp = ROUND(t*127.0);
							tmp++;
							*tmp = *tmp ^ 0x80;
						}
					}	break;
					case 0x1:		// 0 == mid, -127 == bottom, +127 == top (not officially supported format)
					{	int8 *tmp = (int8*)buffer;
						for (int32 count = 0; count<block*channels; count++){
							t = *mem++;
							*tmp++ = ROUND(t*127.0);		// xor 128 to invert sign bit
						}
					}	break;
					}

					Pool.ProgressUpdate( block );
					outTrack->WriteFrames(buffer, block);
					#ifdef __VM_SYSTEM	//RAM
					VM.ReadBlock(convert_buffer, block*channels );
					mem = convert_buffer;
					#endif
				}

				#ifdef __VM_SYSTEM	//RAM
				free(convert_buffer);
				}
				#endif
				
				Pool.changed = false;

				outTrack->Flush();

				BMimeType result;
				BEntry ent(&dir,name);
				entry_ref fref;
				ent.GetRef(&fref);
				BMimeType::GuessMimeType(&fref,&result);
				BNodeInfo ninfo(&newFile); 
				ninfo.SetType(result.Type()); 

			}else{
				(new BAlert(NULL, Language.get("CODEC_FORMAT_ERROR"), Language.get("OK")))->Go();
			}

			file.CloseFile();
			
			free(buffer);
			Pool.HideProgress();
		}
	}else{
		(new BAlert(NULL, Language.get("SAVE_ERROR"), Language.get("OK")))->Go();
	}

	if (Pool.save_mode == 2)
		PostMessage(B_QUIT_REQUESTED);
	if (Pool.save_mode == 1)
		mainWindow->PostMessage(OPEN);

	Pool.save_mode = 0;
}
vector<Molecule> readInMolecules(string filename){
    vector<Molecule> molecules;
    ifstream inFile(filename.c_str());
    string line;
    
    if(inFile.is_open()){
        vector<Bond> bonds;
        vector<Angle> angles;
        vector<Atom> atoms;
        vector<Dihedral> dihedrals;
        vector<Hop> hops;

        //the third line starts the first molecule
        getline(inFile, line); // envrionment
        getline(inFile, line); //blank
        
        Molecule currentMolec;
        int section = 0; // 0 = id, 1 = atom, 2 = bond, 3 = dihedral, 4 = hop, 5 = angle
        while(inFile.good()){
            //printf("bonds: %d\nangles: %d\natoms: %d\ndihedrals: %d\n\n",
              //      bonds.size(), angles.size(), atoms.size(), dihedrals.size());
            getline(inFile, line);
            switch(section){
                case 0: // id
                    if(line.compare("=") == 0)
                        section++;
                    else{
                        currentMolec.id = atoi(line.c_str());
                    }
                    break;
                case 1: // atom
                    if(line.compare("=") == 0)
                        section++;
                    else{
                       atoms.push_back(getAtomFromLine(line)); 
                    }
                    break;
                case 2: // bond
                    if(line.compare("=") == 0)
                        section++;
                    else{
                       bonds.push_back(getBondFromLine(line)); 
                    }
                    break;
                case 3: // dihedral
                    if(line.compare("=") == 0)
                        section++;
                    else{
                       dihedrals.push_back(getDihedralFromLine(line)); 
                    }
                    break;
                case 4: // hop
                    if(line.compare("=") == 0)
                        section++;
                    else{
                        hops.push_back(getHopFromLine(line));
                    }
                case 5: // angle
                    if(line.compare("==") == 0){
                        section = 0;
                        
                        //convert all vectors to arrays
                        Bond *bondArray = (Bond *) malloc(sizeof(Bond) * bonds.size());
                        Angle *angleArray = (Angle *) malloc(sizeof(Angle) * angles.size());
                        Atom *atomArray = (Atom *) malloc(sizeof(Atom) * atoms.size());
                        Dihedral *dihedralArray = (Dihedral *) malloc(sizeof(Dihedral) * dihedrals.size());
                        Hop *hopArray = (Hop *) malloc(sizeof(Hop) * hops.size());

                        for(int i = 0; i < bonds.size(); i++)
                            bondArray[i] = bonds[i];
                        for(int i = 0; i < angles.size(); i++)
                            angleArray[i] = angles[i];
                        for(int i = 0; i < atoms.size(); i++)
                            atomArray[i] = atoms[i];
                        for(int i = 0; i < dihedrals.size(); i++)
                            dihedralArray[i] = dihedrals[i];
                        for(int i = 0; i < hops.size(); i++)
                            hopArray[i] = hops[i];
                       
                        //assign arrays to molecule
                        currentMolec.atoms = atomArray;
                        currentMolec.numOfAtoms = atoms.size();
                        
                        currentMolec.bonds = bondArray;
                        currentMolec.numOfBonds = bonds.size();
                        
                        currentMolec.angles = angleArray;
                        currentMolec.numOfAngles = angles.size();

                        currentMolec.dihedrals = dihedralArray;
                        currentMolec.numOfDihedrals = dihedrals.size();

                        currentMolec.hops = hopArray;
                        currentMolec.numOfHops = hops.size();
                        
                        //add molecule to array of returned molecules
                        molecules.push_back(currentMolec); 
                        
                        Molecule newMolec;
                        currentMolec = newMolec;

                        dihedrals.clear();
                        atoms.clear();
                        bonds.clear();
                        angles.clear();
                        hops.clear();

                    }
                    else{
                       angles.push_back(getAngleFromLine(line)); 
                    }
                    break;
            }
        }
    }
    else{
        return molecules;
    }
    
   return molecules;

}
vector<string>
HuffmanEncoder::readEncodingMapFromFile(string file_name)
{
    //ifstream inFile(file_name.c_str());
    ifstream inFile(file_name.c_str(), ifstream::binary);
    void *toRead;
    int length, len, size;
    uint64_t c;
    vector<string> huffmanMap(256);;

    if (!inFile.good()) {
        printf("Couldn't open map file for reading! \n");
        exit(1);
    }
    inFile.read((char*)(&len), (int)sizeof(uint32_t));

    switch(len) {
    case 8:
        size = 512*sizeof(uint8_t);
        break;
    case 16:
        size = 512*sizeof(uint16_t);
        break;
    case 32:
        size = 512*sizeof(uint32_t);
        break;
    case 64:
        size = 512*sizeof(uint64_t);
        break;
    default:
        printf("len is wrong: %d\n", len);
        exit(1);
    }
    toRead = malloc(size);

    inFile.read((char*)toRead, size);
    inFile.close();
    for (unsigned int i = 0; i < 512; i+=2) {
        switch(len) {
        case 8:
            length = (int) ((uint8_t*)toRead)[i];
            c = (uint64_t) ((uint8_t*)toRead)[i+1];
            break;
        case 16:
            length = (int) ((uint16_t*)toRead)[i];
            c = (uint64_t) ((uint16_t*)toRead)[i+1];
            break;
        case 32:
            length = (int) ((uint32_t*)toRead)[i];
            c = (uint64_t) ((uint32_t*)toRead)[i+1];
            break;
        case 64:
            length = (int) ((uint64_t*)toRead)[i];
            c = (uint64_t) ((uint64_t*)toRead)[i+1];
            break;
        }

        /* Decode bits into chars */
        string s = "";
        for (int j = 0; j < length; ++j) {
            if (c & 1)
                s = "1" + s;
            else
                s = "0" + s;
            c >>= 1;
        }
        /* Every char in alphabet gets 2 bytes, so the code we are reading
         * belongs to the char value of index/2 */
        huffmanMap[i/2] = s;
    }

    return huffmanMap;
}
Ejemplo n.º 24
0
void MainWindow::transform(const QString& curr_folder,const QString& tar_folder)
{
    QDir dir(curr_folder);
    if (!dir.exists())
        return;
    dir.setFilter(QDir::Dirs|QDir::Files);// 除了目录或文件,其他的过滤
    dir.setSorting(QDir::DirsFirst);// 优先显示目录
    QFileInfoList list = dir.entryInfoList();// 获取文件信息列表

    int i = 0;
    bool bIsDir;
    do{
       QFileInfo fileInfo = list.at(i);
       if(fileInfo.fileName()=="."||fileInfo.fileName()=="..")
       {
            i++;
           continue;
       }
       bIsDir = fileInfo.isDir();
       if (!bIsDir)
       {
           // copy 无关文件
           if(!selected_set.contains(fileInfo.suffix()))
           {
               if(ui->copyBox->isChecked())
                    QFile::copy(fileInfo.filePath(),tar_folder + "/" + fileInfo.fileName());
           }
           // 改为指定编码
           else
           {
               QFile inFile(fileInfo.filePath());
               QFile outFile(tar_folder + "/" + fileInfo.fileName());
               if (!inFile.open(QIODevice::ReadOnly)) {
                   qDebug() << "Cannot open file for writing: ";
                   qDebug() << fileInfo.filePath();
                   return;
               }
               if (!outFile.open(QIODevice::WriteOnly)) {
                   qDebug() << "Cannot open file for writing: ";
                   qDebug() << tar_folder + "/" + fileInfo.fileName();
                   return;
               }


               QTextStream in(&inFile);
               QTextStream out(&outFile);

               in.setCodec("GBK");
               out.setCodec("UTF-8");

               QString s = in.readAll();
               qDebug() << s;
               out << s;
               inFile.close();
               outFile.close();

           }
       }
       else
       {
           QString tar = tar_folder + "/" + fileInfo.fileName();
           QDir dir(tar);
           if(!dir.exists())
                if(!dir.mkdir(tar))
                    qDebug() << "mkdir " << tar << " failed";
           transform(fileInfo.filePath(),tar);
       }
       i++;
    }while(i<list.size());
    ui->pushButton_3->setEnabled(true);
}
Ejemplo n.º 25
0
//NOTE: The file index starts at 1, but we'll subtract one
//      to match vector index.
void readInFromFile( vector<Node> &g, string fileName )
{
    //clear g
    g = {};
    
    Node emptyNode;
    
    ifstream inFile( fileName, ios::in );
    if ( !inFile )
    {
        cerr << endl << "File could not be opened." << endl << endl;
        exit(1);
    }
    
    string line;
    
    while( getline(inFile, line) )
    {
        istringstream iss1(line);
        string tab;
        
        string sindex;
        long index;
        getline(iss1, sindex, '\t');
        istringstream issIndex(sindex);
        issIndex >> index;
        //Make index match array index
        --index;
        //cout << "Index: " << index << endl;
        
        //Add a node
        g.push_back(emptyNode);

        
        
        while( getline(iss1, tab, '\t') )
        {
            //extra crap (like space or newline) in last iteration
            if( tab.length() >= 3 )
            {
                istringstream iss2(tab);
                
                string comma;
                
                int counter = 0;
                long iVert = -1000;
                long edgeDist = -1000;
                while( getline(iss2, comma, ',') )
                {
                    istringstream iss3(comma);
                    
                    if( counter == 0 )
                    {
                        iss3 >> iVert;
                        //Make vertex match array index
                        --iVert;
                        counter++;
                    }
                    else if( counter == 1 )
                    {
                        iss3 >> edgeDist;
                        counter++;
                    }
                    else
Ejemplo n.º 26
0
void Builder::ImportDeclarations(void)
{
    QString inFileName = config.workspace + "/" + project->name + "/source/" + project->name + ".cpp"; //main.cpp

    if (QFileInfo(inFileName).exists() == false) {
        return;
    }

    QFile inFile(inFileName);
    inFile.open(QFile::ReadOnly | QFile::Text);
    QTextStream in(&inFile);
    QString code = in.readAll();

    // Remove all single-line comments from the text that will be processed
    QStringList lines = code.split("\n");

    int i=0;
    while (i < lines.count()) {
        int p = lines[i].indexOf("//");
        if (p >= 0) {
            lines[i] = lines[i].right(p);
        }
        i++;
    }
    code = lines.join("\n");

    // remove all multi-line comments from the text that will be processed
    int p1 = code.indexOf("/*");
    //int watchDogMonitor = code.length();
    while (p1 >= 0) {
        QString left = code.left(p1);
        code = code.right(code.length() - p1);
        int p2 = code.indexOf("*/");
        if (p2 >=0) {
            QString right = code.right(code.length() - p2 - 2);
            code = left + right;
        } else {
            return; // could not find the end of a comment: break to avoid infinite loop
        }
        p1 = code.indexOf("/*");

        /*if (code.length() == watchDogMonitor) {
        	return; // exit to avoid an infinite loop
        }
        watchDogMonitor = code.length();*/
    }

    lines = code.split("\n");
    QStringList list;

    i=1;
    while (i < lines.count()) {
        if (lines[i].trimmed().indexOf("{") == 0) {
            lines[i-1] = " " + lines[i];
            lines.erase(lines.begin()+i);
        } else {
            i++;
        }
    }

    for (int i=0; i < lines.count(); i++) {
        //QRegExp rx("(\\ |\\,|\\(|\\)|\\.|\\:|\\t)"); //RegEx for ' ' or ',' or '.' or ':' or '\t'
        QRegExp rx("\\s"); //RegEx for all white spaces, including tab'
        bool ok = rx.isValid();
        QString line = lines[i].trimmed();
        int j = 0;
        while (j < line.size()) { //insert spaces
            if ( ( (line.at(j) == '(') || (line.at(j) == ')') ) || ( (line.at(j) == '{') || (line.at(j) == '}') ) ) {
                line.insert(j, " ");
                j++;
            }
            if ( ( (line.at(j) == ':') || (line.at(j) == ',') ) || ( (line.at(j) == ';') || (line.at(j) == ';') ) ) {
                line.insert(j, " ");
                j++;
            }
            j++;
        }
        QStringList words = line.split(rx);
        int w = 0;
        while (w < words.count()) {
            words[w] = words[w].trimmed().toUpper();
            if (words[w] == "") {
                words.erase(words.begin() + w);
            } else {
                w++;
            }
        }

        if (words.count() < 4) { // at least "TYPE", "FUNCTION_NAME", "(", ")"
            continue;
        }

        bool ignore = false;
        ignore = ignore || (words.indexOf("IF") >= 0);
        ignore = ignore || (words.indexOf("ELSE") >= 0);
        ignore = ignore || (words.indexOf("WHILE") >= 0);
        ignore = ignore || (words.indexOf("DO") >= 0);
        ignore = ignore || (words.indexOf("SWITCH") >= 0);
        ignore = ignore || (lines[i].indexOf(";") >= 0);
        ignore = ignore || (lines[i].indexOf(".") >= 0);
        ignore = ignore || (lines[i].indexOf("->") >= 0);
        ignore = ignore || (lines[i].indexOf("=") >= 0);
        ignore = ignore || (lines[i].indexOf("==") >= 0);
        ignore = ignore || (lines[i].indexOf("\"") >= 0); // any argument with " may be considered a call, and not a definition
        ignore = ignore || (lines[i].indexOf("'") >= 0); // idem

        // check if any argument is a number. If yes, then it shall be considered a function call, and not a definiton
        for (int j=0; j < words.count(); j++) {
            double num = words.at(j).toDouble();
            if (QString::number(num) == words.at(j)) {
                ignore = true;
                break;
            }
        }

        // if the first word is not an recognized data type, ignore
        bool validDataType = false;
        validDataType = validDataType || (words.at(0) == "VOID");
        validDataType = validDataType || (words.at(0) == "INT");
        validDataType = validDataType || (words.at(0) == "CHAR");
        validDataType = validDataType || (words.at(0) == "SHORT");
        validDataType = validDataType || (words.at(0) == "UNSIGNED");
        validDataType = validDataType || (words.at(0) == "SIGNED");
        validDataType = validDataType || (words.at(0) == "LONG");
        validDataType = validDataType || (words.at(0) == "FLOAT");
        validDataType = validDataType || (words.at(0) == "DOUBLE");
        validDataType = validDataType || (words.at(0) == "BYTE");
        validDataType = validDataType || (words.at(0) == "INT16");
        validDataType = validDataType || (words.at(0) == "INT32");
        validDataType = validDataType || (words.at(0) == "INT64");
        validDataType = validDataType || (words.at(0) == "BOOL");
        ignore = ignore || (validDataType == false);

        if (lines[i].indexOf("//") >= 0) {
            lines[i] = lines[i].left(lines[i].indexOf("//"));
        }

        if (ignore) {
            continue;
        }

        int p1 = lines[i].indexOf("(");
        int p2 = lines[i].indexOf(")");
        if ((p1 > 0) && (p2 > p1)) {
            QString def = lines[i].trimmed();
            int p = def.lastIndexOf(")");
            if (p >= 0) {
                def = def.left(p+1) + ";";
                list.append(def);
            }
        }
    }

    QString outFileName = config.workspace + "/" + project->name + "/source/mariamole_auto_generated.h";

    if (QFileInfo(outFileName).exists() == false) {
        return;
    }

    QFile inFile2(outFileName);
    inFile2.open(QFile::ReadOnly | QFile::Text);
    QTextStream in2(&inFile2);
    code = in2.readAll();

    QStringList header = code.split("\n");
    //QStringList alreadyAdded;
    int index = -1;// = header.count() - 2;
    bool foundBegin = false;
    int k=0;
    while (k < header.count()) {
        QString line = header[k].trimmed();
        if (line == "/*--MARIMOLE-DEF_END--*/") {
            foundBegin = false;
        }
        if (foundBegin) {
            header.erase(header.begin() + k);
        } else {
            ++k;
        }
        if (line == "/*--MARIMOLE-DEF_BEGIN--*/") {
            foundBegin = true;
            index = k;
        }
    }

    if (index < 0) {
        return;
    }

    for (int i=0; i < list.count(); i++) {
        header.insert(index, list[i]);
    }

    QFile outFile(outFileName);
    outFile.open(QFile::WriteOnly);
    QTextStream out(&outFile);
    out << header.join("\n");
}
Ejemplo n.º 27
0
int getEventList()
{
  TFile inFile("Wjets_100to200.root", "READ");
  TTree* inTree = static_cast<TTree*>(inFile.Get("bdttree"));

  UInt_t Run;
  ULong64_t Event;
  UInt_t LumiSec;
  inTree->SetBranchAddress("Run", &Run);
  inTree->SetBranchAddress("Event", &Event);
  inTree->SetBranchAddress("LumiSec", &LumiSec);

  Float_t nGoodEl;   inTree->SetBranchAddress("nGoodEl",   &nGoodEl);
  Float_t nGoodMu;   inTree->SetBranchAddress("nGoodMu",   &nGoodMu);
  Float_t LepID;     inTree->SetBranchAddress("LepID",     &LepID);
  Float_t LepChg;    inTree->SetBranchAddress("LepChg",    &LepChg);
  Float_t LepPt;     inTree->SetBranchAddress("LepPt",     &LepPt);
  Float_t LepEta;    inTree->SetBranchAddress("LepEta",    &LepEta);
  Float_t LepDxy;    inTree->SetBranchAddress("LepDxy",    &LepDxy);
  Float_t LepDz;     inTree->SetBranchAddress("LepDz",     &LepDz);
  Float_t LepIso03;  inTree->SetBranchAddress("LepIso03",  &LepIso03);
  Float_t Lep2ID;    inTree->SetBranchAddress("Lep2ID",    &Lep2ID);
  Float_t Lep2Chg;   inTree->SetBranchAddress("Lep2Chg",   &Lep2Chg);
  Float_t Lep2Pt;    inTree->SetBranchAddress("Lep2Pt",    &Lep2Pt);
  Float_t Lep2Eta;   inTree->SetBranchAddress("Lep2Eta",   &Lep2Eta);
  Float_t Lep2Dxy;   inTree->SetBranchAddress("Lep2Dxy",   &Lep2Dxy);
  Float_t Lep2Dz;    inTree->SetBranchAddress("Lep2Dz",    &Lep2Dz);
  Float_t Lep2Iso03; inTree->SetBranchAddress("Lep2Iso03", &Lep2Iso03);
  Float_t Met; inTree->SetBranchAddress("Met",&Met);
  Float_t mt; inTree->SetBranchAddress("mt",&mt);
  Float_t HT;  inTree->SetBranchAddress("HT",&HT);
  Float_t Njet;  inTree->SetBranchAddress("Njet",&Njet);
  Float_t Jet1Pt;  inTree->SetBranchAddress("Jet1Pt",&Jet1Pt);
  Float_t Jet2Pt;  inTree->SetBranchAddress("Jet2Pt",&Jet2Pt);
  Float_t Jet3Pt;  inTree->SetBranchAddress("Jet3Pt",&Jet3Pt);
  Float_t DPhiJet1Jet2;  inTree->SetBranchAddress("DPhiJet1Jet2",&DPhiJet1Jet2);


  ofstream outFile ("eventlist.txt", std::ios::binary);

  Long64_t nentries = inTree->GetEntries();
  for(Long64_t i = 0; i < nentries; ++i)
  {
    if(i >= 100)
      break;
    inTree->GetEntry(i);

    outFile << "Run: " << Run << "; Event: " << Event << "; Lumisection: " << LumiSec << std::endl;
    outFile << "nGoodEl: " << nGoodEl << "; nGoodMu: " << nGoodMu << std::endl;
    outFile << "LepID: " << LepID;
    outFile << "; LepChg: " << LepChg;
    outFile << "; LepPt: " << LepPt;
    outFile << "; LepEta: " << LepEta;
    outFile << "; LepDxy: " << LepDxy;
    outFile << "; LepDz: " << LepDz;
    outFile << "; LepIso03: " << LepIso03 << std::endl;
    outFile << "Lep2ID: " << Lep2ID;
    outFile << "; Lep2Chg: " << Lep2Chg;
    outFile << "; Lep2Pt: " << Lep2Pt;
    outFile << "; Lep2Eta: " << Lep2Eta;
    outFile << "; Lep2Dxy: " << Lep2Dxy;
    outFile << "; Lep2Dz: " << Lep2Dz;
    outFile << "; Lep2Iso03: " << Lep2Iso03 << std::endl;
    outFile << "Met: " << Met << std::endl;
    outFile << "mt: " << mt << std::endl;
    outFile << "HT: " << HT << std::endl;
    outFile << "Njet: " << Njet << std::endl;
    outFile << "Jet1Pt: " << Jet1Pt << std::endl;
    outFile << "Jet2Pt: " << Jet2Pt << std::endl;
    outFile << "Jet3Pt: " << Jet3Pt << std::endl;
    outFile << "DPhiJet1Jet2: " << DPhiJet1Jet2 << std::endl;
    outFile << std::endl;
  }

  return 0;
}
Ejemplo n.º 28
0
int main(int argc, char *argv[])
{
	if (argc != 3)
	{
		std::cout << std::endl
				<< "Filter peptides is used to filter pep.xml files from blacklisted peptides after the search has been performed. For example shared peptides between two species or known containments."
				<< std::endl;
		std::cerr << "Usage: filterPeptides [file.pep.xml] [blacklist]" << std::endl;
		exit(1);
	}
	//Load the blacklist in our hashmap for fast acces
	std::unordered_set<std::string> blacklistSet(loadBlacklist(argv[2]));
	std::cout << "Blacklist (" << argv[2] << ") contains " << blacklistSet.size() << " peptides" << std::endl;

	std::string inFile(argv[1]);
	std::string extension(".pep.xml");
	std::string outFile(inFile.substr(0, inFile.length() - extension.length()));
	outFile.append(".filtered.pep.xml");
	std::ofstream outputStream(outFile, std::ofstream::out | std::ofstream::trunc);

	pugi::xml_document doc;
	pugi::xml_parse_result result = doc.load_file(argv[1]);

	if (result)
	{
		pugi::xml_object_range<pugi::xml_named_node_iterator> spectrumQueries =
				doc.child("msms_pipeline_analysis").child("msms_run_summary").children("spectrum_query");
		std::cout << "pugixml: [" << argv[1] << "] parsed without errors, total spectrum queries: "
				<< std::distance(spectrumQueries.begin(), spectrumQueries.end()) << "\n";
	}
	else
	{
		std::cout << "pugixml: [" << argv[1] << "] parsed with errors\n";
		std::cout << "Error description: " << result.description() << "\n";
		std::cout << "Error offset: " << result.offset;
		exit(1);
	}

	unsigned totalDeleted = 0;
	unsigned totalPeptides = 0;
	//Containers to store our nodes we need to delete afterwards
	std::vector<pugi::xml_node> spectrumToDelete;
	std::vector<pugi::xml_node> searchhitToDelete;
	//Initialize the searchhitNodes counter
	unsigned searchhitNodes = 0;

	for (pugi::xml_node spectrum : doc.child("msms_pipeline_analysis").child("msms_run_summary").children(
			"spectrum_query"))
	{
		//Temp variable for easier acces
		pugi::xml_object_range<pugi::xml_named_node_iterator> search_resultNodes =
				spectrum.child("search_result").children("search_hit");

		for (pugi::xml_node search_hit : search_resultNodes)
		{
			if (blacklistSet.find(search_hit.attribute("peptide").as_string()) != blacklistSet.end())
			{
				//notify the user of our node deletion
				std::cout << "Deleting from spectrum '" << spectrum.attribute("spectrum").as_string();
				std::cout << "' peptide hit '" << search_hit.attribute("peptide").as_string();
				std::cout << "' (rank:" << search_hit.attribute("hit_rank").as_uint() << ")" << "\n";
				searchhitToDelete.push_back(search_hit);

			}
			totalPeptides++;
		}

		if (searchhitToDelete.size() == 0)
		{
			//Short circuit and skip expensive deletion of nodes
			continue;
		}

		//Count the total amount of nodes
		searchhitNodes = std::distance(search_resultNodes.begin(), search_resultNodes.end());
		totalDeleted+=searchhitToDelete.size();

		if ((searchhitNodes - searchhitToDelete.size()) == 0)
		{
			//if the results are empty, delete the whole spectrum with it's query entries
			spectrumToDelete.push_back(spectrum);
		}
		else
		{
			//Remove the required nodes
			for (pugi::xml_node &search_hit : searchhitToDelete)
			{
				spectrum.child("search_result").remove_child(search_hit);
			}
			//TODO re-score the hit ranks of the other nodes
		}
		searchhitToDelete.clear();
	}

	//Clean up empty spectrum nodes
	for (pugi::xml_node &spectrum : spectrumToDelete)
	{
		doc.child("msms_pipeline_analysis").child("msms_run_summary").remove_child(spectrum);
	}

	//Pritn final results to the user
	std::cout << "Total peptides deleted: " << totalDeleted << " of " << totalPeptides << " peptide hits and removed "
			<< spectrumToDelete.size() << " empty spectrum_query results." << std::endl;
	std::cout << "Saving result: " << (doc.save_file(outFile.c_str(), "") ? outFile.c_str() : "failed") << std::endl
			<< std::endl;
}
Ejemplo n.º 29
0
bool LanguageModelInternal::Load(const std::string &filePath
																, FactorType factorType
																, float weight
																, size_t nGramOrder)
{
	assert(nGramOrder <= 3);
	if (nGramOrder > 3)
	{
		UserMessage::Add("Can only do up to trigram. Aborting");
		abort();
	}

	VERBOSE(1, "Loading Internal LM: " << filePath << endl);
	
	FactorCollection &factorCollection = FactorCollection::Instance();

	m_filePath		= filePath;
	m_factorType	= factorType;
	m_weight			= weight;
	m_nGramOrder	= nGramOrder;

	// make sure start & end tags in factor collection
	m_sentenceStart	= factorCollection.AddFactor(Output, m_factorType, BOS_);
	m_sentenceStartArray[m_factorType] = m_sentenceStart;

	m_sentenceEnd		= factorCollection.AddFactor(Output, m_factorType, EOS_);
	m_sentenceEndArray[m_factorType] = m_sentenceEnd;

	// read in file
	VERBOSE(1, filePath << endl);

	InputFileStream 	inFile(filePath);

	// to create lookup vector later on
	size_t maxFactorId = 0; 
	map<size_t, const NGramNode*> lmIdMap;

	string line;
	int lineNo = 0;
	
	while( !getline(inFile, line, '\n').eof())
	{
		lineNo++;

		if (line.size() != 0 && line.substr(0,1) != "\\")
		{
			vector<string> tokens = Tokenize(line, "\t");
			if (tokens.size() >= 2)
			{
				// split unigram/bigram trigrams
				vector<string> factorStr = Tokenize(tokens[1], " ");

				// create / traverse down tree
				NGramCollection *ngramColl = &m_map;
				NGramNode *nGram;
				const Factor *factor;
				for (int currFactor = (int) factorStr.size() - 1 ; currFactor >= 0  ; currFactor--)
				{
					factor = factorCollection.AddFactor(Output, m_factorType, factorStr[currFactor]);
					nGram = ngramColl->GetOrCreateNGram(factor);
	
					ngramColl = nGram->GetNGramColl();

				}

				NGramNode *rootNGram = m_map.GetNGram(factor);
				nGram->SetRootNGram(rootNGram);

				// create vector of factors used in this LM
				size_t factorId = factor->GetId();
				maxFactorId = (factorId > maxFactorId) ? factorId : maxFactorId;
				lmIdMap[factorId] = rootNGram;
				//factorCollection.SetFactorLmId(factor, rootNGram);

				float score = TransformLMScore(Scan<float>(tokens[0]));
				nGram->SetScore( score );
				if (tokens.size() == 3)
				{
					float logBackOff = TransformLMScore(Scan<float>(tokens[2]));
					nGram->SetLogBackOff( logBackOff );
				}
				else
				{
					nGram->SetLogBackOff( 0 );
				}
			}
		}
	}

		// add to lookup vector in object
	m_lmIdLookup.resize(maxFactorId+1);
	fill(m_lmIdLookup.begin(), m_lmIdLookup.end(), static_cast<const NGramNode*>(NULL));

	map<size_t, const NGramNode*>::iterator iterMap;
	for (iterMap = lmIdMap.begin() ; iterMap != lmIdMap.end() ; ++iterMap)
	{
		m_lmIdLookup[iterMap->first] = iterMap->second;
	}

	return true;
}
Ejemplo n.º 30
0
void InputStructures::read_fasta(string chr_file, map<string, string> & chr_seq) {
  //chr_seq.clear();
  map<string,string>::iterator it;

  string temp_chr_seq = "";
  string line = "";
  ifstream inFile(chr_file.c_str());
  string chr_num = "";
  int chrNumber = 0;
  string chrName;
  bool firstContigFound = false;

  if (!inFile.is_open()) {
    cout << "Error opening file: " << chr_file << endl;
    return;
  }
  getline(inFile,line);

  it = chr_seq.begin();

  // Read and process records
  while (!inFile.eof() && line.length() > 0) {
    if (line.at(0) == '#' && !firstContigFound) {
      getline(inFile,line);
    } else
      if (line.at(0) == '>' || line.at(0) == '<') {
        firstContigFound = true;

        if (chrNumber > 0) {
          //chr_seq.push_back(temp_chr_seq);
          chr_seq.insert(it, pair<string,string>(chrName,temp_chr_seq));
          if (DEBUG)
            cout << "Chromosome Name = " << chrName << endl;

          if (temp_chr_seq.length() > 0) {
            if (DEBUG)
              cout << temp_chr_seq.length() << endl;
            temp_chr_seq.clear();
          }

          //chrNumber++;
        }

        chrNumber++;
        //find if there are more than contig name in the line
        int firstspace = line.find(" ");
        chrName = line.substr(1, firstspace-1);
        if (chrName.length() == 0) {
          cerr << " Reference csfasta file provided has no contig name : " << line << endl;
          exit(-1);
        }
        if (DEBUG)
          cout << " Chr Name found in Reference csfasta : " << chrName << endl;
        refSequences.push_back(chrName);

        getline(inFile, line);

      } else {
        temp_chr_seq.append(line);
        getline(inFile,line);
      }

  }

  if (temp_chr_seq.length() > 0) {
    //cout << temp_chr_seq.length() << endl;cout << "Dima's seq : " << temp_chr_seq.substr(279750,100) << endl;
    chr_seq.insert(it, pair<string,string>(chrName,temp_chr_seq));
  }

  inFile.close();
}