This more or less does what I had in mind. The challenge is to refine the use of the index to create the branch to the relevant function for a valid combination of words, and also to only have a <map> (or a <vector>).
There again a word (association + expectation) tree might be better -> how does Siri do it?
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
|
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
int main()
{
std::string word;
std::vector<std::string> word_vector;
std::map<std::string, int> word_map;
// EXTRACT WORDS
std::string input = "number test command mode";
std::stringstream iss(input);
while( iss >> word)
{
word_vector.push_back(word);
}
std::string temp;
int index{0};
for( int i = 0; i < word_vector.size() + 1; i++)
{
do{
temp = "";
for(int j = 0; j < i; j++)
{
temp += (word_vector[j] + ' ');
}
if(!word_map[temp])
{
word_map[temp] = index;
index++;
}
}while(next_permutation(word_vector.begin(), word_vector.end()));
}
for(auto &i: word_map)
std::cout << i.second << '\t' << i.first << '\n';
return 0;
}
|
1
2 command
6 command mode
18 command mode number
42 command mode number test
19 command mode test
43 command mode test number
7 command number
20 command number mode
44 command number mode test
21 command number test
45 command number test mode
8 command test
22 command test mode
46 command test mode number
23 command test number
47 command test number mode
3 mode
9 mode command
24 mode command number
48 mode command number test
25 mode command test
49 mode command test number
10 mode number
26 mode number command
50 mode number command test
27 mode number test
51 mode number test command
11 mode test
28 mode test command
52 mode test command number
29 mode test number
53 mode test number command
4 number
12 number command
30 number command mode
54 number command mode test
31 number command test
55 number command test mode
13 number mode
32 number mode command
56 number mode command test
33 number mode test
57 number mode test command
14 number test
34 number test command
58 number test command mode
35 number test mode
59 number test mode command
5 test
15 test command
36 test command mode
60 test command mode number
37 test command number
61 test command number mode
16 test mode
38 test mode command
62 test mode command number
39 test mode number
63 test mode number command
17 test number
40 test number command
64 test number command mode
41 test number mode
65 test number mode command
Program ended with exit code: 0