Repository navigation
Expand file tree
/
Copy pathtxt_parser.cpp
More file actions
69 lines (60 loc) · 1.78 KB
/
Copy pathtxt_parser.cpp
File metadata and controls
69 lines (60 loc) · 1.78 KB
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
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cctype>
#include "txt_parser.h"
#include "util.h"
using namespace std;
// Complete
void TXTParser::parse(std::istream& istr, std::set<std::string>& allSearchableTerms, std::set<std::string>& allOutgoingLinks)
{
// Remove any contents of the sets before starting to parse.
allSearchableTerms.clear();
// No links are present in a text parser
allOutgoingLinks.clear();
// Initialize the current term and link as empty strings.
string term = "";
// Get the first character from the file.
char c = istr.get();
// Continue reading from the file until input fails.
while(!istr.fail())
{
// Is c a character to split terms?
if (!isalnum(c))
{
// If we have a term to add, convert it to a standard case and add it
if(term != "")
{
term = conv_to_lower(term);
allSearchableTerms.insert(term);
}
term = "";
}
// Otherwise we continually add to the end of the current term.
else
{
term += c;
}
// Attempt to get another character from the file.
c = istr.get();
}
// Since the last term in the file may not have punctuation, there may be a valid term in
// the "term" variable, so we need to insert it into the allSearchableTerms set.
if(term != "")
{
term = conv_to_lower(term);
allSearchableTerms.insert(term);
}
}
// Complete
std::string TXTParser::display_text(std::istream& istr)
{
std::string retval;
char c = istr.get();
// Continue reading from the file until input fails.
while (!istr.fail()) {
retval += c;
c = istr.get();
}
return retval;
}