-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguageManager.cpp
188 lines (156 loc) · 5.85 KB
/
LanguageManager.cpp
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
181
182
183
184
185
186
187
188
/**
* @file LanguageManager.cpp
* @author Marc S. Ressl, Albertina Galan, Alejandro Nahuel Heir
* @brief Administrador de lenguajes, para agregarlos y detectar existentes.
* @version 0.1
* @date 2022-03-29
*
* @copyright Copyright (c) 2022
*
*/
#include <algorithm>
#include <iostream>
#include "LanguageManager.h"
using namespace std;
const string LANGUAGECODE_NAMES_FILE = "../resources/languagecode_names_es.csv";
const string TRIGRAMS_PATH = "../resources/trigrams/";
/**
* @brief Loads trigram data.
*
* @param text: the corpus of the language to add
* @param languageCodeNames: Map of language code vs. language name (in user's locale).
* @param trigramProfiles: The trigram profiles.
* @return: whether the function succeeded.
*/
bool loadLanguagesData(map<string, string> &languageCodeNames, Languages &languages)
{
// Reads available language codes
cout << "Reading language codes..." << endl;
CSVData languageCodesCSVData;
if (!readCSV(LANGUAGECODE_NAMES_FILE, languageCodesCSVData))
return false;
// Reads trigram profile for each language code
for (auto fields : languageCodesCSVData)
{
if (fields.size() != 2)
continue;
string languageCode = fields[0];
string languageName = fields[1];
languageCodeNames[languageCode] = languageName;
cout << "Reading trigram profile for language code \"" << languageCode << "\"..." << endl;
CSVData languageCSVData;
if (!readCSV(TRIGRAMS_PATH + languageCode + ".csv", languageCSVData))
return false;
languages.push_back(Language());
Language &language = languages.back();
language.languageCode = languageCode;
for (auto fields : languageCSVData)
{
if (fields.size() != 2)
continue;
string trigram = fields[0];
float frequency = (float)stoi(fields[1]);
language.trigramProfile[trigram] = frequency;
}
normalizeTrigramProfile(language.trigramProfile);
}
return true;
}
/**
* @brief Allows the addition of a new language to the list of languages the program interprets
*
* @param text: the corpus of the language to add
* @param languageCodeNames: the list of all current languages
* @param languages: the trigram profiles of all languages
* @param languageCode: the code for the new language
* @param languageName: the name of the new language
* @param result: whether the language was added or there was an error
*/
void addLanguage(const Text &text, map<string, string> &languageCodeNames,
Languages &languages, const string &languageCode,
const string &languageName, string &result)
{
// Se crea el trigramProfile del texto nuevo, y se escribe su .csv
TrigramProfile trigramProfileHandler = buildTrigramProfile(text);
CSVData csvDataHandler;
for (auto profile : trigramProfileHandler)
{
csvDataHandler.push_front({profile.first, to_string((int)profile.second)});
}
csvDataHandler.sort(compareTrigramByFreq);
writeCSV(TRIGRAMS_PATH + languageCode + ".csv", csvDataHandler);
// Se agrega al mapa
languageCodeNames[languageCode] = languageName;
// Se escribe el nuevo csv con el nuevo idoma, ordenándolo alfabéticamente
csvDataHandler.clear();
for (auto profile : languageCodeNames)
{
csvDataHandler.push_front({profile.first, profile.second});
}
csvDataHandler.sort();
writeCSV(LANGUAGECODE_NAMES_FILE, csvDataHandler);
// Se actualiza la lista de idiomas que usa main
Language newLanguage;
newLanguage.languageCode = languageCode;
newLanguage.trigramProfile = trigramProfileHandler;
languages.push_back(newLanguage);
result = "Se agregó: " + languageName;
}
/**
* @brief Verifies if the language to add already exists or if there is an error in text format
*
* @param languageCodeNames: the list of all current languages
* @param languageIdentifier: the code and name of the new language. Should be first line of text
* @param languageCode: the code for the new language
* @param languageName: the name of the new language
* @param output: the string with the result: if its able to add the language or not
* @return true Posible agregarlo
* @return false Imposible agregarlo
*/
bool verifyNewLanguage(const map<string, string> &languageCodeNames,
const string &languageIdentifier, string &languageCode,
string &languageName, string &output)
{
bool error = false;
if (languageIdentifier[3] == ',')
{
languageCode = languageIdentifier.substr(0, 3);
auto iteratorCode = languageCodeNames.find(languageCode);
if (iteratorCode == languageCodeNames.end())
{
languageName = languageIdentifier.substr(4);
for (auto it = languageCodeNames.begin(); it != languageCodeNames.end(); ++it)
{
if (it->second == languageName)
{
output = "Ya existe ese idioma: " + languageName;
error = true;
break;
}
}
}
else
{
output = "Ya existe un idioma con ese codigo: " + languageCode;
error = true;
}
}
else
{
output = "Formato de archivo incorrecto: " + languageIdentifier;
error = true;
}
return !error;
}
/**
* @brief Criteria of comparison to order the CSV list, with trigramas and frecuency
*
* @param first Vector of strings with {trigam, frecuency}
* @param second Vector of strings with {trigam, frecuency}
* @return true, first has a higher frequency than second
* @return false, first has a lower frequency than second
*/
bool compareTrigramByFreq(const vector<string> &first, const vector<string> &second)
{
return stof(first[1]) > stof(second[1]);
}