-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
95 lines (92 loc) · 2.77 KB
/
Copy pathmain.cpp
File metadata and controls
95 lines (92 loc) · 2.77 KB
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
#include <string>
#include <iostream>
#include <unordered_map>
#include <fstream>
#include <sstream>
using namespace std;
struct trienode {
unordered_map<char,trienode*> children;
bool wordend;
string condition;
string review;
string rating;
string date;
string usefulCount;
trienode() : wordend(false) {}
};
class Trie {
trienode* root;
public:
Trie() {
root = new trienode;
}
void insert(string &drugName, string &condition, string &review, string rating, string &date, string usefulCount) {
trienode* curr = root;
for(char c : drugName) {
if(curr->children.find(c) == curr->children.end()) {
curr->children[c] = new trienode;
}
curr = curr->children[c];
}
curr->wordend = true;
curr->condition = condition;
curr->review = review;
curr->rating = rating;
curr->date = date;
curr->usefulCount = usefulCount;
}
void search(string drugName) {
trienode* curr = root;
for(char c : drugName) {
if(curr->children.find(c) == curr->children.end()) {
cout << "Not found" << endl;
}
curr = curr->children[c];
}
if(curr->wordend) {
cout << curr->condition << endl;
cout << curr->review << endl;
cout << curr->rating << endl;
cout << curr->date << endl;
cout << curr->usefulCount << endl;
} else {
cout << "Not found" << endl;
}
}
};
void parse(Trie &trie,string line) {
stringstream ss(line);
string drugName,id,condition,review,date,usefulCount,rating;
getline(ss, id, '\t');
getline(ss, drugName, '\t');
getline(ss, condition, '\t');
getline(ss, review, '\t');
getline(ss, rating, '\t');
getline(ss, date, '\t');
getline(ss, usefulCount, '\t');
trie.insert(drugName,condition,review,rating,date,usefulCount);
}
int main() {
Trie trie;
ifstream myfile("drugsComTest_raw.tsv");
string line;
while(getline(myfile, line)) {
parse(trie, line);
}
string input = "";
cout << "enter a drug name, or type exit to stop."<< endl;
while (getline(cin, input)) {
if(input == "exit") {
break;
}
trie.search(input);
cout <<"enter a drug name, or type exit to stop."<<endl;
}
cout<< "thank you"<< endl;
return 0;
};
// https://www.digitalocean.com/community/tutorials/trie-data-structure-in-c-plus-plus
// https://algo.monster/liteproblems/208
// https://www.geeksforgeeks.org/implementation-of-trie-prefix-tree-in-c/
// https://www.baeldung.com/cs/tries-prefix-trees
// https://www.geeksforgeeks.org/introduction-to-trie-data-structure-and-algorithm-tutorials/