Пример #1
0
float language_model::perplexity_per_word(const sentence& tokens) const
{
    if (tokens.size() == 0)
        throw language_model_exception{
            "perplexity_per_word() called on empty sentence"};
    return perplexity(tokens) / tokens.size();
}
/* This function1 searches for a string in a sentence and return true/false*/
bool search_string(string str, sentence sen, int& j) {
  j=0;
  while (j!=sen.size()) {
    if (!sen[j].compare(str)) {
      break;
    }
    j++;
  }  
  if (j==sen.size()) return false; else return true; 
}
void do_add(vector<fd>& x, sentence line) {
  fd temp;
  int length=line.size();
  temp.property=line[length-1];
  temp.filename=line[length-2];
  vector <int> index;
  int minlength=10000000;

  for(int i=0;i!=length-3;i++)
    index.push_back(-1);
  for(int j=1;j!=length-2;j++)
    for(int i=0;i!=x.size();i++) 
      if ((!x[i].property.compare(line[j])) && (!x[i].filename.compare(line[length-2])))
	index[j-1]=i;

  for(int i=0;i!=length-3;i++) {
    if (index[i]>-1)
      if (minlength>x[index[i]].value.size())
	minlength=x[index[i]].value.size();
  }

  for (int j=0;j!=minlength;j++) {
    float value=0;
    for (int i=0;i!=length-2;i++)
      if (index[i]>0)
	value+=x[index[i]].value[j];
    temp.value.push_back(value);
  }
  x.push_back(temp);
}
vector<fd> convert_to_float(sentence s, map<string, vector<int> > x, sentence sf) {
  vector<fd> datatemp;
  int style,set;
  
  if (!s[0].compare("s")) style=1; else style=2;
  for(int i=1;i!=s.size();i++) {
    fd temp;
    switch (style) {
    case 1: {  // This is series case
      set=0;
      for(int j=0;j!=f.size(); j++) {
	if (x[s[i]][j]) {
	  if (!set) {
	    temp.filename="Allfiles";
	    temp.property=s[i];
	    set=1;
	  }
	  for(int tt=1;tt!=f[j].size();tt++) {
	    float t=atof(&f[j][tt][x[s[i]][j]][0]);
	    temp.value.push_back(t);
	  }
	}
	//	datatemp.push_back(temp);
      }
      datatemp.push_back(temp);
      break;
    }
    case 2: { // This is parallel case.
      for(int j=0;j!=f.size(); j++) {
	if (x[s[i]][j]) {
	  temp.filename=sf[j];
	  temp.property=s[i];
	  temp.value.clear();
	  for(int tt=1;tt!=f[j].size();tt++) {
	   float t= 0.0;
            if ((f[j][tt][x[s[i]][j]].compare("nan")))
	      t = atof(&f[j][tt][x[s[i]][j]][0]);
	    temp.value.push_back(t);
	  }
	  datatemp.push_back(temp);
	}
	else if (!s[i].compare("Step")) {
	  temp.filename=sf[j];
	  temp.property=s[i];
	  temp.value.clear();
	  for(int tt=1;tt!=f[j].size();tt++) {
	    float t=atof(&f[j][tt][x[s[i]][j]][0]);
	    temp.value.push_back(t);
	  }
	  datatemp.push_back(temp);
	}
      }
      break;
    }
    }
  }
  return datatemp;
}
/*This function replaces certain input keywords to their appropriate words found in lammps file*/
void transform_userinput(sentence& temp) {
  if (temp.size()) {
    for(int i=1;i!=temp.size();i++) {
      if (!temp[i].compare("t")) temp[i].replace(0,temp[i].length(),"Temp");
      if (!temp[i].compare("p")) temp[i].replace(0,temp[i].length(),"Press");
      if (!temp[i].compare("v")) temp[i].replace(0,temp[i].length(),"Volume");
      if (!temp[i].compare("vd")) temp[i].replace(0,temp[i].length(),"E_vdwl");
      if (!temp[i].compare("te")) temp[i].replace(0,temp[i].length(),"TotEng");
      if (!temp[i].compare("ke")) temp[i].replace(0,temp[i].length(),"KinEng");
      if (!temp[i].compare("pe")) temp[i].replace(0,temp[i].length(),"PotEng");
      if (!temp[i].compare("be")) temp[i].replace(0,temp[i].length(),"E_bond");
      if (!temp[i].compare("ae")) temp[i].replace(0,temp[i].length(),"E_angle");
      if (!temp[i].compare("de")) temp[i].replace(0,temp[i].length(),"E_dihed");
      if (!temp[i].compare("ce")) temp[i].replace(0,temp[i].length(),"E_coul");
      if (!temp[i].compare("le")) temp[i].replace(0,temp[i].length(),"E_long");
    }
  }
}
/*This function prints out the parameter associated index in successfully read file */
void print_file_index_output(sentence s, map<string, vector<int> > x, sentence sf) {
  size_t maxlen=0;
  for(int i=0;i!=sf.size();i++)
    maxlen=max(maxlen,sf[i].length());
  cout << print_space(maxlen+11);
  for(sentence::iterator aa=++s.begin(); aa!=s.end(); aa++) 
    cout << *aa << "\t";
  cout << endl;

  for(int j=0;j!=f.size();j++) {
    cout << "File-> " << sf[j] << print_space(maxlen-sf[j].length()+1) <<":  ";
    for(sentence::iterator aa=++s.begin(); aa!=s.end(); aa++) {
      cout << x[*aa][j] << "\t";
    }
    cout << endl;
  }
}
Пример #7
0
float language_model::perplexity(const sentence& tokens) const
{
    if (tokens.size() == 0)
        throw language_model_exception{"perplexity() called on empty sentence"};
    return std::pow(10.0f, -(log_prob(tokens) / tokens.size()));
}
void plot_xmgrace(sentence s, vector <fd> data) {
  ofstream out("temp");
  // Some string definitions
  string s1 = "@ s";
  string s2 = "   comment ";
  string s3 = "   legend  ";
  int plotiterator=0;  int set;  int style;
  int half_data=data.size()/2;

  // Printing out X and Y axis properties
  out << "@    xaxis  label char size 1.500000\n";
  out << "@    xaxis  ticklabel char size 1.250000\n";
  if (s[1].compare("cross")) 
    out << "@    xaxis label " << "\"time\""<< endl;
  else
    out << "@    xaxis label " << "\"" << s[2] << "\""<< endl;
  out << "@    yaxis  label char size 1.500000\n";
  out << "@    yaxis  ticklabel char size 1.250000\n";

  // Printing out legend properties
  out << "@    legend box linestyle 0\n";
  out << "@    legend 0.25, 0.8\n"; 

  // Selecting style for series or parallel plotting.
  if (!s[0].compare("s")) style=1; else style=2;

  if (s[1].compare("cross")) {
    if (s.size()==2) {
      out << "@ yaxis label " << "\"";
      for(int i=1;i!=s.size();i++) 
	out << s[i] << " ";
      out << "\""<< endl;
    }
  }
  else
    out << "@    yaxis label " << "\"" << s[3] << "\""<< endl; 
  int length=data.size();
  if (!s[1].compare("cross")) length/=2;
  // Storing data in file "temp" as asked (series or parallel)
  for(int i=0;i!=length;i++) {
    switch (style) {
    case 1: { // Series Case
      if (s[1].compare("cross")) { // This is executed if not cross
	out << s1 << plotiterator << s2 << "\""<< data[i].property << "\"" << endl;
	out << s1 << plotiterator << s3 << "\""<< data[i].property << "\"" << endl;
      }
      else
	{
	  out << s1 << plotiterator << s2 << "\""<< data[i+half_data].property << " vs " << data[i].property << "\"" << endl;
	  out << s1 << plotiterator << s3 << "\""<< data[i+half_data].property << " vs " << data[i].property << "\"" << endl;
	}
      out << s1 << plotiterator << " symbol 1" << endl;
      out << s1 << plotiterator << " symbol size 0.50000" << endl;
      out << s1 << plotiterator << " symbol color 1" << endl;
      out << s1 << plotiterator << " symbol pattern 1" << endl;
      out << s1 << plotiterator << " symbol fill color "<< plotiterator+1 << endl;
      out << s1 << plotiterator << " symbol fill pattern 1" << endl;
      out << s1 << plotiterator << " symbol linewidth 1.0" << endl;
      out << s1 << plotiterator << " symbol linestyle 1" << endl;
      out << s1 << plotiterator << " symbol char 65" << endl;
      out << s1 << plotiterator << " symbol char font 0" << endl;
      plotiterator++;
      for(int j=0;j!=data[i].value.size(); j++)  
	if (s[1].compare("cross"))  
	  out << data[i].value[j] << endl; // This is executed if not cross
	else
	  out << data[i].value[j] << " " << data[i+half_data].value[j]<< endl; // This is executed if cross
      out << "&" << endl;
      break;
    }
      
    case 2: { // Parallel Case 
      if (s[1].compare("cross")) {
	out << s1 << plotiterator << s2 << "\""<< data[i].property << ": " << data[i].filename << "\"" << endl;
	if (s.size()<=2)
	  out << s1 << plotiterator << s3 << "\"" << data[i].filename << "\"" << endl;
	else
	  out << s1 << plotiterator << s3 << "\""<< data[i].property << ": " << data[i].filename << "\"" << endl;
      }
      else {
	out << s1 << plotiterator << s2 << "\""<< data[i+half_data].property << " vs " << data[i].property << ": " << data[i].filename << "\"" << endl;
	out << s1 << plotiterator << s3 << "\""<< data[i+half_data].property << " vs " << data[i].property << ": " << data[i].filename << "\"" << endl;
      }
      out << s1 << plotiterator << " symbol 0" << endl;
      out << s1 << plotiterator  << " symbol size 0.50000" << endl;
      out << s1 << plotiterator  << " symbol color 1" << endl;
      out << s1 << plotiterator  << " symbol pattern 1" << endl;
      out << s1 << plotiterator  << " symbol fill color "<< plotiterator+1 << endl;
      out << s1 <<  plotiterator  << " symbol fill pattern 1" << endl;
      out << s1 <<  plotiterator  << " symbol linewidth 1.0" << endl;
      out << s1 <<  plotiterator  << " symbol linestyle 1" << endl;
      out << s1 <<  plotiterator  << " symbol char 65" << endl;
      out << s1 <<  plotiterator  << " symbol char font 0" << endl;
      plotiterator++;
      for(int j=0;j!=data[i].value.size(); j++)  
	if (s[1].compare("cross"))  {
	  out << data[i].value[j] << endl; // This is executed if not cross
        }
	else
	  out << data[i].value[j] << " " << data[i+half_data].value[j]<< endl; // This is executed if cross
      out << "&" << endl;
      break;
    }
    }
  }
  int rv=system("xmgrace temp &");
}