|
-
Mar 25th, 2003, 11:52 PM
#1
Thread Starter
Junior Member
I'm making a Translator, but i need help on this part
Okay in this program there's a translating part. Sounds pretty simple huh? well this won't work for some reason, and i want you to see what's wrong...
I'm getting it to find each word as it goes along, and i've only had one word put in so far just to test it out, Je ("I" in french)
VB Code:
Dim Length As Integer
Dim x As Integer
Dim y As Integer
Dim word As String
Dim sentence As String
Dim xa As String
Dim ya As String
Private Sub Button1_Click(............) Handles Button1.Click
sentence = ""
Length = Len(TextBox1.Text)
For x = 1 To Length
xa = Mid(TextBox1.Text, 1, x)
If xa = " " Then
For y = 1 To x Step -1
ya = Mid(TextBox1.Text, x, 1)
If ya = " " Then
word = Mid(TextBox1.Text, y + 1, x - 1)
If word = " Je " Then
sentence = sentence + " I "
TextBox2.Text = sentence
End If
End If
Next
End If
Next
End Sub
End Class
-
Mar 26th, 2003, 12:11 AM
#2
Fanatic Member
why not do it using regular expressions
here is a demo for your je it uses 2 textboxes the first is the input the second displays the translation and a button fires the whole thing off
the reg expression is very very basic but you can build them to be ultra clever
here is the demo
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Text = translator(TextBox1.Text)
End Sub
Private Function translator(ByVal str As String) As String
Dim regex As New System.Text.RegularExpressions.Regex("je\s")
Return regex.Replace(str, "I ")
End Function
-
Mar 26th, 2003, 12:30 AM
#3
Thread Starter
Junior Member
oh thanks it's really hard transitioning to vb.NET
-
Mar 26th, 2003, 12:38 AM
#4
Thread Starter
Junior Member
now how do i do more then one word?
Dim regex As New System.Text.RegularExpressions.Regex("Je")
Return regex.Replace(str, "I ")
Dim regex As New System.Text.RegularExpressions.Regex("Tu")
Return regex.Replace(str, "You ")
doesn't work... because i'm declaring it twice, i've never used reg expression before
Last edited by SamuraiMooCow; Mar 26th, 2003 at 12:48 AM.
-
Mar 26th, 2003, 02:10 AM
#5
Fanatic Member
You need to modify the regular expression that is used
I would suggest you will need a database that looks something along the lines of this
primary key, English Word, French Word, context
then you could loop depending on the translation eg french to english you query the db loop through each row inserting the french word in the regular expression and placing the english word in the replace string
this of coarse only gives you literal translations and not a true translation. as langauges are not the easiest things to work with. It would also take some length of time if you created a decent db with every word in the english dictionary with its french equivalent. Even using regex which is very quick the database looping would cause a considerable delay
-
Mar 26th, 2003, 02:16 AM
#6
Fanatic Member
for a quick demo of how to do more than one word you could try this approach
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim stringArray() as string = textbox1.text.split(" ")
dim intLoop as integer
dim NewString as string = textbox1.text
for intLoop = 0 to stringArray.length - 1
newstring = translator(newstring,stringArray(intLoop),GetReplaceMentWord(stringArray(intLoop)))
Next intloop
textBox2.text = newstring
End Sub
Private Function translator(ByVal str As String, byval strSearchWord as string, byVal strReplaceWord as string) As String
Dim regex As New System.Text.RegularExpressions.Regex(strsearchword & "\s")
Return regex.Replace(str, strReplaceWord)
End Function
Private Function GetReplaceMentWord(byval strWord as string ) as string
'put your code here to find the correct translated word for the replace function
End Function
Last edited by rudvs2; Mar 26th, 2003 at 02:20 AM.
-
Mar 26th, 2003, 02:24 AM
#7
Fanatic Member
why use the regular expression instead of the string class replace function you may ask?
Well the reason being that you can dynamically get the regular expression to sort for plural values eg "car" "cars" "car's" and all that sort of thing if you build it correctly regular expressions can be emmensly powerful when applied with a bit of thought.
Anyway I hope this helps you out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|