1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <regex>
#include <iterator>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
typedef istream_iterator<string> isIterator;
typedef map<string, string> indexMap;
typedef std::map<string, string>::iterator idxIt;
string numToString(const int &page, const int &line)
{
string pg = to_string(page);
string ln = to_string(line);
stringstream ss;
ss << pg << "." << ln;
string s = ss.str();
return s;
}
void loadDisplayMap(const string &word, const int &page, const int &line, bool display)
{
indexMap idx;
if(display)
{
cout << "Index contents" << endl
<< "--------------" << endl;
for( idxIt iter = idx.begin(); iter != idx.end(); ++iter )
{
cout << iter->first << endl
<< " " << iter->second << endl;
}
}
else
{
string pgLn = numToString(page, line);
std::pair<std::map<string, string>::iterator,bool> ret;
ret = idx.insert( pair<string, string>(word, pgLn) );
if (ret.second==false)
{
std::stringstream ss;
ss << ", " << pgLn;
idx[word] += ss.str();
}
}
}
string parser (const string &wordLine)
{
regex word("[^A-Za-z0-9\\s]");
string parsed;
const string format=" ";
// C++ 11 regex_replace removes all punctuation as listed
// above and replaces it with a space.
parsed = regex_replace(wordLine,word,format,regex_constants::format_default);
transform(parsed.begin(), parsed.end(), parsed.begin(), ::tolower);
return parsed;
}
int main()
{
vector<string> vecSkip;
std::vector<string>::iterator vIt;
string docName, skipName;
cout << "Enter document file name: ";
cin >> docName;
cout << endl;
cout << "Enter the skip-words file name: ";
cin >> skipName;
cout << endl;
ifstream doc(docName), skip(skipName);
if(!doc || !skip)
{
cout << "File(s) failed to open." << endl;
exit(EXIT_FAILURE);
}
int lineNum = 1, pageNum = 1;
copy(isIterator(skip), isIterator(), back_inserter(vecSkip));
while( !doc.eof() )
{
string temp;
getline(doc,temp);
if(temp == "<newpage>")
{
++pageNum;
lineNum = 1;
continue;
}
temp = parser(temp);
istringstream feed(temp);
while ( !feed.eof() )
{
string word;
feed >> word;
vIt = find(vecSkip.begin(), vecSkip.end(), word);
if(vIt == vecSkip.end() && word.length() > 0)
{
loadDisplayMap(word, pageNum, lineNum, false);
}
}
++lineNum;
}
loadDisplayMap(" ", pageNum, lineNum, true);
return 0;
}
| |