Helper function to initialize an InitializableLookupTable from a text file.
| 337 | |
| 338 | // Helper function to initialize an InitializableLookupTable from a text file. |
| 339 | Status InitializeTableFromTextFile(const string& filename, int64 vocab_size, |
| 340 | char delimiter, int32 key_index, |
| 341 | int32 value_index, Env* env, |
| 342 | InitializableLookupTable* table) { |
| 343 | if (key_index == kLineNumber && table->key_dtype() != DT_INT64) { |
| 344 | return errors::InvalidArgument( |
| 345 | "Key index for line number requires table key dtype of int64, got ", |
| 346 | DataTypeString(table->key_dtype())); |
| 347 | } |
| 348 | const DataType& key_dtype = table->key_dtype(); |
| 349 | const DataType& value_dtype = table->value_dtype(); |
| 350 | if (key_index == kWholeLine && !DataTypeIsInteger(key_dtype) && |
| 351 | key_dtype != DT_STRING) { |
| 352 | return errors::InvalidArgument( |
| 353 | "Key index for whole line requires string or integer table key, got ", |
| 354 | DataTypeString(table->key_dtype())); |
| 355 | } |
| 356 | if (value_index == kLineNumber && value_dtype != DT_INT64) { |
| 357 | return errors::InvalidArgument( |
| 358 | "Value index for line number requires table value dtype of int64, got ", |
| 359 | DataTypeString(table->value_dtype())); |
| 360 | } |
| 361 | if (value_index == kWholeLine && value_dtype != DT_STRING) { |
| 362 | return errors::InvalidArgument( |
| 363 | "Value index for whole line requires table value dtype of string, got ", |
| 364 | DataTypeString(table->value_dtype())); |
| 365 | } |
| 366 | |
| 367 | TextFileLineIterator iter; |
| 368 | TF_RETURN_IF_ERROR(iter.Init(filename, vocab_size, delimiter, key_dtype, |
| 369 | key_index, value_dtype, value_index, env)); |
| 370 | // For initialization from files, ignore if the table is already |
| 371 | // initialized. The table shared name should contain the filename to |
| 372 | // avoid trying to initialize the same table from the same file at the same |
| 373 | // time. |
| 374 | Status s = table->Initialize(iter); |
| 375 | if (errors::IsFailedPrecondition(s) && table->is_initialized()) { |
| 376 | LOG(INFO) << "Table trying to initialize from file " << filename |
| 377 | << " is already initialized."; |
| 378 | return Status::OK(); |
| 379 | } |
| 380 | return s; |
| 381 | } |
| 382 | |
| 383 | } // namespace lookup |
| 384 | } // namespace tensorflow |
no test coverage detected