32 emap->
Write(os, binary);
37 char c =
Peek(is, binary);
41 }
else if (c ==
'C') {
43 }
else if (c ==
'T') {
45 }
else if (c ==
'S') {
49 <<
", at file position " << is.tellg();
59 KALDI_ERR <<
"ConstantEventMap::Write(), could not write to stream.";
72 std::vector<EventMap*> table;
73 table.reserve(table_.size());
76 if (table_[value] != NULL) {
78 if (pruned_map != NULL) {
79 table.resize(value + 1, NULL);
80 table[value] = pruned_map;
84 if (table.empty())
return NULL;
89 const unordered_set<EventKeyType> &keys_to_map,
90 const unordered_map<EventValueType,EventValueType> &value_map)
const {
91 std::vector<EventMap*> table;
92 table.reserve(table_.size());
95 if (table_[value] != NULL) {
99 if (keys_to_map.count(key_) == 0) mapped_value = value;
101 unordered_map<EventValueType,EventValueType>::const_iterator
102 iter = value_map.find(value);
103 if (iter == value_map.end()) {
104 KALDI_ERR <<
"Could not map value " << value
105 <<
" for key " << key_;
107 mapped_value = iter->second;
110 if (static_cast<EventValueType>(table.size()) <= mapped_value)
111 table.resize(mapped_value + 1, NULL);
112 if (table[mapped_value] != NULL)
113 KALDI_ERR <<
"Multiple values map to the same point: this code cannot " 114 <<
"handle this case.";
115 table[mapped_value] = this_map;
125 uint32 size = table_.size();
128 for (
size_t t = 0; t < size; t++) {
133 if (!binary) os <<
'\n';
135 KALDI_ERR <<
"TableEventMap::Write(), could not write to stream.";
146 std::vector<EventMap*> table(size);
148 for (
size_t t = 0; t < size; t++) {
159 if (yes == NULL && no == NULL)
return NULL;
160 else if (yes == NULL)
return no;
161 else if (no == NULL)
return yes;
166 const unordered_set<EventKeyType> &keys_to_map,
167 const unordered_map<EventValueType,EventValueType> &value_map)
const {
169 *no = no_->
MapValues(keys_to_map, value_map);
171 if (keys_to_map.count(key_) == 0) {
174 std::vector<EventValueType> yes_set;
176 iter != yes_set_.end();
179 unordered_map<EventValueType, EventValueType>::const_iterator
180 map_iter = value_map.find(value);
181 if (map_iter == value_map.end())
182 KALDI_ERR <<
"Value " << value <<
", for key " 183 << key_ <<
", cannot be mapped.";
185 yes_set.push_back(mapped_value);
196 yes_set_.Write(os, binary);
199 yes_->Write(os, binary);
200 no_->Write(os, binary);
202 if (!binary) os <<
'\n';
204 KALDI_ERR <<
"SplitEventMap::Write(), could not write to stream.";
216 yes_set.
Read(is, binary);
223 if (yes == NULL || no == NULL)
KALDI_ERR <<
"SplitEventMap::Read, NULL pointers.";
230 uint32 size = evec.size();
232 for (
size_t i = 0;
i < size;
i++) {
236 if (!binary) os <<
'\n';
245 for (
size_t i = 0;
i < size;
i++) {
254 std::stringstream ss;
255 EventType::const_iterator iter = evec.begin(), end = evec.end();
256 std::string sep =
"";
257 for (; iter != end; ++iter) {
258 ss << sep << iter->first <<
":"<<iter->second;
265 EventType::const_iterator iter = vec.begin(), end = vec.end();
267 const size_t kPrime1=47087, kPrime2=1321;
268 for (; iter != end; ++iter) {
269 #ifdef KALDI_PARANOID // Check names are distinct and increasing. 270 EventType::const_iterator iter2=iter; iter2++;
271 if (iter2 != end) {
KALDI_ASSERT(iter->first < iter2->first); }
273 ans += iter->first + kPrime1*iter->second;
281 void EventMap::Check(
const std::vector<std::pair<EventKeyType, EventValueType> > &event) {
283 size_t sz =
event.size();
284 for (
size_t i = 0;
i+1 < sz;
i++)
295 #ifdef KALDI_PARANOID 298 std::vector<std::pair<EventKeyType, EventValueType> >::const_iterator
299 begin =
event.begin(),
305 size_t sz = end-begin, half;
308 middle = begin + half;
309 if (middle->first < key) {
317 if (begin != end && begin->first == key) {
318 *ans = begin->second;
326 if (map_in.size() == 0)
330 table_.resize(highest_val+1, NULL);
331 std::map<EventValueType, EventMap*>::const_iterator iter = map_in.begin(), end = map_in.end();
332 for (; iter != end; ++iter) {
333 KALDI_ASSERT(iter->first >= 0 && iter->first <= highest_val);
334 table_[iter->first] = iter->second;
340 if (map_in.size() == 0)
344 table_.resize(highest_val+1, NULL);
345 std::map<EventValueType, EventAnswerType>::const_iterator iter = map_in.begin(), end = map_in.end();
346 for (; iter != end; ++iter) {
347 KALDI_ASSERT(iter->first >= 0 && iter->first <= highest_val);
355 std::vector<EventMap*> children;
357 return children.empty();
377 std::vector<const EventMap*> *nonleaf_nodes,
378 std::map<const EventMap*, const EventMap*> *nonleaf_parents,
379 std::vector<const EventMap*> *leaf_parents) {
381 std::vector<const EventMap*> queue;
385 queue.push_back(top_node);
386 nonleaf_nodes->push_back(top_node);
387 (*nonleaf_parents)[top_node] = top_node;
389 while (!queue.empty()) {
390 const EventMap *parent = queue.back();
392 std::vector<EventMap*> children;
395 for (
size_t i = 0;
i < children.size();
i++) {
400 || leaf < 0)
return false;
401 if (static_cast<int32>(leaf_parents->size()) <= leaf)
402 leaf_parents->resize(leaf+1, NULL);
403 if ((*leaf_parents)[leaf] != NULL) {
404 KALDI_WARN <<
"Repeated leaf! Did you suppress leaf clustering when building the tree?";
407 (*leaf_parents)[leaf] = parent;
409 nonleaf_nodes->push_back(child);
410 (*nonleaf_parents)[child] = parent;
411 queue.push_back(child);
416 for (
size_t i = 0;
i < leaf_parents->size();
i++)
417 if ((*leaf_parents)[
i] == NULL) {
418 KALDI_WARN <<
"non-consecutively numbered leaves";
431 std::vector<int32> *parents) {
437 || leaf != 0)
return false;
448 std::vector<const EventMap*> nonleaf_nodes;
452 std::map<const EventMap*, const EventMap*> nonleaf_parents;
455 std::vector<const EventMap*> leaf_parents;
459 &leaf_parents))
return false;
461 *num_leaves = leaf_parents.size();
462 int32 num_nodes = leaf_parents.size() + nonleaf_nodes.size();
464 std::map<const EventMap*, int32> nonleaf_indices;
468 for (
size_t i = 0;
i < nonleaf_nodes.size();
i++)
469 nonleaf_indices[nonleaf_nodes[
i]] = num_nodes -
i - 1;
471 parents->resize(num_nodes);
472 for (
size_t i = 0;
i < leaf_parents.size();
i++) {
474 (*parents)[
i] = nonleaf_indices[leaf_parents[
i]];
476 for (
size_t i = 0;
i < nonleaf_nodes.size();
i++) {
478 KALDI_ASSERT(nonleaf_parents.count(nonleaf_nodes[i]) != 0);
479 KALDI_ASSERT(nonleaf_indices.count(nonleaf_parents[nonleaf_nodes[i]]) != 0);
480 int32 index = nonleaf_indices[nonleaf_nodes[
i]],
481 parent_index = nonleaf_indices[nonleaf_parents[nonleaf_nodes[
i]]];
483 (*parents)[index] = parent_index;
485 for (
int32 i = 0;
i < num_nodes;
i++)
bool GetTreeStructure(const EventMap &map, int32 *num_leaves, std::vector< int32 > *parents)
This function gets the tree structure of the EventMap "map" in a convenient form. ...
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
virtual EventMap * MapValues(const unordered_set< EventKeyType > &keys_to_map, const unordered_map< EventValueType, EventValueType > &value_map) const
virtual void Write(std::ostream &os, bool binary)
Write to stream.
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
static TableEventMap * Read(std::istream &is, bool binary)
std::string EventTypeToString(const EventType &evec)
void SortAndUniq(std::vector< T > *vec)
Sorts and uniq's (removes duplicates) from a vector.
int Peek(std::istream &is, bool binary)
Peek consumes whitespace (if binary == false) and then returns the peek() value of the stream...
virtual bool Map(const EventType &event, EventAnswerType *ans) const =0
static EventMap * Read(std::istream &is, bool binary)
a Read function that reads an arbitrary EventMap; also works for NULL pointers.
std::vector< EventMap * > table_
void WriteEventType(std::ostream &os, bool binary, const EventType &evec)
virtual void Write(std::ostream &os, bool binary)
Write to stream.
static void Check(const EventType &event)
std::vector< std::pair< EventKeyType, EventValueType > > EventType
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
size_t operator()(const EventType &vec)
virtual EventMap * MapValues(const unordered_set< EventKeyType > &keys_to_map, const unordered_map< EventValueType, EventValueType > &value_map) const
int32 EventKeyType
Things of type EventKeyType can take any value.
void ReadEventType(std::istream &is, bool binary, EventType *evec)
virtual EventMap * MapValues(const unordered_set< EventKeyType > &keys_to_map, const unordered_map< EventValueType, EventValueType > &value_map) const =0
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
static bool GetTreeStructureInternal(const EventMap &map, std::vector< const EventMap *> *nonleaf_nodes, std::map< const EventMap *, const EventMap *> *nonleaf_parents, std::vector< const EventMap *> *leaf_parents)
std::string CharToString(const char &c)
void Read(std::istream &is, bool binary)
virtual void Write(std::ostream &os, bool binary)
Write to stream.
static bool Lookup(const EventType &event, EventKeyType key, EventValueType *ans)
A class that is capable of representing a generic mapping from EventType (which is a vector of (key...
static bool IsLeafNode(const EventMap *e)
static SplitEventMap * Read(std::istream &is, bool binary)
#define KALDI_ASSERT(cond)
virtual void GetChildren(std::vector< EventMap *> *out) const =0
TableEventMap(EventKeyType key, const std::vector< EventMap *> &table)
Takes ownership of pointers.
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
virtual EventMap * Prune() const
static ConstantEventMap * Read(std::istream &is, bool binary)
int32 EventAnswerType
As far as the event-map code itself is concerned, things of type EventAnswerType may take any value e...
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
virtual EventMap * Prune() const =0
virtual EventMap * Prune() const
virtual void Write(std::ostream &os, bool binary)=0
Write to stream.