/*
File: Word.cc
@authors:
Antoine Waugh
SID: 0309888
@date: 02/06/05
*/
//System Includes
#include<string>
#include <iostream>
#include<vector>
#include<fstream>
//Other Includes
#include "Node.h"
#include "Word.h"
#include "Hit.h"
using namespace std;
// constructor that takes in the word to be searched
Word::Word(string searched_word) {
//open word_cmp object file for searching
word_cmp word_cmp_file("data/word.cmp");
//open word_pos object file for linking word_cmp to word_idx
word_pos word_pos_file("data/word.pos");
//open word_idx object file to get required act,play and scene data
word_idx word_idx_file("data/word.idx");
//open speeches_idx object file to get required speech start and finish lines
speeches_idx speeches_idx_file("data/speeches.idx");
//open playfile_map object file to get required play name
playfile_map playfile_map_file("data/playfile.map");
//to be a vector<hits> once tested
vector<Hit> returns;
//search through word_cmp for all records where word == searched_word
//iterate through all entries in word_cmp file
for(int i=0; i<word_cmp_file.size(); ++i) {
//check to see if current listing in file represents required word
if( searched_word.compare( trim(word_cmp_file[i].word) ) == 0) {
//find id from word_cmp to pass to word_pos
temp_id = word_cmp_file[i].id;
//create hit for found word
//use word_pos to link the identified index from word_cmp to word_idx
temp_position = word_pos_file[temp_id].position;
temp_extent = word_pos_file[temp_id].extent;
//this loops for each occurence of the word, ensuring all entries are returned (hits)
for(int x = 0; x<temp_extent; x++) {
//increment temp_position for each entry found (extent)
temp_position = temp_position + x;
//use word_idx to gain play number, act, scene and speech position for further
//querying
play_num = word_idx_file[temp_position].play;
act = word_idx_file[temp_position].act;
scene = word_idx_file[temp_position].scene;
speech_pos = word_idx_file[temp_position].speech;
//open value from speeches_idx for first and ending line numbers
startLine = speeches_idx_file[speech_pos].startLine;
endLine = speeches_idx_file[speech_pos].endLine;
play = playfile_map_file[play_num].getPlay();
playfile_directory = "texts/";
playfile_directory.append(playfile_map_file[play_num].second);
ifstream fileIn(playfile_directory.c_str());
unsigned int count =0;
//account for first line
string line;
while(!fileIn.eof() && count <= endLine) {
count++;
getline(fileIn, line);
if(count>startLine && count <=endLine+1) {
speech.append(line+"\n");
}
}
//identify play, act and scene number from word_idx
//identify play name
Hit newHit(searched_word, play, act, scene, speech);
returns.push_back(newHit);
}
}
}
for(int x = 0; x<returns.size(); ++x) {
cout << returns.at(x);
}
//friend Hits operator()(const Context *context);
//friend ostream& print(ostream& out);
}
/*
usage: to remove all whitespace from a given string
puropse: to be used in conjunction with the compare statement of the constructor
to ensure that any leading or trailing whitespace is not considered when evaluating
word_cmp's or the passed search_string.
*/
string
Word::trim(string x)
{
//trim leading whitespace
while(isWhitespace(x[0])) x.erase(0, 1);
//generates iterator of type basic_string and assigns to beginning of string
basic_string<char, std::char_traits<char> >::iterator i = x.begin();
//iterate through string
while(i != x.end())
{
//if iterator -> whitespace remove from string
if(isWhitespace(*i)) i = x.erase(i);
else i++;
}
return x;
}
bool
Word::isWhitespace(const char& c)
{
//add other whitespace chars as necessary and return
return (c == ' ' || c == '\t');
}