We help IT Professionals succeed at work.

translation question

gorexy
gorexy asked
on
Medium Priority
345 Views
Last Modified: 2012-03-15

HI,
  I want to perform a small translation program.  
The program can read the input file in this format:

1.Wo an a i ei ...
2....

and the output will become
1.ab ei i i eI (for example)
2....

That means each unit will be translated into a new string.
The following is what I did so far.

However, I don't know how to separate each unit by space and so I can compare each unit.  

My code now got a problem.  When the input is a and then ai , the code can just replace ai into ai (e.g.) instead of Ai (e.g.) since a -> a.

''''''''''code begin'''''
Private Sub Command1_Click()
Load Form1
Form1.Show
End Sub

Private Sub Command2_Click()
Load Form1
Output = True
Form1.Show
End Sub

Private Sub Command3_Click()
Dim pathandname As String
Dim filesize As String
Dim path As String
Dim mystring As String

If Text1.Text = "" Then
MsgBox "You must first select a file!"
Exit Sub
End If

'If Right(Form1.File1.path, 1) <> "\" Then
'path = Form1.File1.path + "\"
'Else
'path = Form1.File1.path
'End If

'If Text1.Text = Form1.File1.FileName Then
'pathandname = path + Form1.File1.FileName
'Else
'pathandname = Text1.Text
'End If

filenum = FreeFile
Open Text1.Text For Input As filenum
filenum2 = FreeFile
Open Text2.Text For Output As filenum2

Do While Not EOF(filenum)
Line Input #filenum, mystring
Text1.Font.Name = "Ipa-samd uclphonl"
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
mystring = ReplaceInString(mystring, "b", "p") '1
mystring = ReplaceInString(mystring, "p", "pH") '2
mystring = ReplaceInString(mystring, "m", "m") '3
mystring = ReplaceInString(mystring, "f", "f") '4
mystring = ReplaceInString(mystring, "d", "t") '5
mystring = ReplaceInString(mystring, "t", "tH") '6
mystring = ReplaceInString(mystring, "n", "n") '7
mystring = ReplaceInString(mystring, "l", "l") '8
mystring = ReplaceInString(mystring, "z", "ts") '9
mystring = ReplaceInString(mystring, "c", "tsH") '10
mystring = ReplaceInString(mystring, "s", "s") '11
mystring = ReplaceInString(mystring, "j", "tC") '12
mystring = ReplaceInString(mystring, "q", "tCH") '13
mystring = ReplaceInString(mystring, "x", "C") '14
mystring = ReplaceInString(mystring, "g", "k") '15
mystring = ReplaceInString(mystring, "k", "kH") '16
mystring = ReplaceInString(mystring, "h", "x") '17
mystring = ReplaceInString(mystring, "zh", "chr$(0167)") '18
mystring = ReplaceInString(mystring, "ch", "chr$(0167)") '19
mystring = ReplaceInString(mystring, "sh", "chr$(0167)") '20
mystring = ReplaceInString(mystring, "r", "r") '21
mystring = ReplaceInString(mystring, "ng", Chr(Asc("n") - 32)) '22
mystring = ReplaceInString(mystring, "a", Chr(Asc("a") - 32)) '23
mystring = ReplaceInString(mystring, "o", "o") '24
mystring = ReplaceInString(mystring, "e", "e") '25
mystring = ReplaceInString(mystring, "i", "i") '26
mystring = ReplaceInString(mystring, "u", "u") '27
mystring = ReplaceInString(mystring, "ul", "y") '28
mystring = ReplaceInString(mystring, "er", "y") '29
mystring = ReplaceInString(mystring, "ai", Chr(Asc("a") - 32) + i + "super") '30
mystring = ReplaceInString(mystring, "ei", Chr(Asc("e") - 32) + i + "super") '31
mystring = ReplaceInString(mystring, "ao", Chr(Asc("a") - 32) + u + "super") '32
mystring = ReplaceInString(mystring, "ai", Chr(Asc("o") - 32) + u + "super") '33
mystring = ReplaceInString(mystring, "an", "<")   '34
mystring = ReplaceInString(mystring, "en", "F<") '35
mystring = ReplaceInString(mystring, "ang", Chr(Asc("a") - 32) + Chr$(212))   '36

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Print #filenum2, mystring
Loop

Close filenum
Close filenum2

End Sub

Function ReplaceInString(orgstring As String, orgchar As String, newchar As String) As String
Dim interarr As Variant
Dim i As Integer

ReplaceInString = ""
interarr = Split(orgstring, orgchar)
For i = 0 To UBound(interarr)
  If i <> 0 Then ReplaceInString = ReplaceInString & newchar
  ReplaceInString = ReplaceInString & interarr(i)
 
Next i
End Function

''''''code end'''''

Comment
Watch Question

Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Hi gorexy,

Please check the "Checking Algorithm" in your code to ensure no problem within.

And you can replace the "ReplaceInString" function with "Replace" function if you're using VB6

regards.

Author

Commented:
sorry can you make clear?
My code above has no prolem to run.

and why I need to replace the replaceinstring into replace?

Yes I am using VB6 with SP5
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Hi,

> why I need to replace the replaceinstring into replace?
Just a suggestion, bcos you don't need a Sub to comparing the string.

Replace function in VB:

Description

Returns a string in which a specified substring has been replaced with another substring a specified number of times.

Syntax

Replace(expression, find, replacewith[, start[, count[, compare]]])

Author

Commented:
sorry can you make clear?
My code above has no prolem to run.

and why I need to replace the replaceinstring into replace?

Yes I am using VB6 with SP5

Author

Commented:
oic...but I am not familiar with the replace function,
Can you show me how can I plug the replace function into my code?
Commented:
I think ryancys was on target with his answer, but maybe what you want is the split each word, perform a replace, then merge the pieces back together.  Try this:

Private Function TranslateText(ByVal OriginalText as string) as String
   dim strWordList() as string
   dim intWordCntr as integer
   dim strWord as string

   ' split each "word" into an array element
   strWordList = Split(OriginalText, " ")
   for intWordCntr=0 to ubound(strWordList)
      strWord = strWordList(intWordCntr)
      strWord = MakeTranslation(strWord)
      strWordList(intWordCntr) = strWord
   next intWordCntr

   ' rebuild the text string, with "words" separated by spaced
   TranslateText = join(strWordList, " ")
end function

Private Function MakeTranslation(WordToTranslate as string) as string

' place all translation choices here
   select case WordToTranslate
      case "b"
        MakeTranslation = "p"
      case "p"
        MakeTranslation = "pH"
      case "m"
        MakeTranslation = "m"
      case ...
      case else
        MakeTranslation = string$("*", len(WordToTranslate))
   end select
end function


...
and to use it, do something like this:

Private Sub Command3_Click()
'...
Do While Not EOF(filenum)
   Line Input #filenum, mystring
   Text1.Font.Name = "Ipa-samd uclphonl"
   Print #filenum2, TranslateText(mystring)
Loop
'...
end sub
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Hi gorexy,

Anyway,

> Can you show me how can I plug the replace function into my code?

Example:

mystring = Replace(mystring, "en", "F<") '35

But

According to your situation, use the Replace function may not be the first choice, make sure your checking algorithm is correct so that no characters is overlapping..

And rsphahitz 's Example which shows the "Select Case" statement is more reasonable.

regards
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Hi gorexy,

Anyway,

> Can you show me how can I plug the replace function into my code?

Example:

mystring = Replace(mystring, "en", "F<") '35

But

According to your situation, use the Replace function may not be the first choice, make sure your checking algorithm is correct so that no characters is overlapping..

And rsphahitz 's Example which shows the "Select Case" statement is more reasonable.

regards
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Hi gorexy,

Anyway,

> Can you show me how can I plug the replace function into my code?

Example:

mystring = Replace(mystring, "en", "F<") '35

But

According to your situation, use the Replace function may not be the first choice, make sure your checking algorithm is correct so that no characters is overlapping..

And rsphahitz 's Example which shows the "Select Case" statement is more reasonable.

regards
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
<sorry for multiple postings..>

Author

Commented:
yup excellent , exactly what I need.
Thanks
(and I learn something new to me too)

Commented:
Glad to help again!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.