467,879 Members | 1,331 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,879 developers. It's quick & easy.

i want to create a program that translates a sentance in english to another language

I want to help teach to a minority group in Milwaukee, so I want to create a dictionary program that translates a sentence (like a homework problem or teacher instructions), from English into Hmong.

I have one Idea, and that is to have an English text file of all the A-words, then a Hmong text file of the proper translations. Each text file would have the same amount of lines, with one english word per line, then translated to hmong. Would it be even possible to..

1. user inputs question, "Describe the holocaust"
2. describe is looked in the d text (like if d, search d.txt), then the word is found. then it either compares it to a translation txt (like hmong-d.txt), spitting out the word. then it would prompt to the next word to translate, one line at a time.

A different way was to have 1 giant text file for each letter. like look up "albino", when it finds the word in the a a-dictionary, it searches for the albino string, then finds the comma, then spits out the word/words after the comma. would this be possible as well? which method is easiest for someone that hasn't c++ in a few years?

so far I have a useless code typed up. i just want to start simple getting a-words to work, then get more complex from there. once i have a start i can go with it. please help me, i would love to reach the needs of my esl students who could use this greatly in the US.

summary,i want to take a line of words, compare each word to a specific dictionary, then output one line at a time, the translation. thank you so much!

this code is not correct:

#include <iostream> // I/O
#include <fstream> // file I/O
#include <iomanip> // format manipulation
#include <string>

//This program will hopefully translate english to hmong
using namespace std;
string english; //declare english word to translate
string search; //what to search

int main()
{

cout << "Type the english words here, then press enter. Make sure to copy the words correctly. If you need to spell check use www.google.com";
cin >> english;

ifstream myFile;
myFile.open ("c:\\eAh.txt");

string* search = english; //search using input
int offset; // where it was found (or not (-1))
if ((offset = line.find(search, 0)) != string::npos) {
cout << "found '" << search << "' @ offset " << offset << endl;
}

return 0;
}
May 22 '07 #1
  • viewed: 6448
Share:
91 Replies
DeMan
1GB
Quick couple of questions....
Is this code compiling?
If not, what error messages do you get?
If so, how do you know it isn't working (ie does it crash, or does it give bad ouput and if so what is the output)?
May 22 '07 #2
ilikepython
Expert 512MB
A different way was to have 1 giant text file for each letter. like look up "albino", when it finds the word in the a a-dictionary, it searches for the albino string, then finds the comma, then spits out the word/words after the comma. would this be possible as well? which method is easiest for someone that hasn't c++ in a few years?
I think that that is the easiest way to do it instead of having to look through two different files for each word. How big do you expect these files to get?

Expand|Select|Wrap|Line Numbers
  1. string* search = english; //search using input
  2. int offset; // where it was found (or not (-1))
  3. if ((offset = line.find(search, 0)) != string::npos) {
  4. cout << "found '" << search << "' @ offset " << offset << endl;
  5.  
You never defined "line" and I dont think that you need a search variable.
May 22 '07 #3
DeMan
1GB
retract that........
May 22 '07 #4
hmm... how should i define line? i kind of found this on the internet, where they didn't define the line function. can anyone help with that?

i basically will have 100 words in a textfile for the letter a... (or 200, if it includes both languages seperated by , commas)

so i want it to search through a.txt for "apple" then find apple, and state

apple = koj thoj (made that up)

hmm... maybe i could replace the ,'s in my .txt files to have " = " instead, so it can simply just output the line? that would simplify my code.

so... search textfile of a words, find "apple", then stop when it finds the space (so if it finds a word like applelot (made up) it doesnt create a false positive, then prints out apple = koj thoj.

ps: i'll make sure to give you guys credit in the code. this is a freeware program for students, i want them to have access to this stuff !
May 22 '07 #5
DeMan
1GB
I believe the line is a string.....
While you could read the whole file into the string, you could also consider parsing the file line by line.
Provided each of your line has similar structure (and starts with the word to translate), and assuming that these are listed alphabetically, you could try to implement a binary search to speed the effort up as the file gets large (which it will if you plan to store an entire dictionary)

(I'm pretty sure a file has a getline function (or similar))
May 22 '07 #6
any examples?

i need an example for the function, and what to do with the input.

should i have my text be one text file for a-english words, or two text files with one with english and another with the same amount of lines/line numbers for the different language?

if one textfile for a-words, should it be...

apple = koj thao

apple, koj thao

with the first example, i could simply return the entire line which would help my students. so if i had several dictionaries it would be like this

****
input your sentence in english: I love you.

i = kuv
love = hlub
you = koj

would you like to translate more words? y/n

if i could get a head start, it would help if i could at least get an example of taking an input that is a string, one word, then searching it in a text file, once it is found in the text file, spit it back out with something like love = hlub. the text file will be alphabetical even if it is tedious for me. that way in the future i could increase efficiency.

from here i hope i can figure out how to search multiple words in multiple textfiles using the if statements and your guy's help with the function... so eventually the search can occur in a generic file like trans[a].txt or trans[b].txt where the letter for the dictionary would be based on the first letter of the english word from the cin input. that means eventually i would need to create a string for each english word, and store it temporarily, then compare each one and spit it out one at a time. or in a simple way, word one>function>spit out, word two>function>spit out

first few lines from my a .txt file:

Abdomen = Plab mog
Able = Rooj
Able = Ua tau
Abortion = Rho menyuam tawm
About = Li ntawm
Above = Saum toj
May 22 '07 #7
the description here is similiar in context, but it would not compile for me:(

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=7536&lngWId=3#zip
May 22 '07 #8
DeMan
1GB
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main () {
  7.   string line;
  8. cout << "Type the english words here, then press enter. Make sure to copy the words correctly. If you need to spell check use www.google.com";
  9. cin >> english;
  10.  
  11.   ifstream myFile ("c:\\eAh.txt");
  12.   if (myFile.is_open())
  13.   {
  14.     while (! myFile.eof() )
  15.     {
  16.       getline (myFile,line);
  17.       if(strncmp(line, english, englis.length())
  18.       {
  19.          cout <<"Match found"<< line;
  20.       }
  21.     }
  22.     myFile.close();
  23.   }
  24.  
  25.   else cout << "Unable to open file"; 
  26.  
  27. }
would be one way to try (assuming the english word is the first item in a row of the file you are trying to parse)
May 22 '07 #9
well that is a start.. but a cin>> just restates the entire .txt file dictionary...

anything i could put at the end of a line, to indiciate... stop typing?

maybe an if statement so it only says one word.... then in my txt file have a period? so that at the end of the sentance like

love = hlub.

the line code stops printing out lines? or some better looking character ?
May 22 '07 #10
DeMan
1GB
I thought you were entering texct on teh command line? Is this not the case?
You could also use getline as in the example for the Dicationary file(s) if you want input from a file
May 22 '07 #11
the function you had did not exhist... but i removed it and it works but no matter what you cin/enter for input it spits out the entire text which is better then nothing! hehe.

this part did not compile because of strncmp

if(strncmp(line, english, englis.length())


what i want is... users to insert a sentance... but for now, a word. like "apple". then have it say... hmm... apple starts with a, look at a.txt (a dictionary, or if i have to, one super dictionary of a-z)... once it finds the word apple followed by a space = sign " = ", it would then spit out the word apple = koj (or however it is in the other language)... so it will say the word in english then = then hmong... so love = hlub, so it would search l dictionary (i can figure out if's later, if not a super dictionary a-z), find love, stop then print

love = hlub ...

it would then end line, and go onto the next word to translate.. until finished with the sentance. however at this stage i just want it to find a word, and spit it out with the translation.

example:
"input your sentance to be translated" love

love = hlub


how do i make the stuff stop printing after it prints the line i need? do i end it after a endl; ? or is there a different way? thanks for your help! i feel like were on the right track. i found some cool c examples but c doesnt compile well on my comp, c++ you wrote is a good start for me
May 22 '07 #12
DeMan
1GB
strncmp(a, b, c) - compare string a with string b over c characters - english is misspelt in my example it should be
if(strncmp(line, english, english.length())
May 22 '07 #13
weaknessforcats
Expert Mod 8TB
You should use an association table. That is the only way to resolve a many-to-many relationship.

In C++ this would be a multimap. One for English/Hmong and another for Hmong/English.

You should not have to write a lot of code.

For example, using Spanish as an example, submitting "Hello" as an English key may have several Spanish equivalents. Use iterators with the equal_range algorithm to determined the low and high iterator value for "Hello". Then simply traverse the range to get your possible choices:

Hola
Buenos dias
Como esta usted?
etc...
May 22 '07 #14
i tried this instead, but i imagine it might get rather large and tidious. is there a way to move the dictionary from internal the program to a .txt file?

#include <map>
#include <string>
#include <iostream>

using namespace std;

string pause; // pause the program

int main()
{
map<string,string> dictionary;

// examples of adding to the dictionary
dictionary["I"] = "kuv";
dictionary["love"] = "hlub";
dictionary["you"] = "kij";

// searching the dictionary
string translateme; //user input

cout<<"Please enter the English words / sentance to be translated to Hmong:" << endl;
cin>>translateme;

string s = translateme;

map<string,string>::iterator it = dictionary.find(s);

if (it != dictionary.end())
{
cout << s << " = " << it->second << "\n";
}
else
{
cout << s << " not found in the dictionary\n";
}

cin >>pause;
return 0;
}


what i need to do now, is add words. but i also then need to turn your if statements into a function that will translate each individual word. so i need to make it stop after the space, translate... then move onto the next word correct? hmm... lol
May 22 '07 #15
DeMan
1GB
Why can't you use the file as you did before?
What are the errors you were getting?

Using the text file would be a far better idea...
May 22 '07 #16
i guess i am lost. this one compiled so i was like sweet!

but the other one I hadn't compiled right. so i should try it again.
May 22 '07 #17
DeMan
1GB
Generally (in programs this small), so long as you can find the first error during compile, the problem is (reasonably) easy to trace.
When strange errors appear, it is most commonly mismatched parentheses or missing semi-colons.
May 22 '07 #18
deman i get these errors with your code

Expand|Select|Wrap|Line Numbers
  1.  
  2. 37 C:\hmong dictionary.cpp no matching function for call to `strncmp(std::string&, std::string&, <unknown type>)' 
  3.  
  4. note C:\temp\Dev-Cpp\include\string.h:51 candidates are: int strncmp(const char*, const char*, size_t)
  5.  
May 22 '07 #19
DeMan
1GB
you can try substituting english.length() with english.size()
(in fact english is not declared in my original example, so you may like to add "string english" below "string line" if you haven't already.

Seeing as you have got an array version working, you may like to add the filehandling from the first with the array operations of the latter (then you don't need the strncmp), so that you read from a file into an array. Once you have the array, you can use the find method as you are.
May 22 '07 #20
i made your changes, similiar issue occurs. do we have to define the function at the bottom of the code? what would i do for that? this is what i got

errors:
37 C:\hmong dictionary.cpp no matching function for call to `strncmp(std::string&, std::string&, <unknown type>)'

note C:\temp\Dev-Cpp\include\string.h:51 candidates are: int strncmp(const char*, const char*, size_t)

code:


#include <iostream>

#include <fstream>

#include <string>

using namespace std;



int main () {

string line;
string english;


cout << "Type the english words here, then press enter. Make sure to copy the words correctly. If you need to spell check use www.google.com";

cin >> english;

ifstream myFile ("c:\\eAh.txt");

if (myFile.is_open())

{

while (! myFile.eof() )

{

getline (myFile,line);

// int strncmp(line, english, english.length());

{
if (strncmp(line, english, english.size))
cout <<"Match found"<< line;

}

}

myFile.close();

}



else cout << "Unable to open file";


}
May 22 '07 #21
btw do you sleep? hahaha you are a great help
May 22 '07 #22
DeMan
1GB
This is generally considered bad practice, but you could try adding a cast to see if whether that's the problem:

strncmp(line, english, (size_t)(english.length()));
or
strncmp(line, english, (size_t)(english.size()));


[I'm in Australia so its almost 9 am here.....]
May 22 '07 #23
:(

that just causes more problems err. hehe
May 22 '07 #24
DeMan
1GB
I'll have a think about it.....
you can use
strcmp(String1, String2)
which doesn't need length (the one with length is considered safer because people find it harder to manipulate into overflowing/injecting format strings etc (because you limit the length), for a local app (and certainly for now), it may be worth trying it. )
May 22 '07 #25
12 C:\hmong dictionary.cpp expected constructor, destructor, or type conversion before '(' token

12 C:\hmong dictionary.cpp expected `,' or `;' before '(' token


if i actually put the ; in after this, it recreates both the new and old error messages! errr it makes no sense
May 22 '07 #26
DeMan
1GB
you could also use
Expand|Select|Wrap|Line Numbers
  1.  
  2. int pos = line.find(english);
  3. if(pos!= string::npos)
  4. {
  5.     //pos is the position of the begining of the word you are searching for in the
  6.     // string from the dictionary.  If you have set up the file to have each line
  7.     //  word, definition
  8.     // then pos must be 0 to be a valid word (otherwise you may get strange 
  9.     // results - eg looking for "at" would leave pos= 1 in a string beginning "cat")
  10. }
  11.  
May 22 '07 #27
DeMan
1GB
This error is at line 12 (which I suspect is where you declare the strings)
May 22 '07 #28
this just makes me more confused haha. it compiles, but just couts nothing and then closes
May 22 '07 #29
weird my .txt file i had

a little
awhile

it would not find awhile, it would just show up blank and do nothing.

i then put this in my .txt

fa
a little
awhile

and it worked! lol. i wonder why it hates two word, words....

also if you put...

fart
fa
a little
awhile

and search for fa, you get fart as your return
May 22 '07 #30
DeMan
1GB
So we should be something like this......

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main () {
  7.   string line;
  8.   string english;
  9.   int pos = string::npos;  //initialise to invalid
  10.  
  11. cout << "Type the english words here, then press enter. Make sure to copy the words correctly. If you need to spell check use www.google.com";
  12. cin >> english;
  13.  
  14.   ifstream myFile ("c:\\eAh.txt");
  15.   if (myFile.is_open())
  16.   {
  17.     while (! myFile.eof() )
  18.     {
  19.       getline (myFile,line);
  20.       pos = line.find(english);
  21.       if(pos != string::pos)
  22.       {
  23.          cout <<"Word in line "<< line;
  24.          if(pos == 0)
  25.          {
  26.             cout << "  Word in first position on line ";
  27.          }
  28.       }
  29.     }
  30.     myFile.close();
  31.   }
  32.  
  33.   else cout << "Unable to open file"; 
  34.  
  35. }
  36.  
(I think this may be case sensitive, so apple != Apple
May 22 '07 #31
cool, but how can i stop it once it works?

i mean... if it finds fa, it should stop printing out words that have fa in it like fart (sorry im using this in my dictionary first thing that came to my head)... any way to pause it, end the if statement and then move on?


i did this...
myFile.close();

this might actually help. is there a way that i could run the loop, based on the letter of the word to open the dictionary that starts with that word? (like the a dictionary), then close, go on to the next word? have mini dictionaries? or should i have one big dictionary and open/close it alot? its only going to have less then 5000-8000 words

also... how can i get it to loop until all the words are translated? like 5 words in a sentance using this one dictionary (not multiple)... i'm going to try some ideas but i just like keeping a memo here for myself and your help
May 23 '07 #32
DeMan
1GB
You would modify the loop by adding an extra flag:
Expand|Select|Wrap|Line Numbers
  1. bool found = false;
  2. while ((! myFile.eof()) && (!found) )
  3. {
  4.       getline (myFile,line);
  5.       pos = line.find(english);
  6.       if(pos != string::pos)
  7.       {
  8.          cout <<"Word in line "<< line;
  9.          if(pos == 0)
  10.          { 
  11.             found = true;  // This will break the loop
  12.             cout << "  Word in first position on line ";
  13.          }
  14.       }
  15. }
  16.  
May 23 '07 #33
i might have to load the input into an array and then, translated it between spaces? in a loop?

my next goal is to translate two words in the dictionary, rather then one! woot

my code works for multiple words, i got it to stop crashing. however, once it translates the first word, it needs to move onto the second. the only way i can think of doing this is to either put the string into an array, then find the next space or find some way to skip to the space each time it loops... so now to be lost again haahah


current code, i had to comment stuff to stop errors, but stopped crashing for multiple words:


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


int main () {
string line;
string english;
int pos = string::npos; //initialise to invalid


cout << "Type the english words here, then press enter. Make sure to copy the words correctly. If you need to spell check use www.google.com";
cin >> english;

ifstream myFile ("c:\\eAh.txt");
while (english != ".")
if (myFile.is_open())

{

while (! myFile.eof() )

{

getline (myFile,line);
pos=line.find(english);
if(pos != string::npos)

{

cout <<"Trans"<< line;
if(pos == 0)

{

cout<< endl;
cout<<"sucks"<<endl;
myFile.close();
// cout << " Word in first position on line "<< endl;
}
}
}

myFile.close();

//my crazy while for endl, trying to do multiple words
//else cout << "Unable to open file";
}
cin>>english;
}
May 23 '07 #34
DeMan
1GB
i might have to load the input into an array and then, translated it between spaces? in a loop?
There are alternatives.......
If a sentence is entered as
"icecream is cool"
then the variable "english"
will contain the whole sentence....
if you use "english.find(" ")" to search for spaces, then you can cut each word into a temporary variable, process it, and move on to the next word.

You may like to use english.substring(int ffrom, int length)
eg if you have
Expand|Select|Wrap|Line Numbers
  1. string s = "abcdefgh";
  2. s=s.substring(2, 4);
  3.  
s now equals "cdef"
May 23 '07 #35
with the array, how would it know where a new place starts or ends? maybe find the space, and then put those positions into the array?

the first example looks easier, but how does the loop know to go to the next space?

maybe the temp variable is a good idea, how does c++ keep track of temp variables?

man i feel like your doing all the work and im doing all the panicking hahaha
May 23 '07 #36
DeMan
1GB
consider this type of idea:
Expand|Select|Wrap|Line Numbers
  1. string english = "it was somwhere in the country in a land of rock and scrub";
  2. english = english + " "; //guarantees a space in the sentence
  3. int pos; string tempword;
  4. int pointer =0;
  5. bool finished = false;
  6. while(!finished)
  7. {
  8.   pos = english.find(" ");
  9.   tempword=english.substring(pointer, pos);
  10.   english = english.substring(pos+1, english.length()); //remove word from sentence
  11.    /*  Process tempword here like we did english previously */
  12.   if(english.length() == 0)
  13.   {
  14.     finished  = true;
  15.   }
  16. }
  17.  
You may like to add some checking that our substrings do not breach the bounds of the string......
May 23 '07 #37
string english, can we have it use cin input?
May 23 '07 #38
DeMan
1GB
I think then you need to use getline (and have a maximum length for english)

eg
Expand|Select|Wrap|Line Numbers
  1. const char SIZE = 128;
  2. char english[SIZE]
  3. cin.getline(english, SIZE);
  4.  
May 23 '07 #39
the char idea i couldnt get to work, im so close on your previous post though with the substrings... only one error

"40 C:\hmong day 3 final.cpp 'struct std::string' has no member named 'substring' "


this is referring to:

english = english.substring(pos+1, english.length()); //remove word from sentence
May 23 '07 #40
DeMan
1GB
sorry!!!! My fault! try "substr" instead
May 23 '07 #41
woot that solved it! I need to read closely lol, i forgot you created a tempword for me! then things made sense. i actually like jumped from my chair when it compiled... woot! thanks mate.

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


int main () {
string line;
//string substring;

char SIZE = 128;

string english;


//int pos = string::npos; //initialise to invalid





cout << "Type the english words here, then press enter. Make sure to copy the words correctly. If you need to spell check use www.google.com";
cin>>english;
#
english = english + " "; //guarantees a space in the sentence
#
int pos; string tempword;
#
int pointer =0;
#
bool finished = false;
#
while(!finished)
#
{
#
pos = english.find(" ");
#
tempword=english.substr(pointer, pos);
#
english = english.substr(pos+1, english.length()); //remove word from sentence
#
/* Process tempword here like we did english previously */
ifstream myFile ("c:\\eAh.txt");
while (tempword != ".")
if (myFile.is_open())


{

while (! myFile.eof() )

{

getline (myFile,line);

pos=line.find(tempword);
if(pos != string::npos)

{

cout <<"Trans"<< line;
if(pos == 0)

{

cout<< endl;
cout<<"sucks"<<endl;
// myFile.close();
cout << " Word in first position on line "<< endl;

#
if(english.length() == 0)
#
{
#
finished = true;
#
}
#
}


}
cin>>english;
}
}//extra
}//extra 2
}//extra 3
}//extra 4
May 23 '07 #42
DeMan
1GB
Glad to help, hope it actually works too!

incidently, if you highlight code when you write it and then hit the "#" symbol, it will wrap code tags around it!!!
May 23 '07 #43
cool i will work on cleaning it up eventually thanks for the tip.

what i need to do now, is get the internal loop to reset itself so that it starts at the beginning of the textfile again... that way... if

abba
apple

and someone types, apple abba

what is happening is it would stop and only print apple since abba came first, and its already past this point when it restarts the loop. if that makes sense.

if the user types, abba apple, then it prints both translations amazingly. so im trying to find some reset variables through trial and error
May 23 '07 #44
DeMan
1GB
We're a little outside of my knowledge here, but......
when you are at the point where you have found a word and are moving onto the next one, try

Expand|Select|Wrap|Line Numbers
  1. myFile.close();
  2. myFile.open("c:\\eAh.txt");
  3.  
This should close your file and reopen it from the beginning
May 23 '07 #45
man it wont do it now unless you type it in order... im frustrated lol. meh!

thats what i was trying to do but did the .open part wrong so this is a good start i just need to find a magic spot to place that.... but it seems it will

do first two words in order, or at least the second word if first (but not in reverse with both words, just the second word first then nothing), or it just outputs nothing if i put open/close in wrong spots lol
May 23 '07 #46
DeMan
1GB
try it just inside this loop:
Expand|Select|Wrap|Line Numbers
  1. ifstream myFile;  //Don't need to initalise here now
  2. while (tempword != ".")
  3. {
  4.   myFile.open("filename");
  5.   /* Do All the other Processing */
  6.  
  7.  
  8.  
  9.   myFile.close();
  10. }
  11.  
(I'm not sure that the condition in that while will work as expected either, but we'll worry about that later)
May 23 '07 #47
update when i use the code i said that works, if i go

ab
apple

it outputs ab and apple

if i say apple it outputs nothing
if you input apple ab, it outputs apple lol

maybe its getting stuck if not starting the text from the first word? maybe that is whats hanging up... that the textfile only works if done in order....
May 23 '07 #48
i think we might be subrtacting to early? maybe thats why it freezes or skips a word? im not sure


or maybe the getline, isn't matching the tempword? maybe i should have the getline stop at the = sign, if it matches tempword, print it and if it doesnt, keep searching for the correct line?

maybe i cant use equal like love = hlub... maybe i need if temp = line, then getline from 2nd file 0.o

im exhausted lol
May 23 '07 #49
DeMan
1GB
It probably freezes because( tempString!=".") is always true.....
to compare the actual values, we can either revert back to our facourite
Expand|Select|Wrap|Line Numbers
  1. if(strncmp(".", tempString, 1)!=0)
  2. {
  3. }
  4.  
OR
we can try copmparing a single character
Expand|Select|Wrap|Line Numbers
  1. if(tempString.at(0)!='.')
  2. {
  3. }
  4.  
The not processing elements out of order is more like a file handling thing......which the open/close combination 3 posts ago should have helped....
May 23 '07 #50
91 Replies

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Derek Fountain | last post: by
8 posts views Thread by Aziz McTang | last post: by
52 posts views Thread by piaseckiac | last post: by
46 posts views Thread by vvk4 | last post: by
39 posts views Thread by Quick Fox | last post: by
182 posts views Thread by Jim Hubbard | last post: by
4 posts views Thread by Chris F Clark | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.