//=========================================================================== // BABEL FISH TRANSLATION // Using the component NMHTTP1 // Nick Gessler - 3 March 2006 // // Talking to Babel Fish required several experiments with both the NMHTTP1 // component and with the protocol used at the Babel Fish website. // The code for the NMHTTP1 component is from Borland Help. Variable and // object names have been changed for clarity. // Experiment Set 1: The code between the two bars of "###############" // was written to understand and text Babel Fish protocols. // // Experiment Set 2: The function "doit()" operates the GroupBox "Go" // and provides a flexible framework for more complex operations. // //=========================================================================== #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- //=========================================================================== // VARIABLES //=========================================================================== AnsiString header; AnsiString body; AnsiString textToTranslate; AnsiString translatedText; AnsiString getString; // In a first set of experiments, int positionOfTextToTranslate; int positionOfTranslatedText; int positionOfSpace; // Refined search variables which find any text. int startOfTextToTranslate; int lengthOfTextToTranslate; int startOfTranslatedText; int lengthOfTranslatedText; //=========================================================================== // FUNCTIONS //=========================================================================== void doit (void) { // prepare the getString to send to Babel Fish // it inserts the string from the textToTranslate Memo // and request a translation from english to german "en_de" getString = "http://world.altavista.com/tr?doit=done&urltext=" + Form1->MemoToGo->Text + "&lp=en_de"; // the textToTranslate string contains blank spaces // which must all be replaced with "%20"s while (positionOfSpace = getString.Pos(" ")) { getString = getString.SubString(1, positionOfSpace - 1) + getString.SubString (positionOfSpace + 1, getString.Length() - positionOfSpace) ; getString.Insert("%20", positionOfSpace); } // the string is ready to send, so send it Form1->NMHTTP1->Get(getString); // we need to extract TextToTranslate and TranslatedText // from the body of the returned HTML document body = Form1->NMHTTP1->Body; // extract TextToTranslate from the body // find where it begins startOfTextToTranslate = body.Pos("\"trtext\"") + 10; // start a new string there textToTranslate = body.SubString(startOfTextToTranslate, 100); // find the length of the text we want lengthOfTextToTranslate = textToTranslate.Pos("<") - 1; // trim irrelevant material from the end of the string textToTranslate = textToTranslate.SubString(1, lengthOfTextToTranslate); // display it Form1->MemoToGo->Text = textToTranslate; // extract TranslatedText from the body // we do this in the same way // there are two places where it is found, but let's just try the first if (true) { startOfTranslatedText = body.Pos("style=padding:10px;>") + 20; translatedText = body.SubString(startOfTranslatedText, 100); lengthOfTranslatedText = translatedText.Pos("<") - 1; } else { startOfTranslatedText = body.Pos("\"q\"") + 11; translatedText = body.SubString(startOfTranslatedText, 100); lengthOfTranslatedText = translatedText.Pos("\"") - 1; } translatedText = translatedText.SubString(1, lengthOfTranslatedText); Form1->MemoReturned->Text = translatedText; } //=========================================================================== // EVENT HANDLERS //=========================================================================== void __fastcall TForm1::ButtonGetClick(TObject *Sender) { NMHTTP1->Get(EditURL->Text); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonHeadClick(TObject *Sender) { NMHTTP1->Head(EditURL->Text); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonOptionsClick(TObject *Sender) { NMHTTP1->Options(EditURL->Text); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonTraceClick(TObject *Sender) { AnsiString S; if (InputQuery("Trace Data Required", "Input data to send as trace", S)) NMHTTP1->Trace(EditURL->Text, S); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonPutClick(TObject *Sender) { if (OpenDialog1->Execute()) { NMHTTP1->OutputFileMode = TRUE; NMHTTP1->Put(EditURL->Text, OpenDialog1->FileName); NMHTTP1->OutputFileMode = FALSE; } } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonPostClick(TObject *Sender) { AnsiString S; if (InputQuery("Post Data Required", "Input data to Post", S)) NMHTTP1->Post(EditURL->Text, S); //NMHTTP1->Post(EditURL->Text, // "doit=done, intl=1, trtext=black, lp=en_de, btnTrTxt=Translate"); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonDeleteClick(TObject *Sender) { NMHTTP1->Delete(EditURL->Text); } //--------------------------------------------------------------------------- void __fastcall TForm1::NMHTTP1AuthenticationNeeded(TObject *Sender) { AnsiString AnID, APass; InputQuery("Authentication required", "Enter a user ID", AnID); InputQuery("Authentication required", "Enter a password", APass); NMHTTP1->HeaderInfo->UserId = AnID; NMHTTP1->HeaderInfo->Password = APass; ShowMessage("Authentication information in place, please retry the previous command"); } //--------------------------------------------------------------------------- void __fastcall TForm1::NMHTTP1Failure(CmdType Cmd) { MemoHeader->Text = NMHTTP1->Header; MemoBody->Text = NMHTTP1->Body; switch(Cmd) { case CmdGET: MemoStatus->Lines->Add("HTTP GET Failed"); case CmdPOST: MemoStatus->Lines->Add("HTTP Post Failed"); case CmdHEAD: MemoStatus->Lines->Add("HTTP HEAD Failed"); case CmdOPTIONS: MemoStatus->Lines->Add("HTTP OPTIONS Failed"); case CmdTRACE: MemoStatus->Lines->Add("HTTP TRACE Failed"); case CmdPUT: MemoStatus->Lines->Add("HTTP PUT Failed"); case CmdDELETE: MemoStatus->Lines->Add("HTTP Delete Failed"); } } //--------------------------------------------------------------------------- void __fastcall TForm1::NMHTTP1Redirect(bool &Handled) { if (MessageDlg( "This site is redirecting you to another site. Allow redirect?", mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrNo) Handled = TRUE; } //--------------------------------------------------------------------------- void __fastcall TForm1::NMHTTP1Success(CmdType Cmd) { if (NMHTTP1->CookieIn != "") MemoCookie->Text = NMHTTP1->CookieIn; MemoHeader->Text = NMHTTP1->Header; MemoBody->Text = NMHTTP1->Body; //######################################################################### // First set of experiments to discover how to talk with Babel Fish. // Visiting their site in a browser, using the English-to-German option, // I entered "cryptology" and the website responded with "Kryptologie." // I then viewed the HTML source, searched it for those two words in // order to find any neighboring strings which I could use as targets // for future searches for any other TextToTranslate and TranslatedText. // The code below does approximately the same thing. However, the // TextToTranslate appears only once in the returned document, while the // TranslatedText appears twice. This code only shows the first encounter. header = NMHTTP1->Header; body = NMHTTP1->Body; // This finds the Text to Translate positionOfTextToTranslate = body.Pos("cryptology"); EditFromPosition->Text = positionOfTextToTranslate; textToTranslate = body.SubString(positionOfTextToTranslate - 20, 100); MemoTextToTranslate->Text = textToTranslate; // This finds the Translated Text positionOfTranslatedText = body.Pos("Kryptologie"); EditToPosition->Text = positionOfTranslatedText; translatedText = body.SubString(positionOfTranslatedText - 20, 100); MemoTranslatedText->Text = translatedText; //######################################################################### switch(Cmd) { case CmdGET: MemoStatus->Lines->Add("HTTP GET Successful"); case CmdPOST: MemoStatus->Lines->Add("HTTP POST Successful"); case CmdHEAD: MemoStatus->Lines->Add("HTTP HEAD Successful"); case CmdOPTIONS: MemoStatus->Lines->Add("HTTP OPTIONS Successful"); case CmdTRACE: MemoStatus->Lines->Add("HTTP TRACE Successful"); case CmdPUT: MemoStatus->Lines->Add("HTTP PUT Successful"); case CmdDELETE: MemoStatus->Lines->Add("HTTP DELETE Successful"); } } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonDoItClick(TObject *Sender) { doit (); } //---------------------------------------------------------- That's All Folks