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
|
int main() {
std::map <std::string, std::string> songs = {
{
"Halo", "Remember those walls I built? "
"Well, baby, they're tumbling down "
"And they didn't even put up a fight "
"They didn't even make a sound "
"I found a way to let you in "
"But I never really had a doubt "
"Standin' in the light of your halo "
"I got my angel now "
"It's like I've been awakened "
"Every rule I had you breakin' "
"It's the risk that I'm takin' "
"I ain't never gonna shut you out "
"Everywhere I'm lookin' now "
"I'm surrounded by your embrace "
"Baby, I can see your halo "
"You know you're my saving grace "
"You're everything I need and more "
"It's written all over your face "
"Baby, I can feel your halo "
"Pray it won't fade away "
""
"I can see your halo (halo), halo "
"I can feel your halo (halo), halo "
"I can see your halo (halo), halo"
},
{"See you again", "It's been a long day without you, my friend "
"And I'll tell you all about it when I see you again "
"We've come a long way from where we began "
"Oh, I'll tell you all about it when I see you again "
"When I see you again "
"Damn, who knew? "
"All the planes we flew, good things we been through "
"That I'd be standing right here talking to you "
"'Bout another path, I know we loved to hit the road and laugh "
"But something told me that it wouldn't last "
"Had to switch up, look at things different, see the bigger picture "
"Those were the days, hard work forever pays "
"Now I see you in a better place (see you in a better place) "
"Uh "
"How can we not talk about family when family's all that we got? "
"Everything I went through, you were standing there by my side "
"And now you gon' be with me for the last ride "
"It's been a long day without you, my friend "
"And I'll tell you all about it when I see you again (I'll see you again) "
"We've come a long way (yeah, we came a long way) "
"From where we began (you know we started) "
"Oh, I'll tell you all about it when I see you again (I'll tell you) "
"When I see you again"},
{"Sky full of Stars", "'Cause you're a sky, 'cause you're a sky full of stars"
"I'm gonna give you my heart"
"'Cause you're a sky, 'cause you're a sky full of stars"
"'Cause you light up the path"
"I don't care, go on and tear me apart"
"I don't care if you do, ooh-ooh, ooh"
"'Cause in a sky, 'cause in a sky full of stars"
"I think I saw you"
"'Cause you're a sky, 'cause you're a sky full of stars"
"I wanna die in your arms, oh, oh-oh"
"'Cause you get lighter the more it gets dark"
"I'm gonna give you my heart, oh"
"I don't care, go on and tear me apart"
"I don't care if you do, ooh-ooh, ooh"
"'Cause in a sky, 'cause in a sky full of stars"
"I think I see you"
"I think I see you"
"'Cause you're a sky, you're a sky full of stars"
"Such a heavenly view"
"You're such a heavenly view"
"Yeah, yeah, yeah, ooh"}
};
std::set<std::string> vocabulary;
for (const auto& song : songs) {
std::vector <std::string> tokens = tokenize(preprocess(song.second));
vocabulary.insert(tokens.begin(), tokens.end());
}
std::map<std::string, int> word_index = create_word_index(vocabulary);
int input_size = vocabulary.size();
int hidden_size = 9;
int output_size = songs.size();
NeuralNetwork nn(input_size, hidden_size, output_size);
double learning_rate = 0.1;
int epochs = 500;
std::vector<std::pair<std::string, std::string>> song_list(songs.begin(), songs.end());
std::mt19937 generator(static_cast<unsigned int>(std::time(nullptr)));
for (int epoch = 0; epoch < epochs; ++epoch) {
std::shuffle(song_list.begin(), song_list.end(), generator);
int output_index = 0;
for (const auto& song : song_list) {
std::vector <std::string> tokens = tokenize(preprocess(song.second));
for (const auto& token : tokens) {
std::vector<double> input = one_hot_encode(token, word_index);
std::vector<double> target(output_size, 0.0);
target[output_index] = 1.0;
nn.train(input, target, learning_rate);
}
++output_index;
}
}
std::string test_word = "see you again";
std::vector<double> test_input = one_hot_encode(preprocess(test_word), word_index);
std::vector<double> prediction = nn.forward(test_input);
std::cout << "test string: " << test_word << std::endl;
std::cout << "result : ";
for (double p : prediction) {
std::cout << p << " ";
}
std::cout << std::endl;
size_t best_match = std::distance(prediction.begin(), std::max_element(prediction.begin(), prediction.end()));
size_t index = 0;
std::string best_song_title;
for (const auto& song : songs) {
if (index == best_match) {
best_song_title = song.first;
break;
}
++index;
}
std::cout << "the song '" << test_word << "' matches best to " << best_song_title << std::endl;
return 0;
}
|