Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Please keep in mind to use basic constructs (loops, void functions, referenc

ID: 3197116 • Letter: C

Question

C++

Please keep in mind to use basic constructs (loops, void functions, references, anything can be used, etc)

We have not learned structures and classes and stuff like that.

Write a program pronounce that Lets the user input a word (let’s call the input word W).

We are going to use The CMU Pronouncing Dictionary( http://www.speech.cs.cmu.edu/cgi-bin/cmudict ) as our reference. It is available as a simply formatted plain text file, a direct link to it  .0.7a (http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict.0.7a). It is required to utilize this file in the program (open, read it )

If the word is not found in the dictionary, print “Not found”. Otherwise, report:

Pronunciation: the pronunciation of the word W (as given in the dictionary),

Example:

Example:
"Fortune favors the bold" "Fortune" and "favors the bold".

Please tell me a C++ function that fulfills the prompt and if the helper function (given above) is used please utilize it and call the function.

Thank you for your help.

Explanation / Answer

From your question, it seems that you need to find multiple words in a single run of program. So it will be ineffecient to travel whole dictionary multiple times. time complexity of this algorithm will be O(nk) where n is words in dictionary and k is no of words has to be find. access time to hard disk is also more than RAM. So you first you save your dictionary in ram (In some kind of data structure e.g. array) and then search for your word. I used map which is a container from standard template library. it gives fast search time (O(klogn)). following is my code for the task you can manipulate it according to your needs.

------------------------------

#include <iostream>

#include <algorithm>

#include <fstream>

#include <map>

using namespace std;

map<string,string> dict;

int main(){

ifstream file;

file.open("cmudict.0.7a.txt");

if(!file.is_open()) return -1;

while(!file.eof()){

string key,val;

file>>key;

char c;

file.get();

while(!file.eof()&&(c=file.get())!=' '){

val.push_back(c);

   }

if(key==";;;") continue;

if(!isalpha(key[0])) dict[string(1,key[0])]=val;

dict[key]=val;

}

string word;

cin>>word;

transform(word.begin(),word.end(),word.begin(),::toupper);

if(dict.find(word)==dict.end()) cout<<"Not found";

else cout<<"Pronounciation : "<<dict[word];

file.close();

return 0;}

-----------------------------------

Below is the full version of program which takes input as a santance and output its pronunciation.

----------------------------------

#include <iostream>

#include <vector>

#include <algorithm>

#include <fstream>

#include <map>

using namespace std;

map<string,string> dict;

int main(){

ifstream file;

file.open("cmudict.0.7a.txt");

if(!file.is_open()) return -1;

while(!file.eof()){

string key,val;

file>>key;

char c;

file.get();

while(!file.eof()&&(c=file.get())!=' '){

val.push_back(c);

   }

if(key==";;;") continue;

if(!isalpha(key[0])) dict[string(1,key[0])]=val;

else dict[key]=val;

}

string str,buff;

vector<string> pstr;

getline(cin,str);

for(int i=0;i<str.size();i++){

if(str[i]==' '){

pstr.push_back(buff);

buff.clear();

}else buff.push_back(str[i]);

}

pstr.push_back(buff);

for(string word:pstr){

transform(word.begin(),word.end(),word.begin(),::toupper);

if(dict.find(word)==dict.end()){cout<<"Not found"; break;}

else cout<<dict[word]<<' ';

}

cout<<endl;

file.close();

return 0;}

-------------------------------

Note: your helping code skip space only it does not include case line "name." that '.' is accumulated in nameand it will not found the word in dict.

Edit: Modified code with use of basic constructs

------------------------------------------

#include <iostream>

#include <algorithm>

#include <stdio.h>

#include <fstream>

using namespace std;

int main(int argc, char* argv[])

{

    ifstream file;

    file.open("cmudict.0.7a.txt");

    if(!file.is_open()) return -1;

    string word;

    cin>>word;

    transform(word.begin(),word.end(),word.begin(),::toupper); //if transform is not allowed comment this line and uncomment next line

    //for(int i=0;i<word.size();i++) word[i]=toupper(word[i]);

    while(!file.eof()){

    string key,val;

    file>>key;

    getline(file,val);

    if(key==";;;") continue;

    if(!isalpha(key[0])&&(key[0]==word[0])||key==word){

        cout<<"Pronounciation : "<<val;

        return 0;}

    }

    cout<<"Not found ";

    file.close();

return 0;

}

----------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote