Write a bad English-to-Spanish translator with c++ language. It will read a file
ID: 3828120 • Letter: W
Question
Write a bad English-to-Spanish translator with c++ language. It will read a file word-by-word, and then use supplied files to replace English words with their “Spanish” equivalents. It is “bad” for two reasons – the files only cover the 1000 most common English words; and the provided translations are very literal and rigid, and so the translations generally will be grammatically incorrect or use different meanings of words. The upside is that it is easy to build.
The program should work as follows: consider the files english.txt and spanish.txt. The first is a list of 1000 common English words, each on a different line; the second is a list of corresponding translations. So, for example, the third line in the English file is about, while the third line in spanish.txt is acerca de – because acerca de is a Spanish translation of about.
Your program should read each of these files into arrays of length 1000 – one should get the 1000 English words, the other with the corresponding Spanish translations in the corresponding order. (Note that the Spanish translations may contain more than one word, so getline() will be your friend here.)
Then, write a function called translate. This function should take an English word as input, and return either the word’s Spanish translation, or the string "???" if the word is not one of the English words in the dictionary. Your function may take more inputs also. Then, test your function! Remember the test code I gave you for the twin prime problem?
Write your own test code here, to make sure that translate is working the way you expect it to. Now, your main function should ask the user to enter the names of an input file to read, and an output file to write the input’s translation to. The program should open the input file, and translate each word, writing the translation to the specified output file. To simplify your task slightly, you may assume that • every word is all lowercase; • every line ends with the characters "!!!"; • aside from that, there is no punctuation.
Specifications: your program must • write a function translate that takes in an English word (possibly with other arguments), and returns either the corresponding Spanish word or "???". • ask the user to enter the names of files for input and output. • read the input file word-by-word, and then print the translations of each word into the output file. Remember that the word "!!!" should become a newline in the translation.
Explanation / Answer
C++ Code:
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;
//store words from file into array words
void copyFileToArray(const char* filename,string words[]);
//return spanishword for given englishword
string translate(string englishWord,string englishWords[],string spanishWords[]);
//translate whole words in inputFile to spanish and write in outputFile
void translateFile(string inputFile,string outputFile,string englishWords[],string spanishWords[]);
int main()
{
string englishWords[1000],spanishWords[1000];
string inputFileName,outputFileName;
copyFileToArray("english.txt",englishWords);
copyFileToArray("spanish.txt",spanishWords);
cout<<"Enter an input file to read (with extension eg:input.txt):";
getline(cin,inputFileName);
cout<<"Enter an output file name (with extension eg:output.txt):";
getline(cin,outputFileName);
translateFile(inputFileName,outputFileName,englishWords,spanishWords);
cout<<"Translated file saved as "<<outputFileName;
return 0;
}
void copyFileToArray(const char* filename,string words[]){
string line,word;
int noOfWords=0;
ifstream infile( filename );
if (infile)
{
while (getline( infile, line ))
{
if(noOfWords<1000&&line!=""){//copy only if noofwords<1000 and word is not empty
words[noOfWords]=line;
noOfWords++;
}
}
infile.close();
}
else cout << "File"<< filename<<" cannot open ";
}
string translate(string englishWord,string englishWords[],string spanishWords[]){
string spanishWord="???";
for(int i=0;i<1000;i++){
if(englishWords[i]=="")//the rest of array is empty (default value) occur when words<1000
return spanishWord;
if(englishWords[i]==englishWord)
return spanishWords[i];
}
return spanishWord;
}
void translateFile(string inputFile,string outputFile,string englishWords[],string spanishWords[]){
string line,word;
ifstream infile( inputFile.c_str() );
if (infile)
{
ofstream outfile;
outfile.open(outputFile.c_str());
while (getline( infile, line ))
{
istringstream iss(line);
while(iss>>word)
{
if(word=="!!!")
outfile<<endl;
else
outfile << translate(word,englishWords,spanishWords)<<" ";
}
}
infile.close();
outfile.close();
}
else cout << "cannot open "<<inputFile<<"!";
}
Sample run result:
english.txt
hello
how
what
when
spanish.txt
helli
howee
whatt tee
whenee
input.txt
when hello what happened !!! when say hello
OUTPUT
output.txt
whenee helli whatt tee ???
whenee ??? helli
Note: 1)expand above files appropriatelly to maximum words of 1000
2)Keep all files in the same directory with the above code
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.