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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jerger
    New Member
    • May 2007
    • 77

    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(searc h, 0)) != string::npos) {
    cout << "found '" << search << "' @ offset " << offset << endl;
    }

    return 0;
    }
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    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)?

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Originally posted by jerger
      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?

      [code=cpp]
      string* search = english; //search using input
      int offset; // where it was found (or not (-1))
      if ((offset = line.find(searc h, 0)) != string::npos) {
      cout << "found '" << search << "' @ offset " << offset << endl;
      [/code]
      You never defined "line" and I dont think that you need a search variable.

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        retract that........
        Last edited by DeMan; May 22 '07, 02:05 AM. Reason: Already Answered

        Comment

        • jerger
          New Member
          • May 2007
          • 77

          #5
          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 !

          Comment

          • DeMan
            Top Contributor
            • Nov 2006
            • 1799

            #6
            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))

            Comment

            • jerger
              New Member
              • May 2007
              • 77

              #7
              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>sp it out, word two>function>sp it 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

              Comment

              • jerger
                New Member
                • May 2007
                • 77

                #8
                the description here is similiar in context, but it would not compile for me:(

                http://www.planet-source-code.com/vb/scripts/ShowCode.asp?tx tCodeId=7536&ln gWId=3#zip

                Comment

                • DeMan
                  Top Contributor
                  • Nov 2006
                  • 1799

                  #9
                  Code:
                  #include <iostream>
                  #include <fstream>
                  #include <string>
                  using namespace std;
                  
                  int main () {
                    string line;
                  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);
                        if(strncmp(line, english, englis.length())
                        {
                           cout <<"Match found"<< line;
                        }
                      }
                      myFile.close();
                    }
                  
                    else cout << "Unable to open file"; 
                  
                  }
                  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)

                  Comment

                  • jerger
                    New Member
                    • May 2007
                    • 77

                    #10
                    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 ?

                    Comment

                    • DeMan
                      Top Contributor
                      • Nov 2006
                      • 1799

                      #11
                      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

                      Comment

                      • jerger
                        New Member
                        • May 2007
                        • 77

                        #12
                        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

                        Comment

                        • DeMan
                          Top Contributor
                          • Nov 2006
                          • 1799

                          #13
                          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( ))

                          Comment

                          • weaknessforcats
                            Recognized Expert Expert
                            • Mar 2007
                            • 9214

                            #14
                            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...

                            Comment

                            • jerger
                              New Member
                              • May 2007
                              • 77

                              #15
                              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,stri ng> 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>>translatem e;

                              string s = translateme;

                              map<string,stri ng>::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

                              Comment

                              Working...