-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameAssociator.cpp
More file actions
180 lines (145 loc) · 6.01 KB
/
NameAssociator.cpp
File metadata and controls
180 lines (145 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "NameAssociator.h"
#include <utility>
#include "CSVReader.h"
#include "ErrorHandler.h"
/******************************************************************************/
NameAssociator::NameAssociator(const std::filesystem::path & filepath, SyllableAssociator && syllable_reader, SymbolPairing && symbol_pairing):
m_syllable_reader(std::forward<SyllableAssociator>(syllable_reader)),
m_symbol_pairing(std::forward<SymbolPairing>(symbol_pairing))
{
CSVReader csv_reader(filepath);
auto it_line = csv_reader.cbegin();
// Getting data from the first line
ErrorHandler::assert(it_line->size() >= 3, "File format is wrong (initialization data)");
auto it = it_line->cbegin();
const int first_name_buffer_size = std::stoi(*it++);
const int last_name_buffer_size = std::stoi(*it++);
const int particle_buffer_size = std::stoi(*it++);
++it_line;
// Filling the name generator with the data from the CSV
for (; it_line != csv_reader.cend() ; ++it_line)
{
ErrorHandler::assert(it_line->size() >= (2+first_name_buffer_size+last_name_buffer_size+particle_buffer_size), "File format is wrong (content data)");
auto it = it_line->cbegin();
const RegionLabel region_label = *it++;
m_full_name_dict[region_label].format = *it++;
ConsonanceList first_names;
ConsonanceList last_names;
ConsonanceList particles;
for (size_t i = 0 ; i < first_name_buffer_size ; ++i)
{
if (*it != "")
first_names.push_back(*it);
++it;
}
for (size_t i = 0 ; i < last_name_buffer_size ; ++i)
{
if (*it != "")
last_names.push_back(*it);
++it;
}
for (size_t i = 0 ; i < particle_buffer_size ; ++i)
{
if (*it != "")
particles.push_back(*it);
++it;
}
m_full_name_dict[region_label].possibilities.first_names = first_names;
m_full_name_dict[region_label].possibilities.last_names = last_names;
m_full_name_dict[region_label].possibilities.particles = particles;
}
}
/******************************************************************************/
std::vector<NameAssociator::RegionLabel> NameAssociator::get_region_list() const
{
std::vector<RegionLabel> region_list;
for (const auto & item : m_full_name_dict)
region_list.push_back(item.first);
return region_list;
}
/******************************************************************************/
std::vector<NameAssociator::RegionLabel> NameAssociator::get_region_list(const std::filesystem::path & filepath)
{
std::vector<RegionLabel> region_list;
CSVReader csv_reader(filepath);
auto it_line = csv_reader.cbegin();
++it_line; // Discarding the first line
// Filling the name generator with the data from the CSV (taking only the first element of the line)
for (; it_line != csv_reader.cend() ; ++it_line)
region_list.push_back(*it_line->cbegin());
return region_list;
}
/******************************************************************************/
static void recase_name(std::string & name)
{
if (name == "")
return;
name[0] = toupper(name[0]);
for (size_t is = 1 ; is < name.size() ; ++is)
name[is] = tolower(name[is]);
}
/******************************************************************************/
std::string NameAssociator::generate_random_full_name(const RegionLabel & region) const
{
std::string full_name_result;
const FullNameFormat & format = m_full_name_dict.at(region).format;
for (size_t ic = 0 ; ic < format.size() ; ++ic)
{
char c = format[ic];
if (m_symbol_pairing.find(c) != m_symbol_pairing.end())
{
NameEntity name_entity = m_symbol_pairing.at(c);
std::vector<std::string> consonance_list;
switch (name_entity.name_type)
{
break; case NameType::FIRST_NAME:
consonance_list = m_full_name_dict.at(region).possibilities.first_names;
break; case NameType::LAST_NAME:
consonance_list = m_full_name_dict.at(region).possibilities.last_names;
break; case NameType::PARTICLE:
consonance_list = m_full_name_dict.at(region).possibilities.particles;
}
size_t consonance_i = rand() % consonance_list.size();
const SyllableAssociator::ConsonanceLabel & consonance = consonance_list[consonance_i];
std::string generated_name;
if (name_entity.name_type == NameType::PARTICLE)
generated_name = m_syllable_reader.generate_random_particle(consonance);
else
generated_name = m_syllable_reader.generate_random_name(consonance);
std::string generated_m_name;
for (unsigned int m = 0 ; m < name_entity.multiplicity ; ++m)
generated_m_name += generated_name;
recase_name(generated_m_name);
full_name_result += generated_m_name;
}
else
{
full_name_result += c;
}
}
return full_name_result;
}
/******************************************************************************/
std::string NameAssociator::get_formated_full_name_format(const RegionLabel & region) const
{
std::string result;
const std::string source_string = m_full_name_dict.at(region).format;
for (size_t ic = 0 ; ic < source_string.size() ; ++ic)
{
const char c = source_string.at(ic);
if (m_symbol_pairing.find(c) != m_symbol_pairing.end())
{
switch (m_symbol_pairing.at(c).name_type)
{
break; case NameType::FIRST_NAME: result += "[first name]";
break; case NameType::LAST_NAME: result += "[last name]";
break; case NameType::PARTICLE: result += "[particle]";
}
}
else
{
result += c;
}
}
return result;
}