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

Function BuildMapInternal

external/sentencepiece/src/builder.cc:327–379  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

325#ifdef ENABLE_NFKC_COMPILE
326namespace {
327util::Status BuildMapInternal(
328 Builder::CharsMap *chars_map,
329 std::function<Builder::Chars(const Builder::Chars &)> composer,
330 std::function<Builder::Chars(const Builder::Chars &)> decomposer) {
331#ifdef ENABLE_NFKC_COMPILE
332 // Set of fully NFKD decomposed characters.
333 std::set<Builder::Chars> nfkd_decomposed;
334
335 // Fully normalized one character to unnormalized one character map.
336 std::map<char32, std::set<char32>> norm2orig;
337
338 Builder::CharsMap nfkc_map; // The final NFKC mapping.
339
340 constexpr int kMaxUnicode = 0x10FFFF;
341 for (char32 cp = 1; cp <= kMaxUnicode; ++cp) {
342 if (!U_IS_UNICODE_CHAR(cp)) {
343 continue;
344 }
345 // Aggregates single character to fully NFKC normalized characters.
346 const auto nfkc = composer({cp});
347 if (nfkc.size() >= 2 || (nfkc.size() == 1 && nfkc[0] != cp)) {
348 nfkc_map[{cp}] = nfkc;
349 }
350 const auto nfkd = decomposer({cp});
351 if (nfkd.size() == 1) {
352 // Aggregates reverse mapping from normalized to unnormalized character.
353 norm2orig[nfkd[0]].insert(cp);
354 } else {
355 // One character is decomposed into multiple characters.
356 nfkd_decomposed.insert(nfkd);
357 }
358 }
359
360 for (const auto &nfkd : nfkd_decomposed) {
361 const auto nfkc = composer(nfkd);
362 // This case is already covered by single-character to NFKC mapping.
363 if (nfkc == nfkd) {
364 continue;
365 }
366 // Expand all possible sequences which are normalized into the same
367 // `nfkd`.
368 for (const auto &nfkd_orig : ExpandUnnormalized(nfkd, norm2orig)) {
369 if (nfkd_orig != nfkc) {
370 nfkc_map[nfkd_orig] = nfkc;
371 }
372 }
373 }
374
375 RETURN_IF_ERROR(Builder::RemoveRedundantMap(&nfkc_map));
376 *chars_map = std::move(nfkc_map);
377#endif // ENABLE_NFKC_COMPILE
378 return util::OkStatus();
379}
380} // namespace
381#endif // ENABLE_NFKC_COMPILE
382

Callers 2

BuildNFKCMapMethod · 0.85
BuildNFCMapMethod · 0.85

Calls 3

ExpandUnnormalizedFunction · 0.85
OkStatusFunction · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected