You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.0 KiB
66 lines
2.0 KiB
7 years ago
|
#ifndef DECODER_UTILS_H_
|
||
|
#define DECODER_UTILS_H_
|
||
|
|
||
7 years ago
|
#include <utility>
|
||
7 years ago
|
#include "path_trie.h"
|
||
7 years ago
|
|
||
7 years ago
|
const float NUM_FLT_INF = std::numeric_limits<float>::max();
|
||
|
const float NUM_FLT_MIN = std::numeric_limits<float>::min();
|
||
|
|
||
7 years ago
|
// Function template for comparing two pairs
|
||
7 years ago
|
template <typename T1, typename T2>
|
||
7 years ago
|
bool pair_comp_first_rev(const std::pair<T1, T2> &a,
|
||
7 years ago
|
const std::pair<T1, T2> &b) {
|
||
|
return a.first > b.first;
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
// Function template for comparing two pairs
|
||
7 years ago
|
template <typename T1, typename T2>
|
||
7 years ago
|
bool pair_comp_second_rev(const std::pair<T1, T2> &a,
|
||
7 years ago
|
const std::pair<T1, T2> &b) {
|
||
|
return a.second > b.second;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// Return the sum of two probabilities in log scale
|
||
7 years ago
|
template <typename T>
|
||
7 years ago
|
T log_sum_exp(const T &x, const T &y) {
|
||
|
static T num_min = -std::numeric_limits<T>::max();
|
||
|
if (x <= num_min) return y;
|
||
|
if (y <= num_min) return x;
|
||
|
T xmax = std::max(x, y);
|
||
|
return std::log(std::exp(x - xmax) + std::exp(y - xmax)) + xmax;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// Functor for prefix comparsion
|
||
7 years ago
|
bool prefix_compare(const PathTrie *x, const PathTrie *y);
|
||
7 years ago
|
|
||
7 years ago
|
/* Get length of utf8 encoding string
|
||
|
* See: http://stackoverflow.com/a/4063229
|
||
|
*/
|
||
7 years ago
|
size_t get_utf8_str_len(const std::string &str);
|
||
7 years ago
|
|
||
7 years ago
|
/* Split a string into a list of strings on a given string
|
||
|
* delimiter. NB: delimiters on beginning / end of string are
|
||
|
* trimmed. Eg, "FooBarFoo" split on "Foo" returns ["Bar"].
|
||
|
*/
|
||
7 years ago
|
std::vector<std::string> split_str(const std::string &s,
|
||
|
const std::string &delim);
|
||
|
|
||
7 years ago
|
/* Splits string into vector of strings representing
|
||
|
* UTF-8 characters (not same as chars)
|
||
|
*/
|
||
7 years ago
|
std::vector<std::string> split_utf8_str(const std::string &str);
|
||
7 years ago
|
|
||
7 years ago
|
// Add a word in index to the dicionary of fst
|
||
7 years ago
|
void add_word_to_fst(const std::vector<int> &word,
|
||
|
fst::StdVectorFst *dictionary);
|
||
7 years ago
|
|
||
7 years ago
|
// Add a word in string to dictionary
|
||
7 years ago
|
bool add_word_to_dictionary(
|
||
|
const std::string &word,
|
||
|
const std::unordered_map<std::string, int> &char_map,
|
||
|
bool add_space,
|
||
|
int SPACE_ID,
|
||
|
fst::StdVectorFst *dictionary);
|
||
|
#endif // DECODER_UTILS_H
|