MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / PopulateNodes

Method PopulateNodes

external/sentencepiece/src/unigram_model.cc:547–596  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

545// Model::~Model() {}
546
547void Model::PopulateNodes(Lattice *lattice) const {
548 auto get_chars_length = [&lattice](int begin_pos, const char *end) {
549 int pos = begin_pos;
550 while (lattice->surface(pos) < end) ++pos;
551 return pos - begin_pos;
552 };
553
554 const float unk_score = min_score() - kUnkPenalty;
555
556 const int len = lattice->size();
557 const char *end = lattice->sentence() + lattice->utf8_size();
558
559 // +1 just in case.
560 std::vector<Darts::DoubleArray::result_pair_type> trie_results(
561 trie_results_size_ + 1);
562
563 for (int begin_pos = 0; begin_pos < len; ++begin_pos) {
564 const char *begin = lattice->surface(begin_pos);
565
566 // Finds all pieces which are prefix of surface(begin_pos).
567 const size_t num_nodes = trie_->commonPrefixSearch(
568 begin, trie_results.data(), trie_results.size(),
569 static_cast<int>(end - begin));
570 CHECK_LT(num_nodes, trie_results.size());
571
572 bool has_single_node = false;
573
574 // Inserts pieces to the lattice.
575 for (size_t k = 0; k < num_nodes; ++k) {
576 const int length =
577 get_chars_length(begin_pos, begin + trie_results[k].length);
578 const int id = trie_results[k].value;
579 if (IsUnusedInlined(id)) continue;
580 Lattice::Node *node = lattice->Insert(begin_pos, length);
581 node->id = id; // the value of Trie stores vocab_id.
582 // User defined symbol receives extra bonus to always be selected.
583 node->score = IsUserDefinedInlined(id) ? (length * max_score_ - 0.1)
584 : GetScoreInlined(id);
585 if (!has_single_node && node->length == 1) {
586 has_single_node = true;
587 }
588 }
589
590 if (!has_single_node) {
591 Lattice::Node *node = lattice->Insert(begin_pos, 1);
592 node->id = unk_id_; // add UNK node.
593 node->score = unk_score;
594 }
595 }
596}
597
598int Model::PieceToId(absl::string_view piece) const {
599 auto it = reserved_id_map_.find(piece);

Callers 4

RunEStepMethod · 0.80
PruneSentencePiecesMethod · 0.80
TESTFunction · 0.80
TEST_PFunction · 0.80

Calls 6

surfaceMethod · 0.80
sentenceMethod · 0.80
utf8_sizeMethod · 0.80
InsertMethod · 0.80
sizeMethod · 0.45
dataMethod · 0.45

Tested by 2

TESTFunction · 0.64
TEST_PFunction · 0.64