/
FileParser.cpp
87 lines (66 loc) · 2.49 KB
/
FileParser.cpp
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
#include "FileParser.h"
using namespace std;
void pronunciationParser(RhymeBot& rhymeBot, string filename){
ifstream dataFile;
dataFile.open(filename);
string line;
while(!dataFile.eof()) {
getline(dataFile, line);
if(line.size() == 0 || !isalpha(line.at(0))){
continue;
}
vector<string> parts = tokenize(line, " ");
string word = parts.at(0);
vector<Sound> pronounce;
for(vector<string>::iterator it = parts.begin()+1; it < parts.end(); it++){
if((*it) != "") {
Sound thisSound = Sound::getSound(*it);
pronounce.push_back(thisSound);
}
}
if(word.find_first_of("(") != string::npos){
word = word.substr(0,word.find_first_of("("));
}
transform(word.begin(), word.end(), word.begin(), ::tolower);
rhymeBot.insertWithPronunciation(word, pronounce);
}
}
void parsePartsOfSpeech(RhymeBot& rhymeBot, string filename, PART_OF_SPEECH partOfSpeech){
ifstream dataFile;
dataFile.open(filename);
string line;
while(!dataFile.eof()) {
getline(dataFile, line);
if (line.length() != 0 && isalpha(line.at(0)) && line.find_first_of(" ") != string::npos) {
string word = line.substr(0, line.find_first_of(" "));
// use the following line and modify RhymeBot to utilize multi-word parts of speech
// vector<string> words = tokenize(word,"_");
// extract only single words
if(word.find_first_of("_") != string::npos){
continue;
}
rhymeBot.addPartOfSpeech(word, partOfSpeech);
}
}
}
void parseAllPartsOfSpeech(RhymeBot& rhymeBot){
parsePartsOfSpeech(rhymeBot, "data/index.adj", ADJECTIVE);
parsePartsOfSpeech(rhymeBot, "data/index.adv", ADVERB);
parsePartsOfSpeech(rhymeBot, "data/index.noun", NOUN);
// parsePartsOfSpeech(rhymeBot, "data/index.sense", SENSE);
parsePartsOfSpeech(rhymeBot, "data/index.verb", VERB);
}
vector<string>& tokenize(string line, string delimiter){
vector<string>* allSplits = new vector<string>;
string lineCopy = line;
while(lineCopy.size() != 0){
int location = lineCopy.find(delimiter);
if(location == string::npos){
allSplits->push_back(lineCopy);
break;
}
allSplits->push_back(lineCopy.substr(0, location));
lineCopy.erase(0,location + delimiter.size());
}
return *allSplits;
}