Question
Can Someone help me fix this code. The objective of the program is to read a file, and then process this file to identify all
Can Someone help me fix this code. The objective of the program is to read a file, and then process this file to identify all the co-occurring word-pairs and their frequencies. Two words are said to co-occur if they appear in the same sentence. We will refer to a pair of co-occurring words as word-pairs. The frequency of a word-pair is defined as the number of sentences that consist of this word-pair. You are required to include three files in this project:
There are three files in the code
FileIOsWordPair.h
#include "fileIOs_wordPairs.h"
using namespace std;
// string fname = "SteveJobsSpeech2005.txt";
bool sentenceSplitter(string& fname, vector& sentence) {
fstream file;
file.open(fname.c_str(), ios::in | ios::out);
if (file.fail()) {
cout << "Failed to open file: " << fname << endl;
return false;
}
string token;
stringstream iss;
while (!file.eof()) {
while (getline(file, token, '.') || getline(file, token, '?')) { //CHANGE: we need to account for ." and ?"
if (isspace(token.at(0)) != 0) { //
//strips blank space from in front of a string
//NOTE: maybe make an inline function to handle this part
string temp;
int i = 0;
int len = token.length() - 1;
while (isspace(token.at(i)) != 0) {
i++;
}
temp = token.substr(i, len);
sentences.push_back(temp);
} else {
if (token.at(0) == '"') {
if (token.size() > 1 && isspace(token.at(1))) {
int i = 1;
int len = token.length() - 1;
string temp;
while (isspace(token.at(i)) != 0) {
i++;
}
temp = token.substr(i, len);
sentences.push_back(temp);
}
} else {
sentences.push_back(token);
}
}
}
}
// while (getline(file, token)) {
// iss << token;
// const string temp = iss.str();
// char* tmp = (char*) temp.c_str();
// char * point;
// point = strtok(tmp, ".?");
// printf("%s ", point);
// }
return true;
}
bool wordpairMapping(vector& sentences,
map, int> &wordpairFreq_map) {
// istringstream iss;
string token;
string token2;
pair wordpair;
if (sentences.empty()) { //if sentences is empty, there are no word pairs to find
return false;
}
for (unsigned int i = 0; i < sentences.size(); i++) {
istringstream iss(sentences.at(i));
while (getline(iss, token, ' ')) {
while (getline(iss,token2,' ')) {
transform(token.begin(), token.end(), token.begin(), ::tolower); //convert both to lower case
transform(token2.begin(), token2.end(), token2.begin(), ::tolower);
if (token.compare(token2) != 0) { //if the two tokens aren't the same, then we move on
if (token < token2) { //check if first word is first alphabetically
wordpair = make_pair(token,token2);
}
else {
wordpair = make_pair(token2,token);
}
// if (wordpairFreq_map.find(wordpair) == wordpairFreq_map.end()) { //check if the word pair is already in the map
// wordpairFreq_map.insert(pair,int>(wordpair,1));
// }
// else {
// wordpairFreq_map[wordpair] += 1;
// }
wordpairFreq_map[wordpair]++; //apparently this works
}
}
}
}
return true;
}
bool freqWordpairMmap(map, int> &wordpairFreq_map,
multimap > &freqWordpair_mmap) {
return false;
}
void printWordpairs(multimap > &freqWordpair_multimap,
string outFname, int topCnt, int botCnt) {
}
Main file
# include "fileIOs_wordPairs.h"
# include
# include
# include
#include
using namespace std;
int main (){
bool fileOpened; //tells whether we successfully opened the file or not
string filename = "SteveJobsSpeech2005-full.txt"; //you name the file you want to open here, i used my own file for the input
vector sentences; //you also need to create a vector to contain the sentences
fileOpened = sentenceSplitter(filename, sentences); //the actual method call
if (fileOpened) { //we skip the next part if the file opening failed
for (unsigned int i = 0; i < sentences.size(); i++) { //this for loop prints out all the items in sentences
cout << i << ": " << sentences.at(i) << endl; //and tells me at which place each item is
}
}
string filename2 = "spaghetticode.txt"; //i used another file to test for question marks
vector sentencesElectricBoogaloo;
fileOpened = sentenceSplitter(filename2,sentencesElectricBoogaloo);
if (fileOpened) {
for (unsigned int i = 0; i < sentencesElectricBoogaloo.size(); i++) {
cout << i << ": " << sentencesElectricBoogaloo.at(i) << endl;
}
}
return 0;
}
And header file
/* * Created on: Nov 9, 2017 * Created by: Harold Pedroso * * * * * */ #ifndef FILEIOS_WORDPAIRS_H_ #define FILEIOS_WORDPAIRS_H_
#include
using namespace std;
bool sentenceSplitter( string& fname, vector
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started