Example #1
0
bool
TimeLine::load(mrs_string filename, mrs_string lexicon_labels)
{

  ifstream in;
  filename_ = filename;

  if(filename == "")
    return false;

  in.open(filename.c_str());
  if(!in.is_open())
  {
    MRSWARN("TimeLine::load() -  Problem opening file " << filename_);
    return false;
  }

  FileName f(filename);
  vector<mrs_string> labels;



  // Load lexicon dictionary if available
  mrs_string lexicon_label;
  mrs_string remainder;
  size_t nLabels;

  nLabels = std::count(lexicon_labels.begin(), lexicon_labels.end(), ',');

  if (lexicon_labels != ",")
  {
    for (size_t i=0; i < nLabels; i++)
    {
      lexicon_label = lexicon_labels.substr(0, lexicon_labels.find(","));
      labels.push_back(lexicon_label);
      sort(labels.begin(), labels.end());
      remainder = lexicon_labels.substr(lexicon_labels.find(",") + 1, lexicon_labels.length());
      lexicon_labels = remainder;
    }
  }
  else
    nLabels = 0;


  if (f.ext() == "txt") // audacity label format
  {
    numRegions_ = 0;
    mrs_real start, end;
    mrs_string label;
    regions_.clear();
    while (!in.eof())
    {
      in >> start >> end >> label;


      TimeRegion region;
      region.start = (mrs_natural) (start * srate_);
      region.end = (mrs_natural) (end * srate_);
      region.classId = 1;
      region.name = label;
      mrs_bool label_found = false;

      for (unsigned int i=0; i < labels.size(); i++)
      {
        if (label == labels[i])
        {
          label_found = true;
          region.classId = i;
        }

      }
      if (!label_found)
      {

        if (lexicon_labels == ",")
        {
          labels.push_back(label);
          sort(labels.begin(), labels.end());
        }
      }
      regions_.push_back(region);
      numRegions_ ++;
    }




    // relabel classIds so that they correspond to sorted labels
    for (mrs_natural i=0; i < numRegions_; ++i)
    {
      mrs_string label = regions_[i].name;
      vector<mrs_string>::iterator it = find(labels.begin(), labels.end(), label);
      if (it == labels.end())
        regions_[i].classId = (mrs_natural)-1;
      mrs_natural l = distance(labels.begin(), it);
      regions_[i].classId = l;
    }


    // last region is a duplicate due to empty last line
    // kind of a hack but works
    numRegions_ --;
    regions_.pop_back();


    lineSize_ = 1;
    size_ = (mrs_natural) (end * srate_);

    in.close();
    return true;
  }