document_format.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Copyright 2016 Google Inc. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. // An interface for document formats.
  13. #ifndef SYNTAXNET_DOCUMENT_FORMAT_H__
  14. #define SYNTAXNET_DOCUMENT_FORMAT_H__
  15. #include <string>
  16. #include <vector>
  17. #include "syntaxnet/registry.h"
  18. #include "syntaxnet/sentence.pb.h"
  19. #include "syntaxnet/task_context.h"
  20. #include "syntaxnet/utils.h"
  21. #include "tensorflow/core/lib/io/buffered_inputstream.h"
  22. namespace syntaxnet {
  23. // A document format component converts a key/value pair from a record to one or
  24. // more documents. The record format is used for selecting the document format
  25. // component. A document format component can be registered with the
  26. // REGISTER_SYNTAXNET_DOCUMENT_FORMAT macro.
  27. class DocumentFormat : public RegisterableClass<DocumentFormat> {
  28. public:
  29. DocumentFormat() {}
  30. virtual ~DocumentFormat() {}
  31. virtual void Setup(TaskContext *context) {}
  32. // Reads a record from the given input buffer with format specific logic.
  33. // Returns false if no record could be read because we reached end of file.
  34. virtual bool ReadRecord(tensorflow::io::BufferedInputStream *buffer,
  35. string *record) = 0;
  36. // Converts a key/value pair to one or more documents.
  37. virtual void ConvertFromString(const string &key, const string &value,
  38. std::vector<Sentence *> *documents) = 0;
  39. // Converts a document to a key/value pair.
  40. virtual void ConvertToString(const Sentence &document,
  41. string *key, string *value) = 0;
  42. private:
  43. TF_DISALLOW_COPY_AND_ASSIGN(DocumentFormat);
  44. };
  45. #define REGISTER_SYNTAXNET_DOCUMENT_FORMAT(type, component) \
  46. REGISTER_SYNTAXNET_CLASS_COMPONENT(DocumentFormat, type, component)
  47. } // namespace syntaxnet
  48. #endif // SYNTAXNET_DOCUMENT_FORMAT_H__