morphology_label_set.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include "syntaxnet/morphology_label_set.h"
  13. namespace syntaxnet {
  14. const char MorphologyLabelSet::kSeparator[] = "\t";
  15. int MorphologyLabelSet::Add(const TokenMorphology &morph) {
  16. string repr = StringForMatch(morph);
  17. auto it = fast_lookup_.find(repr);
  18. if (it != fast_lookup_.end()) return it->second;
  19. fast_lookup_[repr] = label_set_.size();
  20. label_set_.push_back(morph);
  21. return label_set_.size() - 1;
  22. }
  23. // Look up an existing TokenMorphology. If it is not present, return -1.
  24. int MorphologyLabelSet::LookupExisting(const TokenMorphology &morph) const {
  25. string repr = StringForMatch(morph);
  26. auto it = fast_lookup_.find(repr);
  27. if (it != fast_lookup_.end()) return it->second;
  28. return -1;
  29. }
  30. // Return the TokenMorphology at position i. The input i should be in the range
  31. // 0..size().
  32. const TokenMorphology &MorphologyLabelSet::Lookup(int i) const {
  33. CHECK_GE(i, 0);
  34. CHECK_LT(i, label_set_.size());
  35. return label_set_[i];
  36. }
  37. void MorphologyLabelSet::Read(const string &filename) {
  38. ProtoRecordReader reader(filename);
  39. Read(&reader);
  40. }
  41. void MorphologyLabelSet::Read(ProtoRecordReader *reader) {
  42. TokenMorphology morph;
  43. while (reader->Read(&morph).ok()) {
  44. CHECK_EQ(-1, LookupExisting(morph));
  45. Add(morph);
  46. }
  47. }
  48. void MorphologyLabelSet::Write(const string &filename) const {
  49. ProtoRecordWriter writer(filename);
  50. Write(&writer);
  51. }
  52. void MorphologyLabelSet::Write(ProtoRecordWriter *writer) const {
  53. for (const TokenMorphology &morph : label_set_) {
  54. writer->Write(morph);
  55. }
  56. }
  57. string MorphologyLabelSet::StringForMatch(const TokenMorphology &morph) const {
  58. std::vector<string> attributes;
  59. for (const auto &a : morph.attribute()) {
  60. attributes.push_back(
  61. tensorflow::strings::StrCat(a.name(), kSeparator, a.value()));
  62. }
  63. std::sort(attributes.begin(), attributes.end());
  64. return utils::Join(attributes, kSeparator);
  65. }
  66. string FullLabelFeatureType::GetFeatureValueName(FeatureValue value) const {
  67. const TokenMorphology &morph = label_set_->Lookup(value);
  68. std::vector<string> attributes;
  69. for (const auto &a : morph.attribute()) {
  70. attributes.push_back(tensorflow::strings::StrCat(a.name(), ":", a.value()));
  71. }
  72. std::sort(attributes.begin(), attributes.end());
  73. return utils::Join(attributes, ",");
  74. }
  75. } // namespace syntaxnet