Question

Write a C++ program that does the task given (the rest of the infomation is to better explain the task). The dictionary can be reached through this url: "http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict.0.7a". Solve as soon as you can (due in a few hours). Thank you very much.

In this project you are going to implement a linguistic application that uses a pronunciation dictionary for finding words with similar pronunciation Example. You enter a word, and it reports similar-sounding words >donut Pronunciation :DOWİ NAH2 T Identical Add phoneme Remove phoneme: DON T Replace phoneme : DONAT DONATE : DOUGHNUT : DONUTS DONUTSDOUGHNUTS We are going to use The CMU Pronouncing Dictionary as our reference. It is available as a simply formatted plain text file, a direct link to it is: cmudict.0.7a An excerpt from it is shown below: PROGRAM P R OW1 G R AE2 M PROGRAMS PROWİ GRAE2 MZ PROGRAMME P R OW1 G R AE2 M PROGRAMMER P R OW1 G R AE2 M ER0 PROGRAMMERS P R OWI G R AE2 M ER0 Z PROGRAMS P R OW1 G R AE2 M Z PROGRAMS P R OW1 G R AE2 M Z PROGRESS P R AAl G R EH2 S PROGRESS (1) P R AH0 GR EH1 S PROGRESS (2) P R OWO G R EH1 S PROGRESSED P R AH0 G R EH1 S T PROGRESSES P R AAl G R EH2 S AHO Z PUSH-UP P UH1 SH AH2 P PUSH-UPS P UH1 SH AH2 P S In linguistics, a phoneme is a perceptually distinct units of sound that distinguishes one word from another, for example p, b, d, and t in the English words pad, pat, bad, and bat.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: In the following program the alternate pronunciations have been included as well.

Code Screenshots:

//Include the required //header files #include <cstdlib> #include <fstream> #include <iostream> include <string> //Use the standard namespace using namespace std; void splitOnSpace (string s, string &before, string &after); string getProunication(string word) I string ; = for (int i-0; i < word.length ) i++) if ((word [i] >= a) && (word [i] <= z) word [ i ] -char ( ( int ) word [ 1 ] -32 ) ; WW + word[i]; string before; string after = ; std: :ifstream fin (cmudict.0.7a); // open file and read in std: :string line; if (fin.fail()) std::cerr <<File cannot be opened for reading. << std: :endl;Sample Output:

Code To Copy:

//Include the required

//header files.

#include <cstdlib>

#include <fstream>

#include <iostream>

#include <string>

//Use the standard namespace.

using namespace std;

void splitOnSpace(string s, string &before, string &after);

string getProunication(string word) {

    string W = "";

    for (int i = 0; i < word.length(); i++) {

        if ((word[i] >= 'a') && (word[i] <= 'z')) {

            word[i] = char((int)word[i] - 32);

        }

        W = W + word[i];

    }

    string before = "";

    string after = "";

    std::ifstream fin("cmudict.0.7a"); // open file and read in

    std::string line;

    if (fin.fail()) {

        std::cerr << "File cannot be opened for reading." << std::endl;

        exit(1);

    }

    while (getline(fin, line)) {

        splitOnSpace(line, before, after);

        if (W == before) {

            return after;

        }

    }

    fin.close();

    //Return not found if the file was

    //fully traversed but the word wasn't found.

    return "Not found";

}

//Define the function getIdentical().

void getIdentical(string word, string pron) {

    //Convert the word to uppercase.

    string W = "";

    for (int i = 0; i < word.length(); i++) {

        if ((word[i] >= 'a') && (word[i] <= 'z')) {

            word[i] = char((int)word[i] - 32);

        }

        W = W + word[i];

    }

    //Declare the strings to store

    //the word and its pronunciation.

    string dict_word = "";

    string dict_pron = "";

    //Open the file.

    ifstream file("cmudict.0.7a");

    string line;

    //Read the file line by line.

    while (getline(file, line)) {

        splitOnSpace(line, dict_word, dict_pron);

       

        //If the Pronounciation of the current word

        //matches the pronounciaton of the given word.

        //then, display the word.

        if (pron == dict_pron && dict_word != W) {

            cout << dict_word << " ";

        }

    }

    cout << endl;

    file.close();

}

//Define the addphenome()

void addphenome(string word, string pron) {

    //Convert the word to uppercase.

    string W = "";

    for (int i = 0; i < word.length(); i++) {

        if ((word[i] >= 'a') && (word[i] <= 'z')) {

            word[i] = char((int)word[i] - 32);

        }

        W = W + word[i];

    }

    //Define the required string variables.

    string before = "";

    string after = "";

    string before1 = "";

    string after1 = "";

    string add_p;

    //Open the file

    ifstream file("cmudict.0.7a");

    string line;

    int fail = 0;

    //Read the file in a line-by-line manner.

    while (getline(file, line)) {

        splitOnSpace(line, before, after);

       

        //Store the current word.

        add_p = before;

        //Split to get the first phenome of the pronounciation.

        splitOnSpace(after, before, after);

        //Split again to get the first phenome

        // conatins an extra space before it.

        splitOnSpace(after, before, after);

        splitOnSpace(pron, before1, after1);

        splitOnSpace(after1, before1, after1);

        fail = 0;

        //Comapre the phenomes of the two word one by one.

        //If the pronounciation of the current word has

        //only one extra phenome then display the current word.

        

        while (after != "" || before != "") {

            if (before == before1) {

                splitOnSpace(after1, before1, after1);

                if (before1 == "") {

                    while (after != "") {

                        splitOnSpace(after, before, after);

                        fail = fail + 1;

                    }

                    splitOnSpace(after, before, after);

                } else {

                    splitOnSpace(after, before, after);

                }

          } else {

                fail++;

                splitOnSpace(after, before, after);

                if (fail == 1 && before == "") {

                    fail = 2;

                    break;

                }

            }

        }

        //Reset the string values.

        before = "";

        after = "";

        //If there was a difference of only one

        //phenome in the pronounciation then,

        //display the word.

        if (fail == 1 && after1 == "") {

            cout << add_p << " ";

        }

       

    }

   

    cout << endl;

    //Close the file.

    file.close();

}

void reomvephenome(string word, string pron) {

    //Convert the word to uppercase.

    string W = "";

    for (int i = 0; i < word.length(); i++) {

        if ((word[i] >= 'a') && (word[i] <= 'z')) {

            word[i] = char((int)word[i] - 32);

        }

        W = W + word[i];

    }

    //Declare the

    //required variables.

    string dict_word = "";

    string dict_pron = "";

    string line;

    string before = "";

    string after = "";

    string trav = "";

    string curr_pron = "";

    string temp_pron = pron;

    splitOnSpace(temp_pron, before, after);

    //Remove a phenome 1 by 1 and check if

    //a word can be formed from the

    //obtained pronounciation.

    while(after != "")

    {

      splitOnSpace(after, before, after);

      trav = trav+" "+ before;

      curr_pron = curr_pron + " " + after;

     

      //Open the file and check the

      //pronounciation of all the words.

      ifstream file("cmudict.0.7a");

     

      while (getline(file, line)) {

        splitOnSpace(line, dict_word, dict_pron);

        if (curr_pron == dict_pron) {

            cout << dict_word << " ";

        }

    }

    temp_pron = after;

    curr_pron = trav;

    file.close();

    }

   

    cout << endl;

   

}

void replacephenome(string word, string pron)

{

//Convert the word to uppercase.

    string W = "";

    for (int i = 0; i < word.length(); i++) {

        if ((word[i] >= 'a') && (word[i] <= 'z')) {

            word[i] = char((int)word[i] - 32);

        }

        W = W + word[i];

    }

    //Define the required string variables.

    string before = "";

    string after = "";

    string before1 = "";

    string after1 = "";

    string add_p;

    //Open the file

    ifstream file("cmudict.0.7a");

    string line;

    int fail = 0;

    //Read the file in a line-by-line manner.

    while (getline(file, line)) {

        splitOnSpace(line, before, after);

       

        //Store the current word.

        add_p = before;

        //Split to get the first phenome of the pronounciation.

        splitOnSpace(after, before, after);

        //Split again to get the first phenome

        // conatins an extra space before it.

        splitOnSpace(after, before, after);

        splitOnSpace(pron, before1, after1);

        splitOnSpace(after1, before1, after1);

        fail = 0;

        while(after !="" || before !="")

        {

          if(before != before1)

          {

            fail = fail + 1;

          }

          if(fail > 1)

          {

            break;

          }

         

          splitOnSpace(after, before, after);

          splitOnSpace(after1, before1, after1);

          if(after == "" || after1 == "")

          {

            break;

          }

        }

        if(fail <= 1 && after1 == "" && after == "")

        {

          if(fail == 0 && add_p != W)

          {

              cout<<add_p<<" ";

          }

          else if(fail == 1 && before == before1)

          {

            cout<<add_p<<" ";

          }

         

        }

    }

    cout << endl;

    //Close the file.

    file.close();

}

void splitOnSpace(string s, string &before, string &after) {

    // this fucntion helsp split my string into parts. Ex: "Fortune favors the

    // bold" ? "Fortune" and "favors the bold".

    // reset strings

    before = "";

    after = "";

    // accumulate before space

    int i = 0;

    while (i < s.size() && !isspace(s[i])) {

        before += s[i];

        i++;

    }

    // skip the space

    i++;

    // accumulate after space

    while (i < s.size()) {

        after += s[i];

        i++;

    }

}

//Define the main() function.

int main() {

    //Declare the string to

    //store the input word.

    string word;

   

    //Declare the string to store the

    //obtained pronunciation of the word.

    string pron;

    cout << "Enter the word: ";

    cin >> word;

   

    //Call the function getProunication() to get

    // the pronunciation of the current

    // word from the file.

    pron = getProunication(word);

    //If the word wasn't found in the file

    //then display the error message

    //and end the program.

    if(pron=="Not found")

    {

      cout<<pron;

        exit(0);

    }

    //Display the pronunciation.

    cout << "Pronounciation :"

    << pron << endl;

    //Display the identical

    //words and the add_phenome words.

    cout << "Identical :";

    getIdentical(word, pron);

    cout << "Add phenome :";

    addphenome(word, pron);

    cout << "Remove phenome: ";

    reomvephenome(word, pron);

    cout<<"Replace phenome: ";

    replacephenome(word, pron);

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that does the task given (the rest of the infomation is to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT