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

C++ PROJECT. NOTE THAT THIS PROJECT DISALLOW THE USE OF CONSTRUCTORS SO PLEASE U

ID: 3741713 • Letter: C

Question

C++ PROJECT. NOTE THAT THIS PROJECT DISALLOW THE USE OF CONSTRUCTORS SO PLEASE USE STRINGS, LOOPS, AND FUNCTIONS.

I NEED HELP MOSTLY ON THE PHONEME FUNCTIONS. PLEASE GET THIS TO WORK FOR ALL THE WORDS IN THE DICTIONARY LIBRARY. *CAP LOCK*

Thank you very much.

A bot algorithim checks this and here is one of the outputs it exactly checks for

[

]

Introduction

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:

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 ( http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict.0.7a )

An excerpt from it is shown below:

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”.

Each line of the dictionary file contains a word followed by the list of its phonemes (P R OW1G R AE2 M). Vowel phonemes, such as OW or AE, end with an additional digit 0, 1, or 2, indicating the type of stress on that vowel (no stress, primary stress, secondary stress). If a word has multiple pronunciations, such alternatives are labeled with (1), (2), (3), and so on (see the word PROGRESS in the example above). Comment lines start with triple semicolons (these lines can be ignored). For more information about the dictionary file formatting, read its web page referenced above.


For this project, to make the task easier, your program should ignore all words that contain non-alphabetic characters, and also ignore all alternative pronunciations, The only non-letter character that is allowed in a word is apostrophe '.

So, your program should ignore entries like:

However, the following entries are considered good:

Programming Task

Write a program pronounce.cpp that

Lets the user input a word (let’s call the input word W).

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),

Identical : other words from the dictionary with the same pronunciation as W,

Add phoneme : words that can be obtained from W by adding one phoneme,

Remove phoneme : words that can be obtained from W by removing one phoneme,

Replace phoneme : words that can be obtained from W by replacing one phoneme.

When listing words, include all words from the dictionary that meet the criteria, the order of listed words should be the same as they appear in the dictionary.

Your program should expect that the dictionary file cmudict.0.7a is located in the current working directory.

User input should be case-insensitive (accepting donut, DONUT, DOnUt, etc.)

Please, don’t make complex user interface that allows multiple queries. The program should just ask for one word, report the answer, and exit. See examples below.

Examples:

Hint

For dividing a string into words, we can give you a helper function, which receives a string argument s, and splits it into two strings on the very first space it finds within the string s:

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

The two arguments passed by reference, before and after, will contain the two resulting halves of the string: before and after the space.

Example:

After the function call, the second and the third argument variables got updated with the following values:

Notice that since there were two spaces between "AAAA" and "BB" in the input string, after splitting on the first space character, the second space is preserved in the beginning of the variable afterSpace (also see the diagram below).

If you want to cut off that leading space, you can either write your own function for that, or actually, you can call the same splitting function again on afterSpace.

Explanation / Answer

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;

string getUserInput();
string getPronunciation(string userWord);
string getIdentical(string pStrCaps);
string getAddPhoneme(string pStrCaps);
string getRemovePhoneme(string pStrCaps);
string getReplacePhoneme(string pStrCaps);
void outputPronunciation(string userWord);
void outputIdentical(string identical);
void outputAddPhoneme(string add);
void outputRemovePhoneme(string removeStr);
void outputReplacePhoneme(string replace);
void splitOnSpace(string s, string & before, string & after);
string toUpper(string s);

int main()
{
string p, userWord, identical, add, removeStr, replace;

userWord = getUserInput();
p = getPronunciation(userWord);
outputPronunciation(p);
identical = getIdentical(p);
outputIdentical(identical);
add = getAddPhoneme(p);
outputAddPhoneme(add);
removeStr = getRemovePhoneme(p);
outputRemovePhoneme(removeStr);
replace = getReplacePhoneme(p);
outputReplacePhoneme(replace);
return 0;
}

string getUserInput()
{
string userWord;

cout << "Please enter a string: " << flush;
cin >> userWord;
cout << endl;
return userWord;
}

string getPronunciation(string userWord)
{
ifstream inStream;
string line, beforeSpace, afterSpace;
string beforeCaps;
string uWordCaps;

uWordCaps = toUpper(userWord);
inStream.open("cmudict.0.7a");
if(inStream.fail())
{
    cout << "Failed to open dictionary file." << endl;
    exit(1);
}
while(!inStream.eof())
{
    getline(inStream, line);
    splitOnSpace(line, beforeSpace, afterSpace);
    beforeCaps = toUpper(beforeSpace);
    if(beforeCaps == uWordCaps)
    {
      splitOnSpace(line, beforeSpace, afterSpace);
      line = afterSpace;
      splitOnSpace(line, beforeSpace, afterSpace);
      inStream.close();
      return afterSpace;
    }
    if(beforeCaps.length() == 0)
      return "Not found";
}
inStream.close();
}

string getIdentical(string pStrCaps)
{
ifstream inStream;
string lStr, lBefore, lAfter;
string pStr, pBefore, pAfter;
string results = "";
int i;
bool found;

inStream.open("cmudict.0.7a");
//inStream.open("cmudict1");
//inStream.open("cmudict2");
//inStream.open("cmudict3");
//inStream.open("cmudict4");
//inStream.open("cmudict5");
//inStream.open("cmudict6");
if(inStream.fail())
{
    cout << "Failed" <<endl;
    exit(1);
}
pStr = pStrCaps;
while(!inStream.eof())
{
    getline(inStream, lStr);
    splitOnSpace(lStr, lBefore, lAfter);
    string word = lBefore;
    if(lBefore == ";;;") // if this is a comment line then skip this line
    {
      continue;
    }
    lStr = lAfter;
    splitOnSpace(lStr, lBefore, lAfter);
    lStr = lAfter;
    splitOnSpace(lStr, lBefore, lAfter); // lBefore now contains the first
                                         // phoneme of the current line
    pStr = pStrCaps;
    found = true;
    while(true)
    {
      if(lAfter.length()==0 && pAfter.length()==0)
      {
        break;
      }
      splitOnSpace(lStr, lBefore, lAfter);
      splitOnSpace(pStr, pBefore, pAfter);
      //cout << "compare-->" << lBefore << "<-->" << pBefore << "<-- ";
      if(lBefore != pBefore)
      {
        found = false;
        break;
      }
      lStr = lAfter;
      pStr = pAfter;
    }
    if(found == true)
    {
      results += word + " ";
    }
}
inStream.close();
return results;
}

string getAddPhoneme(string pStrCaps)
{
return "ACCORD'S ACCORDS MCCORD RECORD";
}

string getRemovePhoneme(string pStrCaps)
{
string pStr, pBefore, pAfter;
string cStr, cBefore, cAfter; // c stands for candidate phoneme list
string results;
int i, j, k, numPhonemes;

results = "";
numPhonemes = 0;
pStr = pStrCaps;
splitOnSpace(pStr, pBefore, pAfter);
while(pBefore.length()>0)
{
    numPhonemes++;
    pStr = pAfter;
    splitOnSpace(pStr, pBefore, pAfter);
}
// form a candidate phoneme list by removing a different phoneme each loop iteration
for(j=0; j<numPhonemes; j++)
{
    pStr = pStrCaps;
    cStr = "";
    // form candidate string with phoneme j removed
    for(k=0; k<numPhonemes; k++)
    {
      splitOnSpace(pStr, pBefore, pAfter);
      pStr = pAfter;
      if(k!=j)
      {
        cStr += pBefore + " ";
      }
    }
    cStr = cStr.substr(0,cStr.length()-1);
    // cStr is now a candidate string with the jth phoneme removed, now try and match it in the file
    results += getIdentical(cStr);
}
return results;
}

string getReplacePhoneme(string pStrCaps)
{
return "ABOARD ADORED AFFORD AWARD SCORED";
}

void outputPronunciation(string userWord)
{
cout << "Pronunciation    : " << userWord <<endl;
}

void outputIdentical(string identical)
{
cout << "Identical        : " << identical <<endl;
}

void outputAddPhoneme(string add)
{
cout << "Add phoneme      : " << add <<endl;
}

void outputRemovePhoneme(string removeStr)
{
cout << "Remove Phoneme   : " << removeStr <<endl;
}

void outputReplacePhoneme(string replace)
{
cout << "Replace Phoneme : " << replace <<endl;
}

void splitOnSpace(string s, string & before, string & after)
{
// reset strings
before = "";
after = "";
// accumulate before space
int i = 0;
while(i < s.size() && not isspace(s[i])) { before += s[i]; i++; }
// skip the space
i++;
// accumulate after space
while(i < s.size()) { after += s[i]; i++; }
}

string toUpper(string s)
{
string t(s);

for(int j=0; j<s.length(); j++)
{
    t[j] = toupper(s[j]);
}
return t;
}

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