442 ng& operator=(
ng&&) noexcept = default;
453 static ng inst{auto_probe_tag{}};
462 const auto code = core::to_utf8(std::wstring{country_code});
463 for (std::size_t i = 0; i < culture_codes_.size(); ++i)
465 if (culture_codes_.at(i) == code)
return static_cast<culture>(i);
475 if (gender_string == L
"m" || gender_string == L
"male")
return gender::m;
476 if (gender_string == L
"f" || gender_string == L
"female")
return gender::f;
485 const auto idx =
static_cast<std::size_t
>(c);
486 return idx < culture_codes_.size() ? culture_codes_.at(idx) : std::string_view{};
494 const auto idx =
static_cast<std::size_t
>(c);
495 return idx < culture_names_.size() ? culture_names_.at(idx) : std::string_view{
"any"};
504 return get_name(request, _engine());
514 std::uint64_t call_seed)
const
516 return solver(
true, request.
gender, request.
culture, call_seed);
531 const core::batch_options& options)
552 const core::batch_options& options,
553 std::uint64_t batch_seed)
const
555 return core::run_batch(options, batch_seed, [&](std::uint64_t item_seed)
556 {
return get_name(request, item_seed); });
572 template <
typename Sink>
574 std::uint64_t batch_seed, Sink&& sink)
const
576 core::for_each_batch(options, batch_seed,
577 [&](std::uint64_t item_seed) {
return get_name(request, item_seed); },
578 std::forward<Sink>(sink));
587 template <
typename Sink>
591 for_each_name(request, options, _engine(), std::forward<Sink>(sink));
611 std::uint64_t call_seed)
const
613 return solver(
false, request.
gender, request.
culture, call_seed);
628 const core::batch_options& options)
649 const core::batch_options& options,
650 std::uint64_t batch_seed)
const
652 return core::run_batch(options, batch_seed, [&](std::uint64_t item_seed)
669 template <
typename Sink>
671 std::uint64_t batch_seed, Sink&& sink)
const
673 core::for_each_batch(options, batch_seed,
674 [&](std::uint64_t item_seed) {
return get_surname(request, item_seed); },
675 std::forward<Sink>(sink));
684 template <
typename Sink>
700 const auto it = _conventions.find(c);
730 const core::rng r{call_seed};
731 auto rg = r.split(
"gender");
733 auto rc = r.split(
"culture");
734 const culture c = resolve_composed_culture_(request.
culture, g == gender::m, g == gender::f, rc);
735 return make_name_(draw_person_(g, c, r.split(
"person")), c, call_seed);
744 const core::batch_options& options)
756 const core::batch_options& options,
757 std::uint64_t batch_seed)
const
759 return core::run_batch(options, batch_seed, [&](std::uint64_t item_seed)
769 template <
typename Sink>
771 std::uint64_t batch_seed, Sink&& sink)
const
773 core::for_each_batch(options, batch_seed,
774 [&](std::uint64_t item_seed) {
return get_full_name(request, item_seed); },
775 std::forward<Sink>(sink));
784 template <
typename Sink>
829 std::uint64_t call_seed)
const
831 const culture c = father._culture;
832 if (c == culture::any)
throw std::invalid_argument(
"get_child_name: the father has no culture");
833 const core::rng r{call_seed};
834 auto rg = r.split(
"gender");
837 const person dad = person_of_(father);
838 const person mum = person_of_(mother);
840 person child{.g = g, .given = pick_forename_(g, c, r.split(
"given")), .patronymic = {}, .family = {}};
841 if (cv.patronymic != patronymic_style::none)
843 child.patronymic = patronymic_of_(cv.patronymic, dad.given, g == gender::f);
845 child.family = child_family_(dad, mum, g, c, cv);
846 return make_name_(child, c, call_seed);
861 const culture c = husband._culture;
862 person p = person_of_(wife);
863 const person other = person_of_(husband);
865 if (c != culture::any && cv.family_name && cv.surnames == 1 && !other.family.empty())
867 p.family = {feminize_(other.family.front(), c,
true)};
869 return make_name_(p, wife._culture, wife.
seed());
887 if (c == culture::any)
throw std::invalid_argument(
"compose_name: the culture must not be any");
888 if (parts.
gender != gender::m && parts.
gender != gender::f)
890 throw std::invalid_argument(
"compose_name: the gender must be male or female");
892 if (parts.
given.empty())
throw std::invalid_argument(
"compose_name: the given name is empty");
895 return make_name_(p, c, call_seed);
929 std::uint64_t call_seed)
const
931 const culture c = child._culture;
932 if (c == culture::any)
throw std::invalid_argument(
"get_parent_name: the child has no culture");
933 if (parent_gender != gender::m && parent_gender != gender::f)
935 throw std::invalid_argument(
"get_parent_name: the parent's gender must be male or female");
937 const core::rng r{call_seed};
939 const person kid = person_of_(child);
940 const bool father = parent_gender == gender::m;
941 person p = draw_person_(parent_gender, c, r.split(
"person"));
943 if (father && cv.patronymic != patronymic_style::none && !kid.patronymic.empty())
945 p.given = father_given_(cv.patronymic, kid, c, p.given, r.split(
"given"));
947 if (cv.family_name && !kid.family.empty())
949 if (cv.surnames == 2 && kid.family.size() == 2)
953 const bool paternal = cv.inheritance == surname_order::paternal_first;
954 const auto& inherited = kid.family.at(father == paternal ? 0 : 1);
955 if (p.family.size() < 2) p.family.push_back(pick_surname_(parent_gender, c, r.split(
"family2")));
956 const std::size_t kept = paternal ? 0 : 1;
959 if (p.family.at(1 - kept) == inherited)
961 const auto rf = r.split(
"family3");
962 for (std::size_t attempt = 0; attempt < 8 && p.family.at(1 - kept) == inherited; ++attempt)
964 p.family.at(1 - kept) = pick_surname_(parent_gender, c, rf.split(attempt));
967 p.family.at(kept) = inherited;
971 p.family = {masculine_(kid.family.back(), c)};
973 else if (cv.surnames == 2)
978 return make_name_(p, c, call_seed);
992 return patronymic_of_(style, father, daughter);
1003 bool married =
false)
const
1005 return feminize_(surname, c, married);
1012 return _convention_table.layers();
1019 return _form_table.layers();
1026 return !_m_pool.empty() || !_f_pool.empty() || !_sur_pool.empty();
1040 void load(
const std::filesystem::path& resource_path)
1043 if (!std::filesystem::is_directory(resource_path, ec))
return;
1044 apply_(read_directory_(resource_path), core::layer_mode::replace,
true);
1058 [[nodiscard]]
bool load(dataset tier)
1060 const std::string tier_dir{core::tier_name(tier)};
1062 for (
const auto& file : embedded_files_())
1064 if (!file.path.starts_with(tier_dir +
"/"))
continue;
1065 const std::string source =
"embedded:" + std::string{file.path};
1066 if (file.path.ends_with(
".names"))
1068 bundle.names.push_back(parse_names_(file.text(), source));
1070 else if (file.path == tier_dir +
"/" + std::string{conventions_file_})
1072 bundle.conventions = core::tsv_document::parse(file.text(), source);
1074 else if (file.path == tier_dir +
"/" + std::string{forms_file_})
1076 bundle.forms = core::tsv_document::parse(file.text(), source);
1079 if (bundle.names.empty())
1081 const auto root = core::find_resources(library_name_, tier_dir +
"/" + std::string{marker_file_});
1082 if (!root)
return false;
1083 bundle = read_directory_(*root / tier_dir);
1085 if (bundle.names.empty())
return false;
1086 apply_(std::move(bundle), core::layer_mode::replace,
true);
1102 void extend(
const std::filesystem::path& path,
1103 core::layer_mode mode = core::layer_mode::override)
1106 if (std::filesystem::is_directory(path, ec))
1108 apply_(read_directory_(path), mode,
false);
1111 if (path.filename() == conventions_file_ || path.filename() == forms_file_)
1114 auto doc = core::tsv_document::parse(read_text_(path), path.string());
1115 (path.filename() == conventions_file_ ? bundle.conventions : bundle.forms) = std::move(doc);
1116 apply_(std::move(bundle), mode,
false);
1119 if (path.extension() !=
".names")
1121 throw std::invalid_argument(
"extend: expected a .names, conventions.tsv or "
1122 "surname_forms.tsv file, got " +
1123 path.filename().string());
1126 bundle.names.push_back(read_file_(path));
1127 apply_(std::move(bundle), mode,
false);
1142 std::string_view text, std::string source,
1143 core::layer_mode mode = core::layer_mode::override)
1146 bundle.conventions = core::tsv_document::parse(text, std::move(source));
1147 apply_(std::move(bundle), mode,
false);
1148 return _convention_table.layers().back();
1162 std::string_view text, std::string source,
1163 core::layer_mode mode = core::layer_mode::override)
1166 bundle.forms = core::tsv_document::parse(text, std::move(source));
1167 apply_(std::move(bundle), mode,
false);
1168 return _form_table.layers().back();
1182 core::layer_mode mode = core::layer_mode::override)
1185 bundle.names.push_back(parse_names_(std::string{text}, std::move(source)));
1186 const auto list = bundle.names.front().list;
1187 const auto c = bundle.names.front().c;
1188 apply_(std::move(bundle), mode,
false);
1189 return _tables.at(list).at(c).layers().back();
1200 return _tables.at(list_of_(g)).at(c).layers();
1212 const auto list = g == gender::m ? male_surname_list
1213 : g == gender::f ? female_surname_list
1215 return _tables.at(list).at(c).layers();
1221 [[nodiscard]] std::uint64_t
data_hash()
const {
return _data_hash; }
1224 static constexpr std::string_view library_name_ =
"name-generator";
1225 static constexpr std::string_view marker_file_ =
"american/male.names";
1226 static constexpr std::string_view conventions_file_ =
"conventions.tsv";
1227 static constexpr std::string_view forms_file_ =
"surname_forms.tsv";
1230 static constexpr std::array<std::string_view, 105> culture_codes_{
1231 "af",
"al",
"dz",
"us",
"ao",
"ar",
"at",
"az",
"bh",
"bd",
"be",
"bo",
"bw",
"br",
"gb",
1232 "bn",
"bg",
"bf",
"bi",
"kh",
"cm",
"ca",
"cl",
"cn",
"co",
"cr",
"hr",
"cy",
"cz",
"dk",
1233 "dj",
"nl",
"ec",
"eg",
"ae",
"ee",
"et",
"fj",
"ph",
"fi",
"fr",
"ge",
"de",
"gh",
"gr",
1234 "gt",
"ht",
"hn",
"hk",
"hu",
"is",
"in",
"id",
"ir",
"iq",
"ie",
"il",
"it",
"jm",
"jp",
1235 "jo",
"kz",
"kr",
"kw",
"lb",
"ly",
"lt",
"lu",
"mo",
"my",
"mv",
"mt",
"mu",
"mx",
"md",
1236 "ma",
"na",
"ng",
"no",
"om",
"ps",
"pa",
"pe",
"pl",
"pt",
"pr",
"qa",
"ru",
"sv",
"sa",
1237 "rs",
"sg",
"si",
"za",
"es",
"sd",
"se",
"ch",
"sy",
"tw",
"tn",
"tr",
"tm",
"uy",
"ye"};
1239 static constexpr std::array<std::string_view, 105> culture_names_{
1240 "afghan",
"albanian",
"algerian",
"american",
"angolan",
1241 "argentinian",
"austrian",
"azerbaijani",
"bahraini",
"bangladeshi",
1242 "belgian",
"bolivian",
"botswanan",
"brazilian",
"british",
1243 "bruneian",
"bulgarian",
"burkinabe",
"burundian",
"cambodian",
1244 "cameroonian",
"canadian",
"chilean",
"chinese",
"colombian",
1245 "costarican",
"croatian",
"cypriot",
"czech",
"danish",
1246 "djiboutian",
"dutch",
"ecuadorian",
"egyptian",
"emirati",
1247 "estonian",
"ethiopian",
"fijian",
"filipino",
"finnish",
1248 "french",
"georgian",
"german",
"ghanaian",
"greek",
1249 "guatemalan",
"haitian",
"honduran",
"hongkonger",
"hungarian",
1250 "icelandic",
"indian",
"indonesian",
"iranian",
"iraqi",
1251 "irish",
"israeli",
"italian",
"jamaican",
"japanese",
1252 "jordanian",
"kazakh",
"korean",
"kuwaiti",
"lebanese",
1253 "libyan",
"lithuanian",
"luxembourgish",
"macanese",
"malaysian",
1254 "maldivian",
"maltese",
"mauritian",
"mexican",
"moldovan",
1255 "moroccan",
"namibian",
"nigerian",
"norwegian",
"omani",
1256 "palestinian",
"panamanian",
"peruvian",
"polish",
"portuguese",
1257 "puertorican",
"qatari",
"russian",
"salvadoran",
"saudi",
1258 "serbian",
"singaporean",
"slovenian",
"southafrican",
"spanish",
1259 "sudanese",
"swedish",
"swiss",
"syrian",
"taiwanese",
1260 "tunisian",
"turkish",
"turkmen",
"uruguayan",
"yemeni"};
1262 static_assert(culture_codes_.size() ==
static_cast<std::size_t
>(culture::any));
1263 static_assert(culture_names_.size() ==
static_cast<std::size_t
>(culture::any));
1268 using list_kind = std::size_t;
1269 static constexpr list_kind male_list = 0;
1270 static constexpr list_kind female_list = 1;
1271 static constexpr list_kind surname_list = 2;
1272 static constexpr list_kind male_surname_list = 3;
1273 static constexpr list_kind female_surname_list = 4;
1274 static constexpr std::size_t list_count = 5;
1282 std::vector<std::uint32_t> ends;
1283 core::discrete dist{1.0};
1285 [[nodiscard]]
bool empty() const noexcept {
return ends.empty(); }
1288 [[nodiscard]] std::vector<std::wstring> names()
const
1290 std::vector<std::wstring> out;
1291 out.reserve(ends.size());
1292 for (std::size_t i = 0; i < ends.size(); ++i) out.push_back(at(i));
1296 [[nodiscard]]
bool contains(std::wstring_view name)
const
1298 std::size_t first = 0;
1299 for (
const std::uint32_t end : ends)
1301 if (std::wstring_view{text}.substr(first, end - first) == name)
return true;
1308 [[nodiscard]] std::wstring at(std::size_t i)
const
1310 const std::size_t end = ends.at(i);
1311 const std::size_t first = i == 0 ? 0 : ends[i - 1];
1312 return text.substr(first, end - first);
1317 [[nodiscard]]
static std::uint64_t hash_of_(
1318 const std::array<std::map<culture, core::layered_table>, list_count>& tables)
1320 std::uint64_t h = 0;
1321 for (std::size_t list = 0; list < tables.size(); ++list)
1323 for (
const auto& [c, table] : tables.at(list))
1325 h = core::hash_combine(h, (list << 8U) |
static_cast<std::uint64_t
>(c));
1326 h = core::hash_combine(h, table.data_hash());
1337 core::tsv_document doc;
1343 std::vector<names_file> names;
1344 std::optional<core::tsv_document> conventions;
1345 std::optional<core::tsv_document> forms;
1352 std::wstring female;
1353 std::wstring married;
1357 std::array<std::map<culture, core::layered_table>, list_count> _tables;
1358 std::uint64_t _data_hash{0};
1359 std::map<culture, name_pool> _m_pool;
1360 std::map<culture, name_pool> _f_pool;
1361 std::map<culture, name_pool> _sur_pool;
1362 std::map<culture, name_pool> _m_sur_pool;
1363 std::map<culture, name_pool> _f_sur_pool;
1364 core::layered_table _convention_table{convention_schema_()};
1365 core::layered_table _form_table{form_schema_()};
1366 std::map<culture, naming_convention> _conventions;
1367 std::map<culture, std::vector<form_rule>> _forms;
1368 std::uint64_t _convention_hash{0};
1371 core::rng _engine{core::rng::from_entropy()};
1374 struct auto_probe_tag {};
1377 explicit ng(auto_probe_tag )
1379 if (!load(dataset::lite)) (void)load(dataset::full);
1382 static std::span<const core::embedded_file> embedded_files_()
1384#ifdef DASMIG_NAME_EMBED
1385 return embedded::namegen::files;
1391 [[nodiscard]]
static constexpr list_kind list_of_(gender g)
1395 case gender::m:
return male_list;
1396 case gender::f:
return female_list;
1397 case gender::any:
return surname_list;
1399 return surname_list;
1402 [[nodiscard]]
const std::map<culture, name_pool>& pools_(list_kind list)
const
1404 if (list == male_list)
return _m_pool;
1405 if (list == female_list)
return _f_pool;
1406 if (list == male_surname_list)
return _m_sur_pool;
1407 if (list == female_surname_list)
return _f_sur_pool;
1413 static core::table_schema schema_()
1415 return {.key = {
"name"}, .required = {}, .optional = {
"weight"}};
1419 static double weight_of_(
const core::tsv_row& row)
1421 const auto w = row.get_or<
double>(
"weight", 1.0);
1422 if (!(w > 0.0))
throw row.error(
"weight",
"must be positive");
1429 static names_file parse_names_(std::string text, std::string source)
1431 if (text.starts_with(
"\xEF\xBB\xBF")) text.erase(0, 3);
1434 auto header_line = [&](std::size_t from, std::size_t line) {
1435 auto end = text.find(
'\n', from);
1436 if (end == std::string::npos)
1438 if (line == 1 || from >= text.size())
1440 throw core::tsv_error{source, line,
"",
"expected a country code and a gender line"};
1444 auto value = std::string_view{text}.substr(from, end - from);
1445 if (value.ends_with(
'\r')) value.remove_suffix(1);
1446 return std::pair{std::string{value}, std::min(end + 1, text.size())};
1448 const auto [code, after_code] = header_line(0, 1);
1449 const auto [gender_text, after_gender] = header_line(after_code, 2);
1452 const auto c = to_culture(core::from_utf8(code));
1453 if (c == culture::any)
1455 throw core::tsv_error{source, 1,
"",
"unknown country code '" + code +
"'"};
1457 list_kind list = surname_list;
1458 if (gender_text ==
"male" || gender_text ==
"m")
1462 else if (gender_text ==
"female" || gender_text ==
"f")
1466 else if (gender_text ==
"male-surname")
1468 list = male_surname_list;
1470 else if (gender_text ==
"female-surname")
1472 list = female_surname_list;
1474 else if (gender_text !=
"any")
1476 throw core::tsv_error{source, 2,
"",
1477 "expected male, female, any, male-surname or female-surname, got '" +
1481 text.insert(after_code,
"#");
1482 text.insert(0,
"#");
1483 return {.c = c, .list = list,
1484 .doc = core::tsv_document::parse(text, std::move(source), {
"name",
"weight",
"disabled"})};
1487 static names_file read_file_(
const std::filesystem::path& file)
1489 return parse_names_(read_text_(file), file.string());
1492 static std::string read_text_(
const std::filesystem::path& file)
1494 std::ifstream in{file, std::ios::binary};
1495 if (!in)
throw core::tsv_error{file.string(), 0,
"",
"cannot open file"};
1496 in.seekg(0, std::ios::end);
1497 const auto size = in.tellg();
1498 std::string text(size > 0 ?
static_cast<std::size_t
>(size) : 0,
'\0');
1499 in.seekg(0, std::ios::beg);
1500 in.read(text.data(),
static_cast<std::streamsize
>(text.size()));
1501 text.resize(
static_cast<std::size_t
>(in.gcount()));
1505 static data_bundle read_directory_(
const std::filesystem::path& dir)
1507 std::vector<std::filesystem::path> paths;
1508 for (
const auto& entry : std::filesystem::recursive_directory_iterator(dir))
1510 if (entry.is_regular_file() && entry.path().extension() ==
".names")
1512 paths.push_back(entry.path());
1515 std::ranges::sort(paths);
1517 bundle.names.reserve(paths.size());
1518 for (
const auto& path : paths) bundle.names.push_back(read_file_(path));
1520 if (
const auto file = dir / conventions_file_; std::filesystem::is_regular_file(file, ec))
1522 bundle.conventions = core::tsv_document::parse(read_text_(file), file.string());
1524 if (
const auto file = dir / forms_file_; std::filesystem::is_regular_file(file, ec))
1526 bundle.forms = core::tsv_document::parse(read_text_(file), file.string());
1532 void rebuild_conventions_()
1534 _conventions.clear();
1535 for (
const auto& row : _convention_table.rows())
1537 _conventions.emplace(culture_from_name_(row[
"culture"]), convention_of_(row));
1540 for (
const auto& row : _form_table.rows())
1542 _forms[culture_from_name_(row[
"culture"])].push_back(form_of_(row));
1544 _convention_hash = core::hash_combine(_convention_table.data_hash(), _form_table.data_hash());
1548 [[nodiscard]] std::pair<core::layered_table, core::layered_table> extended_convention_tables_(
1549 data_bundle& bundle, core::layer_mode mode,
bool reset)
const
1551 auto conventions = reset ? core::layered_table{convention_schema_()} : _convention_table;
1552 auto forms = reset ? core::layered_table{form_schema_()} : _form_table;
1553 const auto layer_mode = reset ? core::layer_mode::replace : mode;
1554 if (bundle.conventions)
1556 (void)conventions.extend(std::move(*bundle.conventions), layer_mode,
1557 [](
const core::tsv_row& row) { (void)convention_of_(row); });
1561 (void)forms.extend(std::move(*bundle.forms), layer_mode,
1562 [](
const core::tsv_row& row) { (void)form_of_(row); });
1564 return {std::move(conventions), std::move(forms)};
1569 void apply_(data_bundle bundle, core::layer_mode mode,
bool reset)
1572 auto [next_conventions, next_forms] = extended_convention_tables_(bundle, mode, reset);
1574 auto& files = bundle.names;
1575 auto next = reset ?
decltype(_tables){} : _tables;
1578 std::array<std::map<culture, bool>, list_count> started;
1579 std::array<std::map<culture, bool>, list_count> touched;
1580 for (
auto& file : files)
1582 auto& tables = next.at(file.list);
1583 auto it = tables.try_emplace(file.c, schema_()).first;
1584 auto list_mode = mode;
1587 list_mode = started.at(file.list)[file.c] ? core::layer_mode::override
1588 : core::layer_mode::replace;
1590 started.at(file.list)[file.c] =
true;
1591 touched.at(file.list)[file.c] =
true;
1592 (void)it->second.extend(std::move(file.doc), list_mode,
1593 [](
const core::tsv_row& row) { (void)weight_of_(row); });
1597 std::array<std::map<culture, name_pool>, list_count> pools;
1598 for (std::size_t list = 0; list < pools.size(); ++list)
1600 if (!reset) pools.at(list) = pools_(list);
1601 for (
const auto& [c, changed] : touched.at(list))
1603 const auto& table = next.at(list).at(c);
1604 pools.at(list).erase(c);
1605 if (table.empty())
continue;
1607 std::vector<double> weights;
1608 weights.reserve(table.size());
1609 pool.ends.reserve(table.size());
1611 std::size_t bytes = 0;
1612 for (
const auto& row : table.rows()) bytes += row[
"name"].size();
1613 if (bytes >= std::numeric_limits<std::uint32_t>::max())
1615 throw std::length_error(
"name-generator: too many names in one list");
1617 pool.text.reserve(bytes);
1618 for (
const auto& row : table.rows())
1620 pool.text += core::from_utf8(row[
"name"]);
1621 pool.ends.push_back(
static_cast<std::uint32_t
>(pool.text.size()));
1622 weights.push_back(weight_of_(row));
1624 if (pool.text.capacity() - pool.text.size() > pool.text.size() / 8)
1626 pool.text.shrink_to_fit();
1628 pool.dist = core::discrete{weights};
1629 pools.at(list).emplace(c, std::move(pool));
1633 _convention_table = std::move(next_conventions);
1634 _form_table = std::move(next_forms);
1635 rebuild_conventions_();
1637 _tables = std::move(next);
1638 _data_hash = hash_of_(_tables);
1639 _m_pool = std::move(pools.at(male_list));
1640 _f_pool = std::move(pools.at(female_list));
1641 _sur_pool = std::move(pools.at(surname_list));
1642 _m_sur_pool = std::move(pools.at(male_surname_list));
1643 _f_sur_pool = std::move(pools.at(female_surname_list));
1649 static culture resolve_culture(culture c,
const std::map<culture, name_pool>& db,
1652 if (c != culture::any)
return c;
1655 throw std::invalid_argument(
"No names loaded for any culture");
1657 auto it = db.begin();
1658 std::advance(it,
static_cast<std::ptrdiff_t
>(core::bounded(engine, db.size())));
1663 static gender resolve_gender(gender g, core::rng& engine)
1665 if (g != gender::any)
return g;
1666 return core::bernoulli(engine, 0.5) ? gender::f : gender::m;
1670 [[nodiscard]]
static const char* gender_label(gender g)
1674 case gender::m:
return "male";
1675 case gender::f:
return "female";
1676 case gender::any:
return "any";
1682 [[nodiscard]]
static std::wstring pick(
const std::map<culture, name_pool>& db,
1683 culture c, gender g, core::rng& engine)
1685 auto it = db.find(c);
1686 if (it == db.end() || it->second.empty())
1688 throw std::invalid_argument(
1689 std::string(
"No ") + gender_label(g) +
" names loaded for culture '" +
1690 std::string{culture_name(c)} +
"'");
1692 const auto& pool = it->second;
1693 return pool.at(pool.dist(engine));
1697 [[nodiscard]] std::tuple<std::wstring, gender, culture> draw_forename_(
1698 gender g, culture c,
const core::rng& r)
const
1700 auto rg = r.split(
"gender");
1701 const gender resolved_g = resolve_gender(g, rg);
1702 const auto& db = resolved_g == gender::f ? _f_pool : _m_pool;
1703 auto rc = r.split(
"culture");
1704 const culture resolved_c = resolve_culture(c, db, rc);
1705 auto rn = r.split(
"name");
1706 return {pick(db, resolved_c, resolved_g, rn), resolved_g, resolved_c};
1712 [[nodiscard]] std::pair<std::wstring, culture> draw_surname_(gender g, culture c,
1713 const core::rng& r)
const
1715 auto rc = r.split(
"culture");
1716 const culture resolved_c = resolve_culture(c, _sur_pool, rc);
1717 auto rn = r.split(
"name");
1718 const auto& gendered = g == gender::f ? _f_sur_pool : _m_sur_pool;
1719 const auto& db = gendered.contains(resolved_c) ? gendered : _sur_pool;
1720 return {pick(db, resolved_c, gender::any, rn), resolved_c};
1725 static core::table_schema convention_schema_()
1727 return {.key = {
"culture"},
1729 .optional = {
"order",
"patronymic",
"family_name",
"surnames",
"second_surname",
1733 static core::table_schema form_schema_()
1735 return {.key = {
"culture",
"male"}, .required = {
"female"}, .optional = {
"married"}};
1738 [[nodiscard]]
static culture culture_from_name_(std::string_view name)
1740 for (std::size_t i = 0; i < culture_names_.size(); ++i)
1742 if (culture_names_.at(i) == name)
return static_cast<culture>(i);
1744 return culture::any;
1747 static void check_culture_(
const core::tsv_row& row)
1749 if (culture_from_name_(row[
"culture"]) == culture::any)
1751 throw row.error(
"culture",
"unknown culture '" + std::string{row[
"culture"]} +
"'");
1756 static naming_convention convention_of_(
const core::tsv_row& row)
1758 check_culture_(row);
1759 naming_convention cv;
1760 const auto order = row.get_or<std::string>(
"order",
"given_family");
1761 if (order ==
"family_given")
1763 cv.order = name_order::family_given;
1765 else if (order !=
"given_family")
1767 throw row.error(
"order",
"expected given_family or family_given, got '" + order +
"'");
1769 const auto patronymic = row.get_or<std::string>(
"patronymic",
"none");
1770 if (patronymic ==
"east_slavic")
1772 cv.patronymic = patronymic_style::east_slavic;
1774 else if (patronymic ==
"icelandic")
1776 cv.patronymic = patronymic_style::icelandic;
1778 else if (patronymic !=
"none")
1780 throw row.error(
"patronymic",
1781 "expected none, east_slavic or icelandic, got '" + patronymic +
"'");
1783 cv.family_name = row.get_or<
bool>(
"family_name",
true);
1784 cv.surnames = row.get_or<
int>(
"surnames", 1);
1785 if (cv.surnames < 1 || cv.surnames > 2)
throw row.error(
"surnames",
"must be 1 or 2");
1786 cv.second_surname = row.get_or<
double>(
"second_surname", 1.0);
1787 if (cv.second_surname < 0.0 || cv.second_surname > 1.0)
1789 throw row.error(
"second_surname",
"must be between 0 and 1");
1791 const auto inheritance = row.get_or<std::string>(
"surname_order",
"paternal_first");
1792 if (inheritance ==
"maternal_first")
1794 cv.inheritance = surname_order::maternal_first;
1796 else if (inheritance !=
"paternal_first")
1798 throw row.error(
"surname_order",
1799 "expected paternal_first or maternal_first, got '" + inheritance +
"'");
1805 static form_rule form_of_(
const core::tsv_row& row)
1807 check_culture_(row);
1808 if (!row.has(
"male"))
throw row.error(
"male",
"missing value (use * for any ending)");
1809 if (!row.has(
"female"))
throw row.error(
"female",
"missing value");
1811 rule.male = row[
"male"] ==
"*" ? std::wstring{} : core::from_utf8(row[
"male"]);
1812 rule.female = core::from_utf8(row[
"female"]);
1813 rule.married = row.has(
"married") ? core::from_utf8(row[
"married"]) : rule.female;
1817 [[nodiscard]] std::uint64_t composed_hash_()
const
1819 return core::hash_combine(_data_hash, _convention_hash);
1824 [[nodiscard]]
culture resolve_composed_culture_(culture c,
bool need_male,
bool need_female,
1825 core::rng& engine)
const
1827 if (c != culture::any)
return c;
1828 std::vector<culture> candidates;
1829 for (
const auto& [culture_id, pool] : _sur_pool)
1831 if (pool.empty())
continue;
1832 if (need_male && !_m_pool.contains(culture_id))
continue;
1833 if (need_female && !_f_pool.contains(culture_id))
continue;
1834 candidates.push_back(culture_id);
1836 if (candidates.empty())
throw std::invalid_argument(
"No names loaded for any culture");
1837 return candidates.at(core::bounded(engine, candidates.size()));
1840 [[nodiscard]] std::wstring pick_forename_(gender g, culture c, core::rng r)
const
1842 return pick(g == gender::f ? _f_pool : _m_pool, c, g, r);
1846 [[nodiscard]] std::wstring pick_surname_(gender g, culture c, core::rng r)
const
1848 const auto& gendered = g == gender::f ? _f_sur_pool : _m_sur_pool;
1849 return pick(gendered.contains(c) ? gendered : _sur_pool, c,
gender::any, r);
1857 std::wstring patronymic;
1858 std::vector<std::wstring> family;
1861 [[nodiscard]] person draw_person_(gender g, culture c,
const core::rng& r)
const
1863 const auto cv = convention(c);
1864 person p{.g = g, .given = pick_forename_(g, c, r.split(
"given")), .patronymic = {}, .family = {}};
1865 if (cv.patronymic != patronymic_style::none)
1867 const auto father = pick_forename_(gender::m, c, r.split(
"father"));
1868 p.patronymic = patronymic_of_(cv.patronymic, father, g == gender::f);
1872 p.family.push_back(pick_surname_(g, c, r.split(
"family")));
1873 auto rs = r.split(
"second");
1874 if (cv.surnames == 2 && core::bernoulli(rs, cv.second_surname))
1877 const auto rf = r.split(
"family2");
1878 std::wstring second;
1879 for (std::size_t attempt = 0; attempt < 8; ++attempt)
1881 second = pick_surname_(g, c, rf.split(attempt));
1882 if (second != p.family.front())
break;
1884 p.family.push_back(std::move(second));
1891 [[nodiscard]]
static person person_of_(
const name& n)
1893 person p{.g = n._gender, .given = n.given(), .patronymic = n.patronymic(), .family = {}};
1894 for (std::size_t i = 0; i < n._parts.size(); ++i)
1896 if (n._roles.at(i) == name_role::family || n._roles.at(i) == name_role::family2)
1898 p.family.push_back(n._parts.at(i));
1905 [[nodiscard]] name make_name_(
const person& p, culture c, std::uint64_t seed)
const
1907 const auto cv = convention(c);
1908 using part = std::pair<name_role, const std::wstring*>;
1909 std::vector<part> given_parts;
1910 given_parts.emplace_back(name_role::given, &p.given);
1911 if (!p.patronymic.empty()) given_parts.emplace_back(name_role::patronymic, &p.patronymic);
1912 std::vector<part> family_parts;
1913 family_parts.reserve(p.family.size());
1914 for (std::size_t i = 0; i < p.family.size(); ++i)
1916 family_parts.emplace_back(i == 0 ? name_role::family :
name_role::
family2, &p.family.at(i));
1918 const bool family_first = cv.order == name_order::family_given;
1919 std::vector<part> sequence = family_first ? family_parts : given_parts;
1920 const auto& rest = family_first ? given_parts : family_parts;
1921 sequence.insert(sequence.end(), rest.begin(), rest.end());
1923 name result{*sequence.front().second, p.g, c,
this, sequence.front().first};
1924 for (std::size_t i = 1; i < sequence.size(); ++i)
1926 result.append_part(*sequence.at(i).second, sequence.at(i).first);
1928 result._composed =
true;
1929 result._signature = {.seed = seed, .data = composed_hash_()};
1934 [[nodiscard]] std::vector<std::wstring> child_family_(
const person& father,
const person& mother,
1935 gender g, culture c,
1936 const naming_convention& cv)
const
1938 if (!cv.family_name || father.family.empty())
return {};
1939 if (cv.surnames == 2 && (father.family.size() == 2 || mother.family.size() == 2))
1941 const bool maternal_first = cv.inheritance == surname_order::maternal_first;
1942 auto first =
maternal_first ? mother.family.back() : father.family.front();
1943 auto second =
maternal_first ? father.family.back() : mother.family.front();
1944 if (first == second)
1948 const auto& of_second =
maternal_first ? father.family : mother.family;
1949 const auto& of_first =
maternal_first ? mother.family : father.family;
1950 const auto different = std::ranges::find_if(of_second, [&](
const std::wstring& w) {
return w != first; });
1951 if (different != of_second.end())
1953 second = *different;
1955 else if (
const auto alt = std::ranges::find_if(of_first, [&](
const std::wstring& w) {
return w != second; });
1956 alt != of_first.end())
1961 return {first, second};
1963 return {g == gender::f ? feminize_(father.family.back(), c,
false) : father.family.back()};
1968 [[nodiscard]] std::wstring feminize_(
const std::wstring& surname, culture c,
bool married)
const
1970 const auto it = _forms.find(c);
1971 if (it == _forms.end())
return surname;
1972 const form_rule* best =
nullptr;
1973 for (
const auto& rule : it->second)
1975 if (rule.male.size() > surname.size())
continue;
1976 if (!std::wstring_view{surname}.ends_with(rule.male))
continue;
1977 if (best ==
nullptr || rule.male.size() > best->male.size()) best = &rule;
1979 if (best ==
nullptr)
return surname;
1980 return surname.substr(0, surname.size() - best->male.size()) +
1981 (married ? best->married : best->female);
1987 [[nodiscard]] std::wstring masculine_(
const std::wstring& surname, culture c)
const
1989 const auto it = _forms.find(c);
1990 if (it == _forms.end())
return surname;
1991 std::wstring best = surname;
1992 std::size_t best_size = 0;
1993 for (
const auto& rule : it->second)
1995 if (rule.female.size() < best_size || rule.female.size() > surname.size())
continue;
1996 if (!std::wstring_view{surname}.ends_with(rule.female))
continue;
1997 auto candidate = surname.substr(0, surname.size() - rule.female.size()) + rule.male;
1998 if (feminize_(candidate, c,
false) != surname)
continue;
1999 best = std::move(candidate);
2000 best_size = rule.female.size();
2007 [[nodiscard]] std::wstring father_given_(patronymic_style style,
const person& kid, culture c,
2008 const std::wstring& fallback, core::rng r)
const
2010 const auto it = _m_pool.find(c);
2011 if (it == _m_pool.end())
return fallback;
2012 std::vector<std::wstring> fits;
2013 for (std::size_t i = 0; i < it->second.ends.size(); ++i)
2015 auto candidate = it->second.at(i);
2016 if (patronymic_of_(style, candidate, kid.g == gender::f) == kid.patronymic)
2018 fits.push_back(std::move(candidate));
2021 if (fits.empty())
return fallback;
2022 return fits.at(core::bounded(r, fits.size()));
2025 [[nodiscard]]
static bool ends_with_(
const std::wstring& text, std::wstring_view ending)
2027 return text.size() >= ending.size() &&
2028 std::wstring_view{text}.substr(text.size() - ending.size()) == ending;
2031 [[nodiscard]]
static std::wstring patronymic_of_(patronymic_style style,
const std::wstring& father,
2036 case patronymic_style::east_slavic:
return east_slavic_patronymic_(father, daughter);
2037 case patronymic_style::icelandic:
return icelandic_patronymic_(father, daughter);
2038 case patronymic_style::none:
break;
2044 [[nodiscard]]
static std::wstring east_slavic_patronymic_(
const std::wstring& father,
bool daughter)
2048 std::wstring_view name;
2049 std::wstring_view son;
2050 std::wstring_view daughter;
2052 static constexpr std::array<irregular, 12> exceptions{{
2053 {.name = L
"Pavel", .son = L
"Pavlovich", .daughter = L
"Pavlovna"},
2054 {.name = L
"Lev", .son = L
"Lvovich", .daughter = L
"Lvovna"},
2055 {.name = L
"Pyotr", .son = L
"Petrovich", .daughter = L
"Petrovna"},
2056 {.name = L
"Petr", .son = L
"Petrovich", .daughter = L
"Petrovna"},
2057 {.name = L
"Dmitry", .son = L
"Dmitriyevich", .daughter = L
"Dmitriyevna"},
2058 {.name = L
"Igor", .son = L
"Igorevich", .daughter = L
"Igorevna"},
2059 {.name = L
"Yakov", .son = L
"Yakovlevich", .daughter = L
"Yakovlevna"},
2060 {.name = L
"Ilya", .son = L
"Ilyich", .daughter = L
"Ilyinichna"},
2061 {.name = L
"Nikita", .son = L
"Nikitich", .daughter = L
"Nikitichna"},
2062 {.name = L
"Luka", .son = L
"Lukich", .daughter = L
"Lukinichna"},
2063 {.name = L
"Kuzma", .son = L
"Kuzmich", .daughter = L
"Kuzminichna"},
2064 {.name = L
"Foma", .son = L
"Fomich", .daughter = L
"Fominichna"},
2066 if (father.empty())
return {};
2067 for (
const auto& e : exceptions)
2069 if (father == e.name)
return std::wstring{daughter ? e.daughter : e.son};
2072 if (ends_with_(father, L
"iy") || ends_with_(father, L
"ey") || ends_with_(father, L
"ay") ||
2073 ends_with_(father, L
"oy") || ends_with_(father, L
"uy"))
2075 return stem + (daughter ? L
"evna" : L
"evich");
2077 if (ends_with_(father, L
"y"))
2080 return stem + (daughter ? L
"yevna" : L
"yevich");
2082 if (ends_with_(father, L
"a"))
2085 return stem + (daughter ? L
"ichna" : L
"ich");
2087 for (
const wchar_t vowel : std::wstring_view{L
"eiou"})
2089 if (father.back() == vowel)
return stem + (daughter ? L
"vna" : L
"vich");
2091 return stem + (daughter ? L
"ovna" : L
"ovich");
2096 [[nodiscard]]
static std::wstring icelandic_patronymic_(
const std::wstring& father,
bool daughter)
2099 if (ends_with_(stem, L
"ur"))
2101 stem.resize(stem.size() - 2);
2104 else if (ends_with_(stem, L
"i"))
2109 else if (!ends_with_(stem, L
"s"))
2113 return stem + (daughter ? L
"dóttir" : L
"son");
2117 [[nodiscard]] name solver(
bool is_name, gender requested_gender,
2118 culture requested_culture, std::uint64_t call_seed)
const
2120 const core::rng r{call_seed};
2121 name result = [&]() -> name {
2124 auto [text, g, c] = draw_forename_(requested_gender, requested_culture, r);
2125 return {std::move(text), g, c,
this};
2128 auto rg = r.split(
"gender");
2129 const gender g = resolve_gender(requested_gender, rg);
2130 auto [text, c] = draw_surname_(g, requested_culture, r);
2131 return {std::move(text), g, c,
this, name_role::family};
2133 result._signature = {.seed = call_seed, .data = data_hash()};
2139 void append_name_impl(name& n, gender g, culture c)
const
2141 const auto r = core::rng{n.seed()}.split(
"part").split(n._parts.size());
2142 n.append_part(std::get<0>(draw_forename_(g, c, r)), name_role::given);
2145 void append_surname_impl(name& n, culture c)
const
2147 const auto r = core::rng{n.seed()}.split(
"part").split(n._parts.size());
2148 n.append_part(draw_surname_(n._gender, c, r).first, name_role::family);
2152 friend struct ::ng_test_access;