#include #include #include "apstring.h" #include "strutils.h" #include "apvector.h" #include "apset3.h" #include "systimer.h" void ReadWords(const apstring & filename, apset & wordset) // postcondition: all unique words in input stored in wordset { ifstream input; input.open(filename.c_str()); apstring word; int count = 0; cout << "reading " << filename << endl; wordset.makeEmpty(); while (input >> word) // reading word succeeded { ToLower(word); StripPunc(word); count++; wordset.add(word); } cout << "read " << count << " words, unique = " << wordset.size() << endl; } int main() { apstring basedir = "d:\\data\\shakespeare\\"; const int NUM_PLAYS = 6; apvector plays(6); plays[0] = "romeo.txt"; plays[1] = "hamlet.txt"; plays[2] = "errors.txt"; plays[3] = "macbeth.txt"; plays[4] = "errors.txt"; plays[5] = "allswell.txt"; int k; apset words,setunion,setintersection; SysTimer timer; timer.start(); ReadWords(basedir+plays[0],words); timer.stop(); cout << " first read = " << timer.elapsedTime() << endl; setunion = setintersection = words; for(k=1; k < NUM_PLAYS; k++) { timer.start(); ReadWords(basedir+plays[k],words); timer.stop(); cout << " first read = " << timer.elapsedTime() << endl; timer.start(); setunion += words; timer.stop(); cout << "union time = " << timer.elapsedTime() << endl; timer.start(); setintersection *= words; timer.stop(); cout << "intersect time = " << timer.elapsedTime() << endl; cout << "union size: " << setunion.size() << endl; cout << "intersection size:" << setintersection.size() << endl; } return 0; }