<commit_before>#include "dynet/io.h"
#include "dynet/tensor.h"
#include "dynet/except.h"

using namespace std;
using namespace dynet;

bool valid_key(const std::string & s) {
  if (s == "/") return false;
  auto it = std::find_if(s.begin(), s.end(),
                         [] (char ch) { return ch == ' ' || ch == '#';});
  return it == s.end();
}

TextFileSaver::TextFileSaver(const string & filename, bool append) :
        datastream(filename, append ? ofstream::app : ofstream::out) {
  if(!datastream)
    DYNET_RUNTIME_ERR("Could not write model to " << filename);
}

void TextFileSaver::save(const ParameterCollection & model,
                         const string & key) {
  if (!valid_key(key)) 
    DYNET_INVALID_ARG("Key could not include ' ' or '#': " << key);
  const ParameterCollectionStorage & storage = model.get_storage();
  if(key.size() == 0) {
    for (auto & p : storage.params) save(*p, key);
    for (auto & p : storage.lookup_params) save(*p, key);
  } else {
    size_t strip_size = model.get_fullname().size();
    for (auto & p : storage.params) 
      save(*p, key + p->name.substr(strip_size));
    for (auto & p : storage.lookup_params) 
      save(*p, key + p->name.substr(strip_size));
  }
}<commit_msg>add key check for ParameterCollection<commit_after>#include "dynet/io.h"
#include "dynet/tensor.h"
#include "dynet/except.h"
#include "dynet/str-util.h"

using namespace std;
using namespace dynet;

bool valid_key(const std::string & s) {
  if (s.size() == 0) return true;
  if (s == "/") return false;
  auto it = std::find_if(s.begin(), s.end(),
                         [] (char ch) { return ch == ' ' || ch == '#';});
  return it == s.end();
}

bool valid_pc_key(const std::string & s) {
  if (s.size() == 0) return true;
  if (!(startswith(s, "/") && endswith(s, "/"))) return false;
  return valid_key(s);
}

TextFileSaver::TextFileSaver(const string & filename, bool append) :
        datastream(filename, append ? ofstream::app : ofstream::out) {
  if(!datastream)
    DYNET_RUNTIME_ERR("Could not write model to " << filename);
}

void TextFileSaver::save(const ParameterCollection & model,
                         const string & key) {
  if (!valid_pc_key(key))
    DYNET_INVALID_ARG("Key should start with, end with '/' and could not include ' ' or '#': " << key);
  const ParameterCollectionStorage & storage = model.get_storage();
  if(key.size() == 0) {
    for (auto & p : storage.params) save(*p, key);
    for (auto & p : storage.lookup_params) save(*p, key);
  } else {
    size_t strip_size = model.get_fullname().size();
    for (auto & p : storage.params) 
      save(*p, key + p->name.substr(strip_size));
    for (auto & p : storage.lookup_params) 
      save(*p, key + p->name.substr(strip_size));
  }
}
{'fraction_non_alphanumeric': 0.16847433116413593, 'fraction_numerical': 0.0014461315979754157, 'mean_word_length': 3.289922480620155, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2788720', 'n_tokens_mistral': 946, 'n_tokens_neox': 927, 'n_words': 231}