MCPcopy Create free account
hub / github.com/catboost/catboost / AddImpl

Function AddImpl

library/cpp/text_processing/dictionary/bpe_builder.cpp:25–72  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23
24template <typename TStringVector>
25static void AddImpl(
26 TStringVector tokens,
27 ui64 weight,
28 bool skipUnknown,
29 const TDictionary& alphabet,
30 TVector<TEraseList<TTokenId>>* tokenIdsLists,
31 TPairStats* pairStats,
32 TVector<ui64>* counts
33) {
34 const TTokenId eosId = alphabet.GetEndOfSentenceTokenId();
35 const auto& alphabetOptins = alphabet.GetDictionaryOptionsRef();
36 const auto tokenLevelType = alphabetOptins.TokenLevelType;
37
38 const bool addEndOfWordToken = alphabetOptins.EndOfWordTokenPolicy == EEndOfWordTokenPolicy::Insert;
39 const bool addEndOfSentenceToken = alphabetOptins.EndOfSentenceTokenPolicy == EEndOfSentenceTokenPolicy::Insert;
40
41 tokenIdsLists->emplace_back();
42 counts->push_back(weight);
43
44 if (tokenLevelType == ETokenLevelType::Letter) {
45 auto addTokenFunc = [&] (TStringBuf character) {
46 AddToken(character, skipUnknown, alphabet, &tokenIdsLists->back());
47 };
48 ApplyFuncToLetterNGrams(tokens, /*gramOrder*/1, addEndOfWordToken, addTokenFunc);
49 } else {
50 Y_ASSERT(tokenLevelType == ETokenLevelType::Word);
51 for (const auto& token : tokens) {
52 AddToken(token, skipUnknown, alphabet, &tokenIdsLists->back());
53 }
54 }
55
56 if (tokenIdsLists->back().Empty()) {
57 tokenIdsLists->pop_back();
58 counts->pop_back();
59 return;
60 }
61 if (tokenLevelType == ETokenLevelType::Word && addEndOfSentenceToken) {
62 tokenIdsLists->back().PushToken(eosId);
63 }
64
65 for (int i = (int)tokenIdsLists->back().Size() - 2; i >= 0; --i) {
66 TPair pair = tokenIdsLists->back().GetPair(i);
67 TPairStat& stat = (*pairStats)[pair];
68 stat.Pair = pair;
69 stat.Count += weight;
70 stat.Positions.emplace_back(std::make_pair((int)tokenIdsLists->size() - 1, i));
71 }
72}
73
74void TBpeDictionaryBuilder::Add(TConstArrayRef<TStringBuf> tokens, ui64 weight) {
75 AddImpl(tokens, weight, SkipUnknown, *Alphabet, &TokenIdsLists, &PairStats, &Counts);

Callers 6

AddMethod · 0.85
AddMethod · 0.85
AddMethod · 0.85
AddMethod · 0.85
AddOrderedMethod · 0.85
AddNonOrderedMethod · 0.85

Calls 12

AddTokenFunction · 0.85
ApplyFuncToLetterNGramsFunction · 0.85
PushTokenMethod · 0.80
GetPairMethod · 0.80
emplace_backMethod · 0.45
push_backMethod · 0.45
backMethod · 0.45
EmptyMethod · 0.45
pop_backMethod · 0.45
SizeMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected