| 282 | } |
| 283 | |
| 284 | Status FixedUnigramSampler::LoadFromFile(Env* env, const string& vocab_file, |
| 285 | float distortion) { |
| 286 | std::unique_ptr<RandomAccessFile> file; |
| 287 | TF_RETURN_IF_ERROR(env->NewRandomAccessFile(vocab_file, &file)); |
| 288 | |
| 289 | io::InputBuffer in(file.get(), 262144 /*bytes*/); |
| 290 | string line; |
| 291 | int32 word_id = weights_.size(); |
| 292 | while (in.ReadLine(&line).ok()) { |
| 293 | // The vocabulary file should be in csv like format, with the last |
| 294 | // field the weight associated with the word. |
| 295 | std::vector<string> cols = str_util::Split(line, ','); |
| 296 | if (cols.empty()) continue; |
| 297 | // Skip entries that do not belong to this shard. |
| 298 | if (word_id % num_shards_ == shard_) { |
| 299 | float w = 0.0; |
| 300 | if (!strings::safe_strtof(cols.at(cols.size() - 1), &w)) { |
| 301 | return errors::InvalidArgument("Wrong vocabulary format at line: ", |
| 302 | line); |
| 303 | } |
| 304 | w = std::pow(w, distortion); |
| 305 | total_weight_ += w; |
| 306 | weights_.push_back(w); |
| 307 | } |
| 308 | ++word_id; |
| 309 | } |
| 310 | return Status::OK(); |
| 311 | } |
| 312 | |
| 313 | void FixedUnigramSampler::LoadFromUnigrams(const std::vector<float>& unigrams, |
| 314 | float distortion) { |
nothing calls this directly
no test coverage detected