affix.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/affix.h"
  13. #include <ctype.h>
  14. #include <string.h>
  15. #include <functional>
  16. #include <string>
  17. #include "syntaxnet/shared_store.h"
  18. #include "syntaxnet/task_context.h"
  19. #include "syntaxnet/term_frequency_map.h"
  20. #include "syntaxnet/utils.h"
  21. #include "syntaxnet/workspace.h"
  22. #include "tensorflow/core/lib/core/status.h"
  23. #include "tensorflow/core/platform/env.h"
  24. #include "tensorflow/core/platform/regexp.h"
  25. #include "util/utf8/unicodetext.h"
  26. namespace syntaxnet {
  27. // Initial number of buckets in term and affix hash maps. This must be a power
  28. // of two.
  29. static const int kInitialBuckets = 1024;
  30. // Fill factor for term and affix hash maps.
  31. static const int kFillFactor = 2;
  32. int TermHash(const string &term) {
  33. return utils::Hash32(term.data(), term.size(), 0xDECAF);
  34. }
  35. // Copies a substring of a Unicode text to a string.
  36. static void UnicodeSubstring(const UnicodeText::const_iterator &start,
  37. const UnicodeText::const_iterator &end,
  38. string *result) {
  39. result->clear();
  40. result->append(start.utf8_data(), end.utf8_data() - start.utf8_data());
  41. }
  42. AffixTable::AffixTable(Type type, int max_length) {
  43. type_ = type;
  44. max_length_ = max_length;
  45. Resize(0);
  46. }
  47. AffixTable::~AffixTable() { Reset(0); }
  48. void AffixTable::Reset(int max_length) {
  49. // Save new maximum affix length.
  50. max_length_ = max_length;
  51. // Delete all data.
  52. for (size_t i = 0; i < affixes_.size(); ++i) delete affixes_[i];
  53. affixes_.clear();
  54. buckets_.clear();
  55. Resize(0);
  56. }
  57. void AffixTable::Read(const AffixTableEntry &table_entry) {
  58. CHECK_EQ(table_entry.type(), type_ == PREFIX ? "PREFIX" : "SUFFIX");
  59. CHECK_GE(table_entry.max_length(), 0);
  60. Reset(table_entry.max_length());
  61. // First, create all affixes.
  62. for (int affix_id = 0; affix_id < table_entry.affix_size(); ++affix_id) {
  63. const auto &affix_entry = table_entry.affix(affix_id);
  64. CHECK_GE(affix_entry.length(), 0);
  65. CHECK_LE(affix_entry.length(), max_length_);
  66. CHECK(FindAffix(affix_entry.form()) == nullptr); // forbid duplicates
  67. Affix *affix = AddNewAffix(affix_entry.form(), affix_entry.length());
  68. CHECK_EQ(affix->id(), affix_id);
  69. }
  70. CHECK_EQ(affixes_.size(), table_entry.affix_size());
  71. // Next, link the shorter affixes.
  72. for (int affix_id = 0; affix_id < table_entry.affix_size(); ++affix_id) {
  73. const auto &affix_entry = table_entry.affix(affix_id);
  74. if (affix_entry.shorter_id() == -1) {
  75. CHECK_EQ(affix_entry.length(), 1);
  76. continue;
  77. }
  78. CHECK_GT(affix_entry.length(), 1);
  79. CHECK_GE(affix_entry.shorter_id(), 0);
  80. CHECK_LT(affix_entry.shorter_id(), affixes_.size());
  81. Affix *affix = affixes_[affix_id];
  82. Affix *shorter = affixes_[affix_entry.shorter_id()];
  83. CHECK_EQ(affix->length(), shorter->length() + 1);
  84. affix->set_shorter(shorter);
  85. }
  86. }
  87. void AffixTable::Read(ProtoRecordReader *reader) {
  88. AffixTableEntry table_entry;
  89. TF_CHECK_OK(reader->Read(&table_entry));
  90. Read(table_entry);
  91. }
  92. void AffixTable::Write(AffixTableEntry *table_entry) const {
  93. table_entry->Clear();
  94. table_entry->set_type(type_ == PREFIX ? "PREFIX" : "SUFFIX");
  95. table_entry->set_max_length(max_length_);
  96. for (const Affix *affix : affixes_) {
  97. auto *affix_entry = table_entry->add_affix();
  98. affix_entry->set_form(affix->form());
  99. affix_entry->set_length(affix->length());
  100. affix_entry->set_shorter_id(
  101. affix->shorter() == nullptr ? -1 : affix->shorter()->id());
  102. }
  103. }
  104. void AffixTable::Write(ProtoRecordWriter *writer) const {
  105. AffixTableEntry table_entry;
  106. Write(&table_entry);
  107. writer->Write(table_entry);
  108. }
  109. Affix *AffixTable::AddAffixesForWord(const char *word, size_t size) {
  110. // The affix length is measured in characters and not bytes so we need to
  111. // determine the length in characters.
  112. UnicodeText text;
  113. text.PointToUTF8(word, size);
  114. int length = text.size();
  115. // Determine longest affix.
  116. int affix_len = length;
  117. if (affix_len > max_length_) affix_len = max_length_;
  118. if (affix_len == 0) return nullptr;
  119. // Find start and end of longest affix.
  120. UnicodeText::const_iterator start, end;
  121. if (type_ == PREFIX) {
  122. start = end = text.begin();
  123. for (int i = 0; i < affix_len; ++i) ++end;
  124. } else {
  125. start = end = text.end();
  126. for (int i = 0; i < affix_len; ++i) --start;
  127. }
  128. // Try to find successively shorter affixes.
  129. Affix *top = nullptr;
  130. Affix *ancestor = nullptr;
  131. string s;
  132. while (affix_len > 0) {
  133. // Try to find affix in table.
  134. UnicodeSubstring(start, end, &s);
  135. Affix *affix = FindAffix(s);
  136. if (affix == nullptr) {
  137. // Affix not found, add new one to table.
  138. affix = AddNewAffix(s, affix_len);
  139. // Update ancestor chain.
  140. if (ancestor != nullptr) ancestor->set_shorter(affix);
  141. ancestor = affix;
  142. if (top == nullptr) top = affix;
  143. } else {
  144. // Affix found. Update ancestor if needed and return match.
  145. if (ancestor != nullptr) ancestor->set_shorter(affix);
  146. if (top == nullptr) top = affix;
  147. break;
  148. }
  149. // Next affix.
  150. if (type_ == PREFIX) {
  151. --end;
  152. } else {
  153. ++start;
  154. }
  155. affix_len--;
  156. }
  157. return top;
  158. }
  159. Affix *AffixTable::GetAffix(int id) const {
  160. if (id < 0 || id >= static_cast<int>(affixes_.size())) {
  161. return nullptr;
  162. } else {
  163. return affixes_[id];
  164. }
  165. }
  166. string AffixTable::AffixForm(int id) const {
  167. Affix *affix = GetAffix(id);
  168. if (affix == nullptr) {
  169. return "";
  170. } else {
  171. return affix->form();
  172. }
  173. }
  174. int AffixTable::AffixId(const string &form) const {
  175. Affix *affix = FindAffix(form);
  176. if (affix == nullptr) {
  177. return -1;
  178. } else {
  179. return affix->id();
  180. }
  181. }
  182. Affix *AffixTable::AddNewAffix(const string &form, int length) {
  183. int hash = TermHash(form);
  184. int id = affixes_.size();
  185. if (id > static_cast<int>(buckets_.size()) * kFillFactor) Resize(id);
  186. int b = hash & (buckets_.size() - 1);
  187. // Create new affix object.
  188. Affix *affix = new Affix(id, form.c_str(), length);
  189. affixes_.push_back(affix);
  190. // Insert affix in bucket chain.
  191. affix->next_ = buckets_[b];
  192. buckets_[b] = affix;
  193. return affix;
  194. }
  195. Affix *AffixTable::FindAffix(const string &form) const {
  196. // Compute hash value for word.
  197. int hash = TermHash(form);
  198. // Try to find affix in hash table.
  199. Affix *affix = buckets_[hash & (buckets_.size() - 1)];
  200. while (affix != nullptr) {
  201. if (strcmp(affix->form_.c_str(), form.c_str()) == 0) return affix;
  202. affix = affix->next_;
  203. }
  204. return nullptr;
  205. }
  206. void AffixTable::Resize(int size_hint) {
  207. // Compute new size for bucket array.
  208. int new_size = kInitialBuckets;
  209. while (new_size < size_hint) new_size *= 2;
  210. int mask = new_size - 1;
  211. // Distribute affixes in new buckets.
  212. buckets_.resize(new_size);
  213. for (size_t i = 0; i < buckets_.size(); ++i) {
  214. buckets_[i] = nullptr;
  215. }
  216. for (size_t i = 0; i < affixes_.size(); ++i) {
  217. Affix *affix = affixes_[i];
  218. int b = TermHash(affix->form_) & mask;
  219. affix->next_ = buckets_[b];
  220. buckets_[b] = affix;
  221. }
  222. }
  223. } // namespace syntaxnet