// David Ferris - Capstone 2014 - Word.h // Last Updated 5/10/2014 #ifndef WORD_H #define WORD_H #include #include using namespace std; class Word { public: //------------Construction/Destruction------------ Word(); Word(string t, int index); virtual ~Word(); Word(const Word & orig); Word & operator=(const Word & right); //---------------Accessors/Mutators--------------- string GetText(); // Returns the word's text int GetFirstAppearanceIndex(); // Returns the sentence index of the first appearance of the word int * GetSentenceIndices(); // Returns a pointer to an array of sentence-table indices where the word is found (via reference) int GetAppearancesCount(); // Returns the amount of times the word appears in the full text (Size of appearances array) void AddSentenceIndex(int index); // Adds another sentence-index to the appearances array void Dump(); private: string text; int appearances_count; int * appearances; // Will point to dynamically-allocated appearances array }; //------------Construction/Destruction------------ Word::Word() { text = "Invalid"; appearances_count = 0; appearances = 0; } /************************************************************ Takes a string and an int index as parameters. Sets the appearance count to 1, and records the sentence index of the appearance in the first cell of the appearances table. ************************************************************/ Word::Word(string t, int index) { text = t; appearances_count = 1; appearances = new int [appearances_count]; appearances[0] = index; } Word::Word(const Word & orig) { text = orig.text; appearances_count = orig.appearances_count; appearances = new int [appearances_count]; for(int i = 0; i < appearances_count; i++) appearances[i] = orig.appearances[i]; } Word & Word::operator=(const Word & right) { text = right.text; appearances_count = right.appearances_count; if(appearances != 0) delete [] appearances; appearances = new int [appearances_count]; for(int i = 0; i < appearances_count; i++) appearances[i] = right.appearances[i]; return *this; } Word::~Word() { if(appearances != 0) delete [] appearances; appearances = 0; appearances_count = 0; } //---------------Accessors/Mutators--------------- string Word::GetText() { return text; } /************************************************************ --------------------GetSentenceIndices----------------------- Returns a pointer to an array which contains the indices of all sentences where the word can be found in the sentences table. For example, if a word appears in sentences found at lines 4, 7, and 12 in the file, a pointer to a length 3 array containing 4, 7, and 12 will be returned. ************************************************************/ int * Word::GetSentenceIndices() { int * arr = new int [appearances_count]; for(int i = 0; i < appearances_count; i++) arr[i] = appearances[i]; return arr; } int Word::GetFirstAppearanceIndex() { if(appearances != 0) return appearances[0]; else return -1; } int Word::GetAppearancesCount() { return appearances_count; } /************************************************************ ---------------------AddSentenceIndex------------------------ Adds an appearance (line # in the senteces file) to a word's appearances array. This simply entails adding a new index to a dynamic array, and inserting the passed integer into it. ************************************************************/ void Word::AddSentenceIndex(int index) { // Creates a temp array of size appearances_count + 1 int * temp = new int [appearances_count + 1]; // Transfer contents of appearances to temp for(int i = 0; i < appearances_count; i++) temp[i] = appearances[i]; // Add new data to the extra temp cell temp[appearances_count] = index; // Delete old appearances array delete [] appearances; // Point appearances to the new array, and temp to null appearances = temp; temp = 0; appearances_count++; // Increment count } /************************************************************ ----------------------------Dump----------------------------- Function used solely for testing. Output gives the text of the Word, the number of times it has appeared, and the indices of the sentences in which the Word appeared. ************************************************************/ void Word::Dump() { cout << " Text: " << text << endl; cout << " Appearances Count: " << appearances_count << endl; cout << " Appearances: " << endl; for(int i = 0; i < appearances_count; i++) cout << " #" << i + 1 << " - Sentence Index: " << appearances[i] << endl; } #endif