void Concordance::print(int wordIndent, int lineLength)
{
	std::cout << "CONCORDANCE TABLE" << std::endl << std::endl;
	
	WordTable::WordList listOfWords;
	listOfWords = table.index();

	// Iterate through all the words in listOfWords and call a table.getOccurrences(word) on each one to 
	// get the OccurrenceList then call getContext() on each occurrence and print the wordOccurrence and its context out

	std::vector<std::string>::iterator wordIter = listOfWords.begin();
	while(wordIter != listOfWords.end())
	{
		const WordTable::OccurrenceList& occurList = table.getOccurrences(*wordIter);
		// occurList is a reference to a vector<std::streampos>. iterate through this vector, getting the context of each
		// occurrence
		std::vector<std::streampos>::const_iterator occurrenceIterator = occurList.begin();
		while(occurrenceIterator != occurList.end())
		{
			//get the context of each occurrence and print it out
			std::string wordString = *wordIter;
			// for now we'll *assume* we need to make a wordOccurrence to pass to 
			// getContext since we only get streamposes from an occurrenceList
			WordOccurrence tempWord(wordString,*occurrenceIterator);

			//get the word's context
			StringPair contextStrings = wordStream.getContext(tempWord,wordIndent,lineLength-wordString.length()-wordIndent-2);

			//print the word's prefix...
			//difference between how many chars in prefix & the desired amount
			int prefixDiff = wordIndent - contextStrings.first.length(); 
			while(prefixDiff > 0){ //padding spaces if required
				std::cout << " ";
				prefixDiff--;
			}	
			std::cout << contextStrings.first << "|" << wordString << "|" << contextStrings.second << std::endl;
			//std::cout << lineLength-wordString.length()-wordIndent-2<< std::endl;
			++occurrenceIterator;
		}
		++wordIter;
		std::cout << std::endl; //blank line between each word
	}
}
Exemplo n.º 2
0
int main(int argc, const char * argv[]) {
    try {
        setlocale(LC_ALL, "");
        std::locale loc;
        wordMap words;

        // Define vars
        std::string filename = "Erlrouter.txt";
        char tempChar(0);
        std::string tempWord("");
        int lineCounter(1);

        // Open file
        std::ifstream book(filename.c_str());
        if(!book.is_open()) throw std::runtime_error("Die Datei \"" + filename + "\" konnte nicht geöffnet werden!");
        
/*      Get line || get word
        std::string line("");
        std::istream_iterator<std::string> iiter(book);
        std::istream_iterator<std::string> eos;
        for(int i = 0;i<=2;++i)
            std::cout << *iiter++ << std::endl; // Read word
            getline(book, line);
        std::cout << line << std::endl;
*/

        // Go through file and read in char-by-char
        do {
            tempChar = book.get();
            if(tempChar == '\n') lineCounter++;
            if(isspace(tempChar, loc)) {
                // Write word
                if(tempWord.length() > 0) {
                    if(isNomen(tempWord)) {
                        // We have a nomen!
                        addWordToMap(words, *new Nomen(tempWord, lineCounter));
                    } else if(isAkronym(tempWord)) {
                        // We have an acronym!
                        addWordToMap(words, *new Akronym(tempWord, lineCounter));
                    }
                }
                tempWord = "";
            } else if(isalpha(tempChar, loc)) {
                tempWord = tempWord + tempChar;
            }
        } while(book.good());
        book.close(); // Close file
        
        printMap(words);
        
        // Write to Index.txt
        std::string outputFilename("Index.txt");
        std::ofstream index(outputFilename.c_str());
        writeMap(words, index);
        
        return 0;
    } catch(std::exception &e) {
        std::cerr << e.what() << std::endl;
    } catch(...) {
        std::cerr << "Ein unbekannter Fehler trat auf." << std::endl;
    }
}