PDA

View Full Version : language translation proggy



lydia_sp
15-11-2011, 13:16
ok, I am just starting this example. my idea is to translate words from input textbox into english, french, italian, spain, germany. my aim should looks like "I am an spanish girl" to translate this whole sentence into the language you like.
first I want only to translate one word like "hello" or "love" into various language. But I don't know what's best way. And what module I should prefer. I will open a text file with pre-defined vocabulary of special language and then translate this word into another language. perhaps here's at board an example to study?

my silly startpoint looks like this ;)





' Empty GUI script created on 11-15-2011 10:42:28 by lydia (ThinAIR) Uses "UI", "console", "tokenizer" ' -- ID numbers of controls Begin ControlID %ButtonClose = 1001 %tEnglish %List01 %tInput End ControlID Dim hDlg As DWord Dialog New 0, "translate_proggy",-1,-1, 480, 250, _ %WS_POPUP Or _ %WS_VISIBLE Or _ %WS_CLIPCHILDREN Or _ %WS_CAPTION Or _ %WS_SYSMENU Or _ %WS_MAXIMIZEBOX Or _ %WS_MINIMIZEBOX, _ 0 To hDlg Control Add Textbox hDlg, %tInput , "", 300, 25, 250, 280, %ES_AUTOHSCROLL Or %ES_LEFT Or %WS_BORDER Or %WS_TABSTOP Or %ES_MULTILINE Or %ES_WANTRETURN Or %WS_VSCROLL, %WS_EX_CLIENTEDGE Or %WS_EX_LEFT, Call pickup() Control Add Button, hDlg, %ButtonClose, "Click to kill", 290, 5, 50, 18 Dialog Show Modal hDlg Call cbDialog CallBack Function cbDialog() As Long Local hDlg As Long, hText1 As Long Local lRslt As Long Dim vList(6) As String vList(1) = "french" vList(2) = "german" vList(3) = "italian" vList(4) = "spain" vList(5) = "english" vList(6) = "dutch" Dim vList2(100) As String vList2(1) = "Boy" Select Case CBMSG Case %WM_INITDIALOG Control Add COMBOBOX, CBHNDL, %List01 , vList(),150,5, 100, 200, %CBS_DROPDOWNLIST Or %CBS_HASSTRINGS Or %WS_TABSTOP Or %WS_VSCROLL COMBOBOX Select CBHNDL, %List01, 1 Control Add LISTBOX, CBHNDL,%tEnglish, vList2(), 5,25,250,280, %WS_VSCROLL Or %LBS_NOINTEGRALHEIGHT,%WS_EX_CLIENTEDGE, Call pickup() Case %WM_COMMAND If CBWPARAM = %ButtonClose Then Dialog End CBHNDL Case %WM_DESTROY MsgBox 0, "Window is to be destroyed." End Select End Function CallBack Function pickup() Local hdlg As Long Local str As String Dim vList2(100) As String vList2(1) = "Girl" If CBCTLMSG = %LBN_SELCHANGE Or CBCTLMSG=%LBN_DBLCLK Then LISTBOX Get Text hDlg, %tEnglish To str 'LISTBOX Get Text hWnd, ctrlID To Txt MsgBox 0, "hello english" LISTBOX Reset cbhndl,%tEnglish,vList2() 'LISTBOX RESET hWnd, ctrlID [, NewStringArray()] End If End Function


help or ideas are welcome. maybe the code tags don't working. I am working with linux and openoffice.org "writer" and this app copied whole text as block, don't know why, sorry. example in attachement too.

greetings, lydia

largo_winch
15-11-2011, 14:49
hello lydia, perhaps an example from petr can help about your asking for translation help topic:

http://www.thinbasic.com/community/showthread.php?10505-Partial-automation-of-code-translation

bye, largo

ErosOlmi
15-11-2011, 17:23
Hi lydia,

can you please change posted code into your first post?
It seems not to have carriage return line feed pairs so it has been condensed into one line.

Thanks
Eros

zak
15-11-2011, 17:48
my idea is to use the web control to display the site:
http://translate.google.com/
as an example if you have translated the "i am a spanish girl" to spanish you will notice that the address like this:
http://translate.google.com/#en|es|I%20am%20an%20spanish%20girl
1- look at #en|es|
to investigate about abbreviations to other languages try to translate to different languages and look at the address how it changed.
2- also it replaces the space character with "%20"
4- so simply attach the content of the thinbasic textbox to a string variable and add it to "http://translate.google.com/#en|es|"
then supply it to the web browser control.
5- the text from the thinbasic textbox ,we must change every space between words to "%20", i have used Replace$.
6- to get back the translated text from google translator to a second thinbasic textbox , this is i don't know.
attached the source adapted from webBrowser_01.tbasic from the examples:
C:\thinBasic\SampleScripts\WebBrowser
7623


'------------------------------------------------------------------
' Pre-processor prerequisites
'------------------------------------------------------------------
#MINVERSION 1.7.8.0

'------------------------------------------------------------------
' Declare needed thinBasic modules
'------------------------------------------------------------------
USES "UI"
USES "UIAdv"
uses "console"

'------------------------------------------------------------------
' Global declarations
'------------------------------------------------------------------
begin const
%ID_WEB2 = 200
%ID_OK
%TM_WEB2_Busy
%translate
%tInput
end const

'------------------------------------------------------------------
' Main script entry point
'------------------------------------------------------------------
function TBMain() as long
Global hDlg As Long
local Count as long
Global txt As String
'---Create a new window
'DIALOG NEW pixels, 0, "Web Browser control & google translator",-1,-1,800,600,%WS_SYSMENU Or %WS_MINIMIZEBOX Or %WS_CAPTION To hDlg -1, -1, 800, 600, _
Dialog New Pixels, 0, "Web Browser control test", _
-1, -1, 740, 480, _
%WS_CLIPCHILDREN Or _
%WS_CLIPSIBLINGS Or _
%WS_DLGFRAME Or _
%DS_CENTER Or _
%WS_CAPTION Or _
%WS_SYSMENU Or _
%WS_OVERLAPPEDWINDOW , _
0 _
To hDlg
Control Add Textbox hDlg, %tInput , "", 10, 400, 300, 300, %ES_AUTOHSCROLL Or %ES_LEFT Or %WS_BORDER Or %WS_TABSTOP Or %ES_MULTILINE Or %ES_WANTRETURN Or %WS_VSCROLL, %WS_EX_CLIENTEDGE Or %WS_EX_LEFT
Control Add Button, hDlg, %translate, "translate eng to spain", 350, 400, 140, 50, Call translate()

'---Show new window and assign it callback function
DIALOG SHOW MODELESS hDlg, call cbDialog_Proc
Do
DIALOG DOEVENTS 0 To Count
Loop While Count

end function

'------------------------------------------------------------------
' Window callback handle. Use to handle message pump of main window
'------------------------------------------------------------------
callback function cbDialog_Proc() as long
local sStr as string
local lValue as long
local IsBusy as long

SELECT CASE cbMsg

CASE %WM_INITDIALOG
'Control Add WEBBROWSER, cbhndl, %ID_WEB, "", 100, 10, 300, 240 'call cbWeb
DIALOG SET TIMER cbhndl, %TM_WEB2_Busy, 500
WebBrowser_Create CBHNDL, %ID_WEB2, "", 0, 0, 750, 400, %WS_CHILD Or %WS_VISIBLE
'WebBrowser_Navigate2(CBHNDL, %ID_WEB2, "http://www.google.com")
Control Set Resize CBHNDL, %ID_WEB2, 1, 1, 1, 1
'control add button cbhndl, %ID_OK, "OK", 10, 10, 40, 20
case %WM_TIMER
select case cbctl
case %TM_WEB2_Busy
if WebBrowser_Busy(cbhndl, %ID_WEB2) then
dialog set text cbhndl, "Busy ..."
else
dialog set text cbhndl, "Done!"
DIALOG kill TIMER cbhndl, %TM_WEB2_Busy
end if
end select

case %WM_DESTROY
'DIALOG kill TIMER cbhndl, %TM_WEB2_Busy

'case %WM_COMMAND
' select case cbctl
' case %ID_OK
' sStr = WebBRowser_Doc_GetElementValueById(cbhndl, %ID_WEB, "Input_text")
' msgbox 0, sStr
' sStr = WebBRowser_Doc_GetElementInnerHtmlById(cbhndl, %ID_WEB, "Output")
' msgbox 0, acode$(sStr)
'
' end select
END SELECT

end function

'------------------------------------------------------------------
callback function cb_Web2_Busy() as long
'------------------------------------------------------------------


end function
CallBack Function translate()

If CBMSG = %WM_COMMAND Then
If CBCTLMSG = %BN_CLICKED Then
Control Get Text hDlg, %tInput To txt
txt = "http://translate.google.com/#en|es|" + txt +
'txt = "http://translate.google.com/#en|es|I%20am%20an%20spanish%20girl "
txt = Replace$(txt, " ", "%20")
WebBrowser_Navigate2(CBHNDL, %ID_WEB2, txt)

'MsgBox 0,txt
End If
End If

End Function

ErosOlmi
15-11-2011, 18:43
6- to get back the translated text from google translator to a second thinbasic textbox , this is i don't know.


Looking at the web page created by Google Translator I've seen that the translated text is returned inside the following line:

<span id="result_box" class="short_text" lang="it"><span class="hps">___xxxxxx___</span></span>

where ___xxxxxx___ is the translated text

The ID of the area we need to get is called "result_box"

If the above is true, than you can use WebBrowser_Doc2_GetElementInnerHtmlById to extract html code inside the requested element ID

So change your callback in something like that:


CallBack Function translate()

Local sOutput As String
Local TimeOut As Double

If CBMSG = %WM_COMMAND Then
If CBCTLMSG = %BN_CLICKED Then
Control Get Text hDlg, %tInput To txt
txt = "http://translate.google.com/#en|es|" + txt +
'txt = "http://translate.google.com/#en|es|I%20am%20an%20spanish%20girl "
txt = Replace$(txt, " ", "%20")
WebBrowser_Navigate2(CBHNDL, %ID_WEB2, txt)

'---Loop until you get something or timeout
TimeOut = Timer
Do
sOutput = WebBrowser_Doc2_GetElementInnerHtmlById (CBHNDL, %ID_WEB2, "result_box")
Sleep 10
Loop While (Len(sOutput) = 0 And (Timer - TimeOut) < 3) '---give maximum 3 seconds

'---If we got something ...
If Len(sOutput) Then
MsgBox 0, sOutput
Else
'---...otherwise some error has occurred
MsgBox 0, "Something wrong happened"
End If
End If
End If

End Function




I think there are clever way to get the ready status of the web browser control. For the moment I've used a timeout of 3 seconds.

zak
16-11-2011, 09:47
thank you Eros, your example works for me
such as
google is my preferred island
translated to spanish:
google es mi isla preferida
the contents of the sOutput variable is:
<SPAN class=hps closure_uid_dy1rim="103">google</SPAN> <SPAN class=hps closure_uid_dy1rim="104">es mi isla</SPAN> <SPAN class=hps closure_uid_dy1rim="105">preferida</SPAN>

it seems we just need the words between > and < , this is a regex problem but easier to save the above sOutput variable to html file :
MsgBox 0, sOutput
FILE_Save(APP_SourcePath +"results3.html",sOutput)
and html engine will do the hard work and offer the sentence as normal.

note: before using FILE_Save, we add: Uses "File"

ErosOlmi
16-11-2011, 11:14
it seems we just need the words between > and < , this is a regex problem but easier to ...


Yes all that html items are used by Google (<span ...) to let users dynamically interact with web page (Ajax and other dynamic page content change).

Interesting solution that will bring me to think about new solutions to be implemented in WebBrowser control.

Petr Schreiber
16-11-2011, 11:58
For this case, the detagification can be done this way:


'---If we got something ...
If Len(sOutput) Then
String parsedTokens()
Long nParsedTokens = Parse(sOutput, parsedTokens, Any "<>")
Long i

sOutput = ""
For i = 1 To nParsedTokens Step 2
sOutput += parsedTokens(i)
Next

MsgBox 0, sOutput
Else
'---...otherwise some error has occurred
MsgBox 0, "Something wrong happened"
End If


Alternatively, the GRAB$ could be used as well, but the above approach is faster I think.


Petr

largo_winch
16-11-2011, 12:20
hello, here's another approach for translation of words, but it's not perfect as I didn't know how to open and scan the vocabeldata file (fictive, it's full of language vocabel letters and words)

only test version for improving:


' Empty GUI script created on 11-15-2011 10:42:28 by largo_winch (ThinAIR)

Uses "UI", "console", "tokenizer"

' -- ID numbers of controls
Begin ControlID
%ButtonClose = 1001
%tEnglish
%tfrance
%tgerman
%tspain

%List01
%tInput
%tLoad
%tSave
%typein
%typeinlabel
End ControlID

Dim hDlg As DWord
Dim sFile As String
Dim sInput As String

Global vocab_engl() As String, item_english As Long
Global vocab_french() As String, item_french As Long
Global vocab_italian() As String, item_italian As Long
Global vocab_spain() As String, item_spain As Long

'------------------------------------------------------------>

Dialog New 0, "translate_proggy by lw",-1,-1, 580, 360, _
%WS_POPUP Or _
%WS_VISIBLE Or _
%WS_CLIPCHILDREN Or _
%WS_CAPTION Or _
%WS_SYSMENU Or _
%WS_MAXIMIZEBOX Or _
%WS_MINIMIZEBOX, _
0 To hDlg

Control Add Textbox hDlg, %tInput , "", 300, 25, 250, 280, %ES_AUTOHSCROLL Or %ES_LEFT Or %WS_BORDER Or %WS_TABSTOP Or %ES_MULTILINE Or %ES_WANTRETURN Or %WS_VSCROLL, %WS_EX_CLIENTEDGE Or %WS_EX_LEFT, Call pickup()
Control Add Button, hDlg, %ButtonClose, "Click to kill", 290, 5, 50, 18
Control Add Button, hDlg, %tLoad, "Load", 5, 5, 60, 14
Control Add Button, hDlg, %tSave, "SaveFile", 505, 5, 70, 14
Control Add Textbox, hDlg, %typein, "type in word for translation", 10, 330, 245, 20
Control Set Color hDlg, %typein, %BLUE, %YELLOW

Control Add Label, hDlg, %typeinlabel, "Type some language words and press <ENTER> key",12,310,180,12
Control Add Button, hDlg, %IDOK, "", 1000, 1000, 60, 14, Call Translate_Button

Dialog Show Modal hDlg Call cbDialog


CallBack Function cbDialog() As Long
Local hDlg As Long, mytext As Long

Local lRslt As Long
Dim vList(6) As String
vList(1) = "french"
vList(2) = "german"
vList(3) = "italian"
vList(4) = "spain"
vList(5) = "english"
vList(6) = "dutch"

Dim vList2(100) As String
vList2(1) = "Boy"

Select Case CBMSG

Case %WM_INITDIALOG

Control Add COMBOBOX, CBHNDL, %List01 , vList(),150,5, 100, 200, %CBS_DROPDOWNLIST Or %CBS_HASSTRINGS Or %WS_TABSTOP Or %WS_VSCROLL
COMBOBOX Select CBHNDL, %List01, 1
Control Add LISTBOX, CBHNDL,%tEnglish, vList2(), 5,25,250,280, %WS_VSCROLL Or %LBS_NOINTEGRALHEIGHT,%WS_EX_CLIENTEDGE, Call pickup()

'----------------------------------> largos part --------------------------------------------->
' readVocabulary ("ien.vcb",vocab_engl(),item_english,%id_iteminglese,"Vocaboli in english:")
' readVocabulary ("ifr.vcb",vocab_french(),item_french,%id_itemfrancese,"Vocaboli in french:")
' readVocabulary ("ita.vcb",vocab_italian(),item_italian,%id_itemtedesco,"Vocaboli in german:")
' readVocabulary ("isp.vcb",vocab_spain(),item_spain,%id_itemspain,"Vocaboli in spanish:")

Control Handle hDlg, %tInput To myText
Control Set Focus hDlg,%tInput
'----------------------------------> largos part --------------------------------------------->

Case %WM_COMMAND
If CBWPARAM = %ButtonClose Then Dialog End CBHNDL

If CBWPARAM = %tLoad Then
'If CBCTLMSG = %BN_CLICKED Then
sFile = OpenFile(CBHNDL)
If Len(sFile) Then
Control Set Text CBHNDL, %tInput,FILE_Load(sFile)
End If
End If

If CBWPARAM = %tSave Then
MsgBox 0, "here!"
If CBCTLMSG = %BN_CLICKED Then
sFile = SaveFile(CBHNDL)
If Len(sFile) Then
FILE_Save(sFile, Control_GetText(CBHNDL, %tInput))
End If
End If
End If

Case %WM_DESTROY

End Select
End Function

CallBack Function pickup()
Local hdlg As Long
Local str As String
Dim vList2(100) As String
vList2(1) = "Girl"

If CBCTLMSG = %LBN_SELCHANGE Or CBCTLMSG=%LBN_DBLCLK Then
LISTBOX Get Text hDlg, %tEnglish To str
MsgBox 0, "hello english"
LISTBOX Reset cbhndl,%tEnglish,vList2()
'LISTBOX RESET hWnd, ctrlID [, NewStringArray()]

End If
End Function

Function SaveFile(hDlg As DWord) As String
Dim sFile As String
Dim sFilter As String

sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"

sFile = Dialog_SaveFile(hDlg, _
"Save a file", _
APP_SourcePath, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST Or %OFN_HIDEREADONLY Or %OFN_ENABLESIZING)
Function = sFile
End Function

CallBack Function Translate_Button()
If CBCTLMSG = %BN_CLICKED Then
DoTheTranslation(CBHNDL, %typein, %tInput, "this is translated text.. " )
End If
End Function

Function DoTheTranslation(ByVal hDlg As DWord, ByVal ID_In As Long, ByVal ID_Out As Long, AdditionalText As String) As Long
Local counter As Long, sBuffer As String

Control Get Text hDlg, ID_In To sInput
If Trim$(sInput) = "" Then
sInput = "more.."
End If
Counter += 1
sInput = AdditionalText & Format$(Counter, "000") & " " & sInput

Control Get Text hDlg, ID_Out To sBuffer
Control AppendToTop Text hDlg, ID_Out, sInput & $CRLF
Control Set Text hDlg, ID_In, ""
Control Set Focus hDlg, ID_In
Control Set Text hDlg, %tInput, sInput

End Function

Function OpenFile(hDlg As DWord) As String
Dim sFile As String
Dim sFilter As String

sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"

sFile = Dialog_OpenFile(hDlg, _
"Open a file", _
APP_SourcePath, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST Or %OFN_HIDEREADONLY Or %OFN_ENABLESIZING)
Function = sFile
End Function

Sub proofLanguage(id As DWord,vocabeldata() As String,item As Long,words As String,idtexto As Long,texto As String)

Local j As Long,finding As Long
LISTBOX Reset hDlg,id
If words="!" Then Exit Sub
For j=1 To item
If LEFT$(words,1)="!" Then
If Ucase$(LEFT$(vocabeldata(j),Len(words)-1))=Ucase$(RIGHT$(words,Len(words)-1)) Then LISTBOX Add hDlg,id,vocabeldata(j):Incr finding
Else
If InStr(Ucase$(vocabeldata(j)),Ucase$(words)) Then LISTBOX Add hDlg,id,vocabeldata(j):Incr finding
End If
If finding>10 And Len(words)<3 Then Control Set Text hDlg,idtexto,"too few letters, looking more special..":Exit Sub
Next

Control Set Text hDlg,idtexto,texto+Str$(finding)+"."
End Sub

'-----------------------> SEARCHING CLASS IN DATA LANGUAGE FILE -------------->
Function text_searching_class (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Local lzStr As Asciiz * 128, lRet As Long
Static lString As String
Select Case wMsg
Case %WM_KEYUP
Case %WM_KEYDOWN

Case %WM_CHAR
Select Case wParam
Case %VK_BACK
lString=LEFT$(lString,Len(lString)-1)
proofLanguage %tenglish ,vocab_engl(),item_english,lString,%id_iteminglese,"In english"
proofLanguage %tfrance,vocab_french(),item_french,lString,%id_itemfrancese,"In french"
proofLanguage %tgerman ,vocab_italian(),item_italian,lString,%id_itemtedesco,"In german"
proofLanguage %tspain ,voc_spain(),item_spain,lString,%id_itemspain,"In spanish"

Case &h20 To &hff
lString = lString + Chr$(wParam)

proofLanguage %tenglish ,vocab_engl(),item_english,lString,%id_iteminglese,"In inglese"
proofLanguage %tfrance,vocab_french(),item_french,lString,%id_itemfrancese,"In francese"
proofLanguage %tgerman ,vocab_italian(),item_italian,lString,%id_itemtedesco,"In tedesco"
proofLanguage %tspain ,vocab_spain(),item_spain,lString,%id_itemspain,"In spanish"

Case &h0D
Select Case Ucase$(lString)
Case "DIR","TIME","BEEP","KILL" : lString = "*" + lString + "*"
End Select

lString = ""
End Select
End Select

End Function

'------------> not perfect but important part to scan file for vocabeldata ------------------------------------->
Sub readVocabulary(nameof_file As String,vocaboli() As String,item As Long,idtexto As Long,texto As String)
Local tmp As String,j As Long,tabulatore As Long
' tabulatore=Max%(tabulatore,InStr(tmp,"¦"))

Dim vocabeldata(1 To item)
For j=1 To item

vocaboli(j)=Extract$(vocabeldata(j),"¦")+$SPC(tabulatore-Len(Extract$(vocabeldata(j),"¦")))+Remain$(vocabeldata(j),"¦")
Next

Control Set Text hDlg,idtexto,texto+Str$(item)+"."

End Sub



hope this could help also for building data file with different language words (italian, english, spanish, french, german, dutch etcpp). my idea is not using internet and google translation.

bye, largo

lydia_sp
16-11-2011, 17:03
thank you all for very fast help or solutions :) !

1) the translation example causes an error, the message box shows that. but the proggy works but without some relays and doesn't close here correct (after one, two tests). my version I add as attachement file. it's already finished? I included petr's and eros suggestions too in it. thanks zak for the example and embedding internet access and webbrowser.

2) my idea was similiar to largo's last example. I will proof to find a database for vocabulary content. thanks for that! looks very interesting. I will looking closer to petr's translation example, perphaps I can understand how that's going on and working well for my example too.

I am sitting still at university. cannot send any example. here the computer are not working well.

- saludos, lydia

lydia_sp
18-11-2011, 11:59
hello dear thinbasic friends. I am looking for an "extract$" example for thinbasic. I found nothing at board. Or I can not use perfectly the search module ;) second question what's the benefit of "tokenizer" module and where to use it with profits? third question: to check a whole file after special words, what's the best command? ("instr", "array sort, array scan" or another command?)

greetings, -lydia