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

Function ApplyImpl

library/cpp/text_processing/dictionary/bpe_dictionary.cpp:47–104  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45// TODO(annaveronika): more efficient apply.
46template <typename TStringVector, typename TUnitToTokenId>
47static void ApplyImpl(
48 TStringVector tokens,
49 TVector<TTokenId>* tokenIds,
50 const IDictionary* alphabet,
51 const TUnitToTokenId& unitToTokenId,
52 EUnknownTokenPolicy unknownTokenPolicy
53) {
54 tokenIds->clear();
55 alphabet->Apply(tokens, tokenIds, unknownTokenPolicy);
56
57 if (tokenIds->size() <= 1) {
58 return;
59 }
60
61 TUnitQueue minIds;
62 TEraseList<TTokenId> tokenIdsList(*tokenIds);
63 TPositionsMap positions(Max<TTokenId>(), tokenIds->size());
64
65 for (size_t i = 0; i + 1 < tokenIds->size(); ++i) {
66 auto unit = TUnit((*tokenIds)[i], (*tokenIds)[i + 1]);
67 auto maybeTokenId = unitToTokenId(unit);
68 if (!maybeTokenId) {
69 continue;
70 }
71 auto unitId = *maybeTokenId;
72 minIds.push({unitId, unit});
73 positions[unitId].push_back(i);
74 }
75
76 while (!minIds.empty()) {
77 auto bestIdWithUnit = minIds.top();
78 auto bestId = bestIdWithUnit.first;
79 auto bestUnit = bestIdWithUnit.second;
80
81 while (!minIds.empty() && minIds.top() == bestIdWithUnit) {
82 minIds.pop();
83 }
84
85 for (int firstPosition : positions[bestId]) {
86 if (
87 !tokenIdsList.IsValidElement(firstPosition) ||
88 tokenIdsList.IsLastElement(firstPosition) ||
89 tokenIdsList.GetPair(firstPosition) != bestUnit
90 ) {
91 continue;
92 }
93 tokenIdsList.Erase(tokenIdsList.GetNextPosition(firstPosition));
94 tokenIdsList.UpdateToken(firstPosition, bestId);
95 if (!tokenIdsList.IsFirstElement(firstPosition)) {
96 int prevPosition = tokenIdsList.GetPrevPosition(firstPosition);
97 AddPair(prevPosition, unitToTokenId, &tokenIdsList, &positions, &minIds);
98 }
99 AddPair(firstPosition, unitToTokenId, &tokenIdsList, &positions, &minIds);
100 }
101 }
102
103 (*tokenIds) = tokenIdsList.GetValidElements();
104}

Callers 5

ApplyMethod · 0.70
ApplyMethod · 0.70
ApplyMethod · 0.70
ApplyMethod · 0.70
ApplyMethod · 0.70

Calls 15

TUnitClass · 0.85
AddPairFunction · 0.85
IsValidElementMethod · 0.80
IsLastElementMethod · 0.80
GetPairMethod · 0.80
GetNextPositionMethod · 0.80
IsFirstElementMethod · 0.80
GetPrevPositionMethod · 0.80
GetValidElementsMethod · 0.80
clearMethod · 0.45
ApplyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected