fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <set>
  4. #include <fstream>
  5. #include <algorithm>
  6. #include <cctype>
  7. #include <iomanip>
  8. #include <vector>
  9. #include <map>
  10. #include <queue>
  11. #include <numeric>
  12. #include <string>
  13. #include <cmath>
  14. #include <climits>
  15. #include <stack>
  16. #include <complex>
  17. #include <cstdlib>
  18. #include <cstring>
  19. #include <array>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. #include <functional>
  23. #include <bitset>
  24. #include <cassert>
  25. #include <tuple>
  26.  
  27. #include <iostream>
  28. #include <sstream>
  29. #include <set>
  30. #include <fstream>
  31. #include <algorithm>
  32. #include <string>
  33. #include <cmath>
  34. #include <unordered_set>
  35.  
  36. using namespace std;
  37. typedef long long ll;
  38.  
  39. class StringSet {
  40. private:
  41. set <string> wrds;
  42. multiset <string> wrdss;
  43. void procText(const string &txt) {
  44. stringstream ss(txt);
  45. string w;
  46. while (ss >> w) {
  47. // Remove punctuation and convert to lowercase
  48. w.erase(remove_if(w.begin(), w.end(), ::ispunct), w.end());
  49. transform(w.begin(), w.end(), w.begin(), ::tolower);
  50. if (!w.empty()) {
  51. wrds.insert(w);
  52. wrdss.insert(w);
  53. }
  54. }
  55. }
  56.  
  57. public:
  58. // Constructor to load words from a file or text
  59. StringSet(const string &filename, bool txt = false) {
  60. if (txt) {
  61. procText(filename);
  62. return;
  63. }
  64. ifstream file(filename);
  65. string line;
  66. while (getline(file, line)) {
  67. procText(line);
  68. }
  69. file.close();
  70. }
  71.  
  72. void add(const string &w) {
  73. wrds.insert(w);
  74. wrdss.insert(w);
  75. }
  76.  
  77. void remove(const string &w) {
  78. wrdss.erase(wrdss.find(w));
  79. if (!wrdss.count(w)) wrds.erase(w);
  80. }
  81.  
  82. void clear() {
  83. wrds.clear();
  84. }
  85.  
  86. int size() const {
  87. return wrds.size();
  88. }
  89.  
  90. void display() const {
  91. for (const auto &w : wrds) {
  92. cout << w << " ";
  93. }
  94. cout << endl;
  95. }
  96.  
  97. // Return union of two sets
  98. StringSet operator+(const StringSet &other) const {
  99. StringSet res("",true);
  100. res.wrds = wrds;
  101. res.wrdss=wrdss;
  102. res.wrds.insert(other.wrds.begin(), other.wrds.end());
  103. res.wrdss.insert(other.wrdss.begin(), other.wrdss.end());
  104. return res;
  105. }
  106.  
  107. // Return intersection of two sets
  108. StringSet operator*(const StringSet &other) const {
  109. StringSet res("",true);
  110. for (const auto &w : wrds) {
  111. if (other.wrds.count(w)) {
  112. res.wrds.insert(w);
  113. }
  114. }
  115. return res;
  116. }
  117.  
  118. // Compute similarity using binary cosine coefficient
  119. double similarity(const StringSet &other) const {
  120. int commonWrds = (*this * other).size(); // Intersection size
  121. return 1.*commonWrds / (sqrt(size()) * sqrt(other.size()));
  122. }
  123. };
  124.  
  125. int main() {
  126. int programChoice;
  127. cout << "Choose a program:\n1. Document Similarity\n2. Task Manager\n";
  128. cin >> programChoice;
  129.  
  130. if (programChoice == 1) {
  131. StringSet docSet("",true), querySet("",true);
  132. string input, word;
  133. int isFileInput;
  134.  
  135.  
  136. cout << "Enter first document:\n1. From file\n2. Manually enter text\n";
  137. cin >> isFileInput;
  138. cin.ignore();
  139.  
  140. if (isFileInput == 1) {
  141. cout << "Enter file name: ";
  142. getline(cin, input);
  143. docSet = StringSet(input);
  144. } else {
  145. cout << "Enter text: ";
  146. getline(cin, input);
  147. docSet = StringSet(input, true);
  148. }
  149.  
  150.  
  151. cout << "Enter second document/query:\n1. From file\n2. Manually enter text\n";
  152. cin >> isFileInput;
  153. cin.ignore();
  154.  
  155. if (isFileInput == 1) {
  156. cout << "Enter file name: ";
  157. getline(cin, input);
  158. querySet = StringSet(input);
  159. } else {
  160. cout << "Enter text: ";
  161. getline(cin, input);
  162. querySet = StringSet(input, true);
  163. }
  164.  
  165. int option;
  166. do {
  167. cout << "\nChoose an operation:\n";
  168. cout << "1. Add word\n2. Remove word\n3. Display set\n";
  169. cout << "4. Calculate similarity\n5. Clear set\n6. Quit\n";
  170. cin >> option;
  171. cin.ignore();
  172.  
  173. if (option == 1) {
  174. cout << "Enter word to add: ";
  175. getline(cin, word);
  176. docSet.add(word);
  177. } else if (option == 2) {
  178. cout << "Enter word to remove: ";
  179. getline(cin, word);
  180. docSet.remove(word);
  181. } else if (option == 3) {
  182. cout << "Document Set Words: ";
  183. docSet.display();
  184. cout << "Query Set Words: ";
  185. querySet.display();
  186. } else if (option == 4) {
  187. cout << "Similarity: " << docSet.similarity(querySet) << endl;
  188. } else if (option == 5) {
  189. docSet.clear();
  190. cout << "Document set cleared.\n";
  191. } else if (option == 6) {
  192. cout << "Exiting Document Similarity program.\n";
  193. } else {
  194. cout << "Invalid option, try again.\n";
  195. }
  196. } while (option != 6);
  197.  
  198. } else if (programChoice == 2) {
  199.  
  200. cout << "Task Manager program not implemented yet.\n";
  201. } else {
  202. cout << "Invalid program choice.\n";
  203. }
  204.  
  205. return 0;
  206. }
  207.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Choose a program:
1. Document Similarity
2. Task Manager
Invalid program choice.