- #include "wordsearch.h"
- #include<dirent.h>
- #include<fstream>
- #include<vector>
- #include<sstream>
- WordSearch::WordSearch()
- {
- mWord = new word();
- }
- void WordSearch::loadFiles(string directory_name)
- {
- DIR *dir;
- struct dirent *entry;
- if((dir = opendir(directory_name.c_str()))== NULL ){
- cout<<"Error ";
- }
- while((entry = readdir(dir))!=NULL){
- if(string(entry->d_name) != "." && string(entry->d_name) != "..")
- {
- cout<<"Opening:" << entry->d_name<<endl;
- string slash("/");
- ifstream fin((string(directory_name)+slash+entry->d_name).c_str()); //open using absolute path
- string word;
- while(true){
- fin>>word;
- cout<<entry->d_name<<"::"<<word<<endl;
- mWord->Insert(word, entry->d_name);
- if(fin.eof()) {cout << "EOF " << entry->d_name << endl; break;}
- // Now the string "word" holds the keyword, and the string "files[i]" holds the document name.
- // Use these two strings to search/insert in your linked lists
- // wordIndex.Insert(word, files[i]);
- }
- fin.close();
- }
- }
- closedir(dir);
- mWord->printWordList();
- }
- std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
- std::stringstream ss(s);
- std::string item;
- while(std::getline(ss, item, delim)) {
- elems.push_back(item);
- }
- return elems;
- }
- std::vector<std::string> split(const std::string &s, char delim) {
- std::vector<std::string> elems;
- return split(s, delim, elems);
- }
- void WordSearch::doQuery(string query, string result[], int& size)
- {
- cout << "query=" << query << endl;
- vector<string> strings = split(query, ' ');
- for(int i=0; i<strings.size(); ++i){
- cout << "string " << i + 1 << " " << strings[i] << endl;
- }
- // cout <<"isthere Black " << mWord->IsThere("Black") << endl;
- //get list of files for strings[0] and strings[2]
- list firstWordList, secondWordList;
- //cout<<"BP0"<<endl;
- mWord->GetFileList(strings[0], firstWordList);
- mWord->GetFileList(strings[2], secondWordList);
- //if string[1] is AND or MINUS
- vector<string> filesWord1, filesWord2;
- vector<string> res;
- if(strings[1]=="AND"){
- //test it
- for(int i=0; i<filesWord1.size(); ++i){
- // cout << "filepath "<< i+1 << " " << filesWord1[i]<<endl;
- if(secondWordList.IsThere(filesWord1[i]))
- res.push_back(filesWord1[i]);
- }
- for(int i=0; i<res.size(); ++i){
- //cout<<res[i]<<"<- the answer "<< endl;
- result[i]=res[i];
- }
- }
- // result = &res[0];
- /*for(int i=0; i<filesWord2.size(); ++i){
- cout <<"filepath "<< i+1 << " " << filesWord2[i]<<endl;
- }*/
- else if(strings[1] == "MINUS"){
- if(!secondWordList.IsThere(filesWord1[i])){
- res.push_back(filesWord1[i]);
- }
- for(int i=0;i<res.size();++i)
- result[i]=res[i];
- }
- else{
- cout << "No Such File" << endl;
- }
- size = res.size();
- }