Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.88 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "wordsearch.h"
  2. #include<dirent.h>
  3. #include<fstream>
  4. #include<vector>
  5. #include<sstream>
  6. WordSearch::WordSearch()
  7. {
  8.   mWord = new word();
  9. }
  10.  
  11. void WordSearch::loadFiles(string directory_name)
  12. {
  13.   DIR *dir;
  14.   struct dirent *entry;
  15.   if((dir = opendir(directory_name.c_str()))== NULL ){
  16.     cout<<"Error ";
  17.   }
  18.  
  19.   while((entry = readdir(dir))!=NULL){
  20.     if(string(entry->d_name) != "." && string(entry->d_name) != "..")
  21.       {
  22.         cout<<"Opening:" << entry->d_name<<endl;
  23.         string slash("/");
  24.         ifstream fin((string(directory_name)+slash+entry->d_name).c_str()); //open using absolute path
  25.         string word;
  26.         while(true){
  27.           fin>>word;
  28.           cout<<entry->d_name<<"::"<<word<<endl;
  29.           mWord->Insert(word, entry->d_name);
  30.           if(fin.eof()) {cout << "EOF " << entry->d_name << endl; break;}
  31.           // Now the string "word" holds the keyword, and the string "files[i]" holds the document name.
  32.           // Use these two strings to search/insert in your linked lists
  33.           // wordIndex.Insert(word, files[i]);
  34.         }
  35.         fin.close();
  36.       }
  37.   }
  38.   closedir(dir);
  39.   mWord->printWordList();
  40. }
  41.  
  42. std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
  43.   std::stringstream ss(s);
  44.   std::string item;
  45.   while(std::getline(ss, item, delim)) {
  46.     elems.push_back(item);
  47.   }
  48.   return elems;
  49. }
  50.  
  51.  
  52. std::vector<std::string> split(const std::string &s, char delim) {
  53.   std::vector<std::string> elems;
  54.   return split(s, delim, elems);
  55. }
  56.  
  57. void WordSearch::doQuery(string query, string result[], int& size)
  58. {
  59.   cout << "query=" << query << endl;
  60.   vector<string> strings = split(query, ' ');
  61.   for(int i=0; i<strings.size(); ++i){
  62.     cout << "string " << i + 1 << " " << strings[i] << endl;
  63.   }
  64.  
  65.   //  cout <<"isthere Black " << mWord->IsThere("Black") << endl;
  66.  
  67.   //get list of files for strings[0] and strings[2]
  68.   list firstWordList, secondWordList;
  69.   //cout<<"BP0"<<endl;
  70.   mWord->GetFileList(strings[0], firstWordList);
  71.   mWord->GetFileList(strings[2], secondWordList);
  72.    //if string[1] is AND or MINUS
  73.   vector<string> filesWord1, filesWord2;
  74.   vector<string> res;
  75.   if(strings[1]=="AND"){
  76.    //test it
  77.    for(int i=0; i<filesWord1.size(); ++i){
  78.      // cout << "filepath "<< i+1 << " " << filesWord1[i]<<endl;
  79.      if(secondWordList.IsThere(filesWord1[i]))
  80.         res.push_back(filesWord1[i]);
  81.    }
  82.    for(int i=0; i<res.size(); ++i){
  83.      //cout<<res[i]<<"<- the answer "<< endl;
  84.      result[i]=res[i];
  85.    }
  86.   }
  87.  
  88.    //     result = &res[0];
  89.      /*for(int i=0; i<filesWord2.size(); ++i){
  90.      cout <<"filepath "<< i+1 << " " << filesWord2[i]<<endl;
  91.      }*/
  92.      else if(strings[1] == "MINUS"){
  93.        if(!secondWordList.IsThere(filesWord1[i])){
  94.          res.push_back(filesWord1[i]);
  95.        }
  96.        for(int i=0;i<res.size();++i)
  97.          result[i]=res[i];
  98.      }
  99.      else{
  100.        cout << "No Such File" << endl;
  101.      }
  102.    size = res.size();
  103. }