Results 1 to 7 of 7

Thread: I'm making a Translator, but i need help on this part

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2003
    Location
    colorado
    Posts
    17

    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:
    1. Dim Length As Integer
    2.     Dim x As Integer
    3.     Dim y As Integer
    4.     Dim word As String
    5.     Dim sentence As String
    6.     Dim xa As String
    7.     Dim ya As String
    8.  
    9.     Private Sub Button1_Click(............) Handles Button1.Click
    10.         sentence = ""
    11.         Length = Len(TextBox1.Text)
    12.         For x = 1 To Length
    13.             xa = Mid(TextBox1.Text, 1, x)
    14.             If xa = " " Then
    15.                 For y = 1 To x Step -1
    16.                     ya = Mid(TextBox1.Text, x, 1)
    17.                     If ya = " " Then
    18.                         word = Mid(TextBox1.Text, y + 1, x - 1)
    19.                         If word = " Je " Then
    20.                             sentence = sentence + " I "
    21.                             TextBox2.Text = sentence
    22.                         End If
    23.                     End If
    24.                 Next
    25.             End If
    26.         Next
    27.     End Sub
    28.  
    29. End Class

  2. #2
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         TextBox2.Text = translator(TextBox1.Text)
    3.     End Sub
    4.  
    5.     Private Function translator(ByVal str As String) As String
    6.         Dim regex As New System.Text.RegularExpressions.Regex("je\s")
    7.         Return regex.Replace(str, "I ")
    8.     End Function

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2003
    Location
    colorado
    Posts
    17
    oh thanks it's really hard transitioning to vb.NET

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2003
    Location
    colorado
    Posts
    17
    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.

  5. #5
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    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

  6. #6
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    for a quick demo of how to do more than one word you could try this approach

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2. dim stringArray() as string = textbox1.text.split(" ")
    3. dim intLoop as integer
    4. dim NewString as string = textbox1.text
    5. for intLoop = 0 to stringArray.length - 1      
    6. newstring  = translator(newstring,stringArray(intLoop),GetReplaceMentWord(stringArray(intLoop)))
    7. Next intloop
    8. textBox2.text = newstring
    9.     End Sub
    10.  
    11.     Private Function translator(ByVal str As String, byval strSearchWord as string, byVal strReplaceWord as string) As String
    12.         Dim regex As New System.Text.RegularExpressions.Regex(strsearchword & "\s")
    13.         Return regex.Replace(str, strReplaceWord)
    14.     End Function
    15.  
    16.  
    17. Private Function GetReplaceMentWord(byval strWord as string ) as string
    18. 'put your code here to find the correct translated word for the replace function
    19. End Function
    Last edited by rudvs2; Mar 26th, 2003 at 02:20 AM.

  7. #7
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    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
  •  



Click Here to Expand Forum to Full Width