Problema
I modelli linguistici lavorano su token, non su testo grezzo. Un tokenizzatore semplice converte il testo in minuscolo e tratta ogni sequenza di lettere a-z e cifre 0-9 come una parola; tutto il resto (spazi, punteggiatura, altri caratteri) separa le parole.
Costruisci il vocabolario: le parole distinte ordinate per frequenza (la più frequente per prima), con i pareggi risolti in ordine alfabetico. La posizione di una parola in questa lista è il suo ID di token.
Implementa build_vocabulary(text).
Input
text: Un testo qualsiasi.
Output
Restituisci il vocabolario come lista di parole. Se il testo non contiene parole, restituisci una lista vuota.
Vincoli
0 ≤ lunghezza di text ≤ 100.000
Esempi
Input:
text = "The cat sat on the mat. The end!"
Output atteso:
expected = ["the", "cat", "end", "mat", "on", "sat"]
Input:
std::string text = "The cat sat on the mat. The end!";
Output atteso:
std::vector<std::string> expected = {"the", "cat", "end", "mat", "on", "sat"};
Input:
string text = "The cat sat on the mat. The end!";
Output atteso:
string[] expected = new string[] {"the", "cat", "end", "mat", "on", "sat"};
Spiegazione
In minuscolo, le parole sono the, cat, sat, on, the, mat, the, end. "the" compare 3 volte; le altre compaiono una volta e sono ordinate alfabeticamente: ["the", "cat", "end", "mat", "on", "sat"].
Input:
text = "AI, ai and A.I. (2024)"
Output atteso:
expected = ["ai", "2024", "a", "and", "i"]
Input:
std::string text = "AI, ai and A.I. (2024)";
Output atteso:
std::vector<std::string> expected = {"ai", "2024", "a", "and", "i"};
Input:
string text = "AI, ai and A.I. (2024)";
Output atteso:
string[] expected = new string[] {"ai", "2024", "a", "and", "i"};
Spiegazione
Parole: ai, ai, and, a, i, 2024. "ai" compare due volte; le altre una volta, in ordine alfabetico con le cifre prima delle lettere: ["ai", "2024", "a", "and", "i"].
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.