-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
152 lines (145 loc) · 5.16 KB
/
Copy pathParser.cpp
File metadata and controls
152 lines (145 loc) · 5.16 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// Created by Sammy Timmins on 9/22/20.
//
#include <map>
#include "Parser.h"
#include "DSString.h"
#include "Entry.h"
void addToEntries(int &pageNumber, DSString &word, DSLinkedList<Entry> &entries) //adds a word that is not a sub entry to the index
{
Entry indexed;
indexed = word;
int index = entries.search(indexed);
if(index == -1) //if entry is not already in the entries vector it adds it
{
indexed.addPage(pageNumber);
entries.add(indexed);
} else //if entries is in the entries vector it adds the page number to the entry
{
entries[index].addPage(pageNumber);
}
}
void addChildToIndex(int &pageNumber, Entry &child, DSLinkedList<Entry> &entries)
{
child.addPage(pageNumber);
entries.add(child);
}
void addChildToEntry(int &pageNumber, DSString &word, DSString &parent, DSLinkedList<Entry> &entries) //adds a subentry to the index
{
Entry indexed, child;
indexed = parent;
child = word;
int index = entries.search(indexed);
if(index == -1) //if entry is not already in the entries vector it adds it
{
int checkChild = entries.search(child);
if(checkChild == -1) //if the child has not been added to the index yet
{
addChildToIndex(pageNumber, child, entries);
}
else //if the child has been added to the index
{
entries[checkChild].addPage(pageNumber);
}
indexed.addChild(child);
entries.add(indexed);
} else //if entries is in the entries vector it adds the page number to the entry
{
int checkChild = entries.search(child);
if(checkChild == -1) //if the child has not been added as an entry yet
{
addChildToIndex(pageNumber, child, entries);
checkChild = entries.search(child);
}
else //if the child has been added as an entry
{
entries[checkChild].addPage(pageNumber);
}
entries[index].addChild(entries[checkChild]);
}
}
void parse(ifstream &inputFile, DSLinkedList<Entry> &entries)
{
char buffer[80];
DSString pageNumberString;
int pageNumber;
while(inputFile.getline(buffer, 80)) //repeats until end of file
{
DSString line = buffer;
if(line[0] == '<')
{
pageNumberString = "";
for(int i = 1; line[i] != '>'; i++) //grabs the page number from between the <>
{
pageNumberString = pageNumberString + line[i];
}
pageNumber = pageNumberString.dsstoi();
}
else
{
for(int i = 0; i < line.getLength();)
{
DSString word, parent;
if(line[i] != '[') //checks if first in line is a phrase
{
while((line[i] != ' ') && (line[i] != '(') && (i != line.getLength())) //adds characters to word until the end of the word
{
word = word + tolower(line[i]);
++i;
}
if((line[i] == ' ') || (i == line.getLength())) //adds indexed word that is not a sub entry to the entries vector
{
addToEntries(pageNumber, word, entries);
++i;
} else if (line[i] == '(')
{
if(line[i + 1] != '[') //handles if the parent is a phrase or keyword
{
++i;
}
else
{
i += 2;
}
while(line[i] != ')')
{
parent = parent + line[i];
++i;
}
addChildToEntry(pageNumber, word, parent, entries);
}
} else if(line[i] == '[') //if the the thing to be indexed is a phrase
{
++i;
while((line[i] != ']'))
{
word = word + tolower(line[i]);
++i;
}
if((line[i] == ']') && (line[i + 1] == '(')) //if the indexed phrase is a child of a parent entry
{
if(line[i + 2] != '[') //handles if the parent is a phrase or a keyword
{
i += 2;
}
else
{
i += 3;
}
while(line[i] != ')')
{
parent = parent + line[i];
++i;
}
addChildToEntry(pageNumber, word, parent, entries);
}
else if(line[i] == ']') //if the phrase is not a sub entry
{
addToEntries(pageNumber, word, entries);
++i;
}
}
}
}
}
}