問題
言語モデルは生のテキストではなくトークンを扱います。単純なトークナイザはテキストを小文字にし、a-z の文字と 0-9 の数字が連続する部分を1つの単語とみなします。それ以外(空白、句読点、その他の文字)はすべて単語の区切りです。
語彙を作ります:異なる単語を、出現回数の多い順に並べ、同数ならアルファベット順にします。このリストでの単語の位置がトークン ID です。
build_vocabulary(text) を実装してください。
入力
text: 任意のテキスト。
出力
語彙を単語のリストとして返してください。テキストに単語がなければ空のリストを返します。
制約
0 ≤ text の長さ ≤ 100,000
例
例 1
入力:
text = "The cat sat on the mat. The end!"
期待される出力:
expected = ["the", "cat", "end", "mat", "on", "sat"]
入力:
std::string text = "The cat sat on the mat. The end!";
期待される出力:
std::vector<std::string> expected = {"the", "cat", "end", "mat", "on", "sat"};
入力:
string text = "The cat sat on the mat. The end!";
期待される出力:
string[] expected = new string[] {"the", "cat", "end", "mat", "on", "sat"};
解説
小文字にすると、単語は the、cat、sat、on、the、mat、the、end です。"the" は3回、残りは1回ずつでアルファベット順に並びます:["the", "cat", "end", "mat", "on", "sat"]。
例 2
入力:
text = "AI, ai and A.I. (2024)"
期待される出力:
expected = ["ai", "2024", "a", "and", "i"]
入力:
std::string text = "AI, ai and A.I. (2024)";
期待される出力:
std::vector<std::string> expected = {"ai", "2024", "a", "and", "i"};
入力:
string text = "AI, ai and A.I. (2024)";
期待される出力:
string[] expected = new string[] {"ai", "2024", "a", "and", "i"};
解説
単語:ai、ai、and、a、i、2024。"ai" は2回、他は1回ずつで、数字が文字より前に来るアルファベット順に並びます:["ai", "2024", "a", "and", "i"]。
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。