You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.6 KiB

  1. // util/simple-io-funcs.cc
  2. // Copyright 2009-2011 Microsoft Corporation
  3. // See ../../COPYING for clarification regarding multiple authors
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  10. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  11. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  12. // MERCHANTABLITY OR NON-INFRINGEMENT.
  13. // See the Apache 2 License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "util/simple-io-funcs.h"
  16. #include "util/text-utils.h"
  17. namespace kaldi {
  18. bool WriteIntegerVectorSimple(const std::string& wxfilename,
  19. const std::vector<int32>& list) {
  20. kaldi::Output ko;
  21. // false, false is: text-mode, no Kaldi header.
  22. if (!ko.Open(wxfilename, false, false)) return false;
  23. for (size_t i = 0; i < list.size(); i++) ko.Stream() << list[i] << '\n';
  24. return ko.Close();
  25. }
  26. bool ReadIntegerVectorSimple(const std::string& rxfilename,
  27. std::vector<int32>* list) {
  28. kaldi::Input ki;
  29. if (!ki.OpenTextMode(rxfilename)) return false;
  30. std::istream& is = ki.Stream();
  31. int32 i;
  32. list->clear();
  33. while (!(is >> i).fail()) list->push_back(i);
  34. is >> std::ws;
  35. return is.eof(); // should be eof, or junk at end of file.
  36. }
  37. bool WriteIntegerVectorVectorSimple(
  38. const std::string& wxfilename,
  39. const std::vector<std::vector<int32> >& list) {
  40. kaldi::Output ko;
  41. // false, false is: text-mode, no Kaldi header.
  42. if (!ko.Open(wxfilename, false, false)) return false;
  43. std::ostream& os = ko.Stream();
  44. for (size_t i = 0; i < list.size(); i++) {
  45. for (size_t j = 0; j < list[i].size(); j++) {
  46. os << list[i][j];
  47. if (j + 1 < list[i].size()) os << ' ';
  48. }
  49. os << '\n';
  50. }
  51. return ko.Close();
  52. }
  53. bool ReadIntegerVectorVectorSimple(const std::string& rxfilename,
  54. std::vector<std::vector<int32> >* list) {
  55. kaldi::Input ki;
  56. if (!ki.OpenTextMode(rxfilename)) return false;
  57. std::istream& is = ki.Stream();
  58. list->clear();
  59. std::string line;
  60. while (std::getline(is, line)) {
  61. std::vector<int32> v;
  62. if (!SplitStringToIntegers(line, " \t\r", true, &v)) {
  63. list->clear();
  64. return false;
  65. }
  66. list->push_back(v);
  67. }
  68. return is.eof(); // if we're not at EOF, something weird happened.
  69. }
  70. } // end namespace kaldi