Caeg

Untitled

Oct 6th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. #include<sstream> // To use istringstream
  5. #include<algorithm> // To use transform()
  6. using namespace std;
  7.  
  8. struct TokenFreq
  9. {
  10. string token;
  11. int freq = 1;
  12. };
  13.  
  14. void matrixInit(vector<vector<int> > &matrix, int numRows, int numCols) {
  15. int i, j;
  16. matrix.resize(numRows, vector<int>(numCols)); // Resizing vector
  17. for (i = 0; i < numRows; i++) { // Initializing
  18. for (j = 0; j < numCols; j++)
  19. matrix[i][j] = i * j;
  20. }
  21. }
  22.  
  23. void getTokenFreqVec(const string &istr, vector<TokenFreq> &tfVec) {
  24. string tkn;
  25. int i, flag;
  26. TokenFreq t;
  27. istringstream isStream(istr);
  28. while (getline(isStream, tkn, ' '))
  29. { // Tokenizing string
  30. transform(tkn.begin(), tkn.end(), tkn.begin(), ::tolower); // Converting all to lower case
  31. flag = 1;
  32.  
  33. for (i = 0; i < tfVec.size(); i++) { // If tfVec contains the frequency for token
  34. if (tfVec[i].token == tkn) {
  35. tfVec[i].freq += 1; // Increment it
  36. flag = 0;
  37. }
  38. }
  39. if (flag) { // Inserting new word into vector if token is not present in it
  40. t.token = tkn;
  41. tfVec.push_back(t);
  42. }
  43. }
  44. }
  45.  
  46. void selectionSort(vector<TokenFreq> &tokFreqVector) {
  47. int i, j, min;
  48. TokenFreq tkn;
  49. for (i = 0; i < tokFreqVector.size() - 1; i++)
  50. {
  51. min = i;
  52. for (j = i + 1; j < tokFreqVector.size(); j++) {
  53. if (tokFreqVector[j].freq < tokFreqVector[min].freq) { // Swap values if new minimum element is found
  54. tkn = tokFreqVector[min];
  55. tokFreqVector[min] = tokFreqVector[j];
  56. tokFreqVector[j] = tkn;
  57. }
  58. }
  59. }
  60. }
  61.  
  62. void insertionSort(vector<TokenFreq> &tokFreqVector) {
  63. int i, j;
  64. TokenFreq token;
  65. for (i = 1; i < tokFreqVector.size(); i++) {
  66. token = tokFreqVector[i];
  67. for (j = i - 1; j >= 0 && tokFreqVector[j].freq < token.freq; j--)
  68. tokFreqVector[j + 1] = tokFreqVector[j];
  69. tokFreqVector[j + 1] = token;
  70. }
  71. }
  72.  
  73. int main() {
  74. string istr = "And no, I'm not a walking C++ dictionary. I do not keep every technical detail in my head at all times. If I did that, I would be a much poorer programmer. I do keep the main points straight in my head most of the time, and I do know where to find the details when I need them. by Bjarne Stroustrup";
  75. vector<TokenFreq> tfVec;
  76. vector<vector<int> > matrix;
  77. int i, j;
  78.  
  79.  
  80. matrixInit(matrix, 3, 4);
  81. cout << "\nTesting matrix.\n";
  82. for (i = 0; i < 3; i++) {
  83. for (j = 0; j < 4; j++)
  84. cout << "\nmatrix[" << i << "][" << j << "]=" << matrix[i][j];
  85. }
  86. getTokenFreqVec(istr, tfVec);
  87. cout << "\n\n\nTesting Tokenizer.\n\nSize: " << tfVec.size() << endl;
  88. for (i = 0; i < tfVec.size(); i++)
  89. cout << "\nToken: " << tfVec[i].token << ", Freq: " << tfVec[i].freq;
  90. selectionSort(tfVec);
  91. cout << "\n\n\nTesting selection sort.\n\n";
  92. for (i = 0; i < tfVec.size(); i++)
  93. cout << "\nToken: " << tfVec[i].token << ", Freq: " << tfVec[i].freq;
  94. insertionSort(tfVec);
  95. cout << "\n\n\nTesting insertion sort.\n\n";
  96. for (i = 0; i < tfVec.size(); i++)
  97. cout << "\nToken: " << tfVec[i].token << ", Freq: " << tfVec[i].freq;
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment