| 147 | } |
| 148 | |
| 149 | static TIntrusivePtr<TBpeDictionary> BuildBpeLetter( |
| 150 | const TDictionaryBuilderOptions& dictionaryBuilderOptions, |
| 151 | const TDictionaryOptions& dictionaryOptions, |
| 152 | const TBpeDictionaryOptions& bpeOptions, |
| 153 | const TString& inputFilePath, |
| 154 | const TTokenizerOptions& tokenizerOptions, |
| 155 | bool useTokenizer, |
| 156 | bool verbose |
| 157 | ) { |
| 158 | if (verbose) { |
| 159 | Cerr << "Stage [1/2]: Dictionary building\n"; |
| 160 | } |
| 161 | |
| 162 | NFH::TFlatHashMap<TString, ui64> tokenCounts; |
| 163 | TDictionaryBuilder dictionaryBuilder(dictionaryBuilderOptions, dictionaryOptions); |
| 164 | |
| 165 | ApplyFuncTotokenizedText( |
| 166 | inputFilePath, |
| 167 | tokenizerOptions, |
| 168 | useTokenizer, |
| 169 | verbose, |
| 170 | [&](const TVector<TString>& tokens) { |
| 171 | dictionaryBuilder.Add(tokens, /*weight*/1); |
| 172 | for (const auto& token : tokens) { |
| 173 | ++tokenCounts[token]; |
| 174 | } |
| 175 | } |
| 176 | ); |
| 177 | |
| 178 | auto dictionary = dictionaryBuilder.FinishBuilding(); |
| 179 | |
| 180 | if (verbose) { |
| 181 | Cerr << "Stage [2/2]: Bpe building\n"; |
| 182 | } |
| 183 | |
| 184 | TBpeDictionaryBuilder bpeBuilder( |
| 185 | bpeOptions.NumUnits, |
| 186 | bpeOptions.SkipUnknown, |
| 187 | dictionary); |
| 188 | |
| 189 | for (const auto& [token, count] : tokenCounts) { |
| 190 | bpeBuilder.Add(TVector<TStringBuf>({token}), count); |
| 191 | } |
| 192 | |
| 193 | return bpeBuilder.FinishBuilding(); |
| 194 | } |
| 195 | |
| 196 | TIntrusivePtr<TBpeDictionary> NTextProcessing::NDictionary::BuildBpe( |
| 197 | const TString& inputFilePath, |
no test coverage detected