#include "unigram_model.h"
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <complex>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "util.h"
namespace sentencepiece {
namespace unigram {
namespace {
constexpr size_t kPreallocateLatticeNodeSize = …;
constexpr float kUnkPenalty = …;
constexpr float kEpsilon = …;
inline float LogSumExp(float x, float y, bool init_mode) { … }
inline float Gumbel() { … }
}
Lattice::Lattice() : … { … }
Lattice::~Lattice() { … }
const std::vector<Lattice::Node *> &Lattice::begin_nodes(int pos) const { … }
const std::vector<Lattice::Node *> &Lattice::end_nodes(int pos) const { … }
int Lattice::size() const { … }
int Lattice::utf8_size() const { … }
const char *Lattice::sentence() const { … }
const char *Lattice::surface(int pos) const { … }
Lattice::Node *Lattice::bos_node() const { … }
Lattice::Node *Lattice::eos_node() const { … }
Lattice::Node *Lattice::NewNode() { … }
void Lattice::Clear() { … }
void Lattice::SetSentence(absl::string_view sentence) { … }
Lattice::Node *Lattice::Insert(int pos, int length) { … }
Lattice::LatticePathWithScore Lattice::Viterbi() { … }
std::vector<float> Lattice::ForwardAlgorithm(float inv_theta) const { … }
std::vector<float> Lattice::BackwardAlgorithm(float inv_theta) const { … }
float Lattice::PopulateMarginal(float freq,
std::vector<float>* expected) const { … }
float Lattice::CalculateEntropy(float inv_theta) const { … }
namespace {
struct Hypothesis { … };
Hypothesis* CloneHypAndDependents(
const Hypothesis* to_clone,
absl::flat_hash_map<const Hypothesis*, Hypothesis*>* clone_map,
model::FreeList<Hypothesis>* allocator) { … }
}
std::vector<Lattice::LatticePathWithScore> Lattice::NBest(size_t nbest_size,
bool sample,
float inv_theta) { … }
std::vector<Lattice::Node*> Lattice::Sample(float inv_theta) { … }
void Model::PopulateNodes(Lattice *lattice) const { … }
int Model::PieceToId(absl::string_view piece) const { … }
void Model::BuildTrie(std::vector<std::pair<absl::string_view, int>> *pieces) { … }
Model::Model(const ModelProto &model_proto) { … }
Model::~Model() { … }
EncodeResult Model::Encode(absl::string_view normalized) const { … }
NBestEncodeResult Model::NBestEncode(absl::string_view normalized,
int nbest_size) const { … }
EncodeResult Model::SampleEncode(absl::string_view normalized,
float inv_theta) const { … }
NBestEncodeResult Model::SampleEncodeAndScore(absl::string_view normalized,
float inv_theta,
int samples,
bool wor,
bool include_best) const { … }
float Model::CalculateEntropy(absl::string_view normalized,
float inv_theta) const { … }
bool Model::VerifyOutputsEquivalent(absl::string_view expected,
absl::string_view actual) const { … }
EncodeResult Model::EncodeOptimized(absl::string_view normalized) const { … }
}
}