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

vocab.h 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_TEXT_VOCAB_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_TEXT_VOCAB_H_
  18. #include <string>
  19. #include <memory>
  20. #include <unordered_map>
  21. #include <vector>
  22. #include "minddata/dataset/util/status.h"
  23. #ifdef ENABLE_PYTHON
  24. #include "pybind11/pybind11.h"
  25. #include "pybind11/stl.h"
  26. #endif
  27. namespace mindspore {
  28. namespace dataset {
  29. #ifdef ENABLE_PYTHON
  30. namespace py = pybind11;
  31. #endif
  32. using WordIdType = int32_t;
  33. using WordType = std::string;
  34. class Vocab {
  35. public:
  36. #ifdef ENABLE_PYTHON
  37. // Build a vocab from a python dictionary key is each word ,id needs to start from 2, no duplicate and continuous
  38. // @param const py::dict &words - a dictionary containing word, word id pair.
  39. // @param std::shared_ptr<Vocab> *vocab - return value, vocab object
  40. // @return error code
  41. static Status BuildFromPyDict(const py::dict &words, std::shared_ptr<Vocab> *vocab);
  42. // Build a vocab from a python list, id will be assigned automatically, start from 2
  43. // @param const py::list &words - a list of string, used to build vocab, id starts from 2
  44. // @param std::shared_ptr<Vocab> *vocab - return value, vocab object
  45. // @return error code
  46. static Status BuildFromPyList(const py::list &words, const py::list &special_tokens, bool prepend_special,
  47. std::shared_ptr<Vocab> *vocab);
  48. // Build a vocab from reading a vocab file, id are automatically assigned, start from 2
  49. // @param std::string &path - path to vocab file , each line is assumed to contain 1 word
  50. // @param std::string &delimiter - delimiter to break each line with
  51. // @param int32_t vocab_size - number of words to read from file
  52. // @param std::shared_ptr<Vocab> *vocab - return value, vocab object
  53. // @return error code
  54. static Status BuildFromFile(const std::string &path, const std::string &delimiter, int32_t vocab_size,
  55. const py::list &special_tokens, bool prepend_special, std::shared_ptr<Vocab> *vocab);
  56. #endif
  57. /// \brief Build a vocab from a c++ map. id needs to start from 2, no duplicate and continuous
  58. /// \param[in] words An unordered_map containing word, word id pair.
  59. /// \param[out] vocab A vocab object
  60. /// \return Error code
  61. static Status BuildFromUnorderedMap(const std::unordered_map<WordType, WordIdType> &words,
  62. std::shared_ptr<Vocab> *vocab);
  63. /// \brief Build a vocab from a c++ vector. id needs to start from 2, no duplicate and continuous
  64. /// \param[in] words A vector of string, used to build vocab, id starts from 2
  65. /// \param[in] special_tokens A vector of string contain special tokens
  66. /// \param[in] prepend_special Whether special_tokens will be prepended/appended to vocab
  67. /// \param[out] vocab A vocab object
  68. /// \return Error code
  69. static Status BuildFromVector(const std::vector<WordType> &words, const std::vector<WordType> &special_tokens,
  70. bool prepend_special, std::shared_ptr<Vocab> *vocab);
  71. /// \brief Build a vocab from reading a vocab file, id are automatically assigned, start from 2
  72. /// \param[in] path Path to vocab file , each line is assumed to contain 1 word
  73. /// \param[in] delimiter Delimiter to break each line with
  74. /// \param[in] vocab_size Number of words to read from file
  75. /// \param[in] special_tokens A vector of string contain special tokens
  76. /// \param[in] prepend_special Whether special_tokens will be prepended/appended to vocab
  77. /// \param[out] vocab A vocab object
  78. /// \return Error code
  79. static Status BuildFromFileCpp(const std::string &path, const std::string &delimiter, int32_t vocab_size,
  80. const std::vector<WordType> &special_tokens, bool prepend_special,
  81. std::shared_ptr<Vocab> *vocab);
  82. // Lookup the id of a word, if word doesn't exist in vocab, return default_id
  83. // @param const WordType word - word to look up
  84. // @param WordIdType default_id - word id to return to user when its not in the vocab
  85. // @return WordIdType, word_id
  86. WordIdType Lookup(const WordType &word) const;
  87. // constructor, shouldn't be called directly, can't be private due to std::make_unique()
  88. // @param std::unordered_map<WordType, WordIdType> map - sanitized word2id map
  89. explicit Vocab(std::unordered_map<WordType, WordIdType> map);
  90. Vocab() = default;
  91. // add one word to vocab, increment it's index automatically
  92. // @param std::string & word - word to be added will skip if word already exists
  93. void append_word(const std::string &word);
  94. // return a read-only vocab
  95. const std::unordered_map<WordType, WordIdType> vocab() { return word2id_; }
  96. // destructor
  97. ~Vocab() = default;
  98. static const WordIdType kNoTokenExists;
  99. private:
  100. std::unordered_map<WordType, WordIdType> word2id_;
  101. };
  102. } // namespace dataset
  103. } // namespace mindspore
  104. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_TEXT_VOCAB_H_