Android Question B4A Code for python project

Kadandale Ganapathy Bhat

Member
Licensed User
Dear everybody
Here is python project to convert the script.
It will be very helpful if anybody convert this to b4a project
Thank you
 

Attachments

  • devanagari-to-roman-script-transliteration-master.zip
    17.5 KB · Views: 130

DonManfred

Expert
Licensed User
Longtime User
i don´t think you´ll get any help in porting pythoncode to b4x as you can not use python in b4x.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
It seems not so difficult to do the conversion.
1. createDict.py -> write 2 csv files as output from 2 lists of maps. These 2 files already generated inside the attachment.
2. runTransliteration.py -> read these files and using for loop, load the values into 2 arrays. It executes by passing a filename as an input argument, e.g sample and output as filename + OUTPUT e.g. sampleOUTPUT

The script read every character and replace it with the Roman letter using For loop and if-else from the loaded key-value from the arrays / maps.
 
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
Dear everybody
Here is python project to convert the script.
It will be very helpful if anybody convert this to b4a project
Thank you
I use Cursor to convert it, but it may not work.

Converting Python code to B4A (Basic4android) code is not straightforward due to the differences in syntax, libraries, and overall language design. However, I can provide a rough equivalent of your Python code in B4A.
Please note that B4A doesn't have a direct equivalent for Python's CSV reader or dictionary. So, we'll use Lists and Maps instead. Also, B4A doesn't support command-line arguments, so we'll use hardcoded filenames for simplicity.

runTransliteration.py:
Sub Process_Globals
    Dim vowels As Map
    Dim consonants As Map
    Dim content As List
    Dim str1 As String
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    vowels = LoadCSV("svar.csv")
    consonants = LoadCSV("vyanjan.csv")
    content = File.ReadList(File.DirRootExternal, "input.txt")
    str1 = ""
    
    For Each x As String In content
        Dim words() As String = Regex.Split(" ", x)
        For Each y As String In words
            For i = 0 To y.Length - 1
                Dim c As String = y.CharAt(i)
                Dim p As Int = 1
                If i + 1 < y.Length And y.CharAt(i + 1) = " ़" Then
                    c = c & y.CharAt(i + 1)
                    p = 2
                End If
                If vowels.ContainsKey(c) Then
                    str1 = str1 & vowels.Get(c)
                Else If consonants.ContainsKey(c) Then
                    If i + p < y.Length And consonants.ContainsKey(y.CharAt(i + p)) Then
                        If (c = "झ" And i <> 0) Or (i <> 0 And i + p + 1 < y.Length And vowels.ContainsKey(y.CharAt(i + p + 1))) Then
                            str1 = str1 & consonants.Get(c)
                        Else
                            str1 = str1 & consonants.Get(c) & "a"
                        End If
                    Else
                        str1 = str1 & consonants.Get(c)
                    End If
                Else If " \n\t!,।-:\\_?".Contains(c) Or IsAlphaNumeric(c) Then
                    str1 = str1 & c.Replace("।", ".")
                End If
            Next
            str1 = str1 & " "
        Next
        str1 = str1 & CRLF
    Next
    
    File.WriteString(File.DirRootExternal, "output.txt", str1.Trim)
    Log("DONE! Open output.txt")
End Sub

Sub LoadCSV(filename As String) As Map
    Dim Map1 As Map
    Map1.Initialize
    Dim lines As List
    lines = File.ReadList(File.DirRootExternal, filename)
    For Each line As String In lines
        Dim parts() As String = Regex.Split(",", line)
        Map1.Put(parts(0), parts(1))
    Next
    Return Map1
End Sub

Sub IsAlphaNumeric(c As String) As Boolean
    Return c >= "0" And c <= "9" Or c >= "A" And c <= "Z" Or c >= "a" And c <= "z"
End Sub




createDict.py:
Sub Process_Globals
    Dim vowels As Map
    Dim consonants As Map
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    vowels.Initialize
    consonants.Initialize

    vowels.Put("ँ", "n")
    vowels.Put("ं", "n")
    '... add the rest of the vowels here ...

    consonants.Put("क", "k")
    consonants.Put("ख", "kh")
    '... add the rest of the consonants here ...

    WriteCSV("svar.csv", vowels)
    WriteCSV("vyanjan.csv", consonants)
End Sub

Sub WriteCSV(filename As String, Map1 As Map)
    Dim sb As StringBuilder
    sb.Initialize
    For Each k As String In Map1.Keys
        sb.Append(k).Append(",").Append(Map1.Get(k)).Append(CRLF)
    Next
    File.WriteString(File.DirRootExternal, filename, sb.ToString)
End Sub
 
Upvote 0

Kadandale Ganapathy Bhat

Member
Licensed User
I use Cursor to convert it, but it may not work.

Converting Python code to B4A (Basic4android) code is not straightforward due to the differences in syntax, libraries, and overall language design. However, I can provide a rough equivalent of your Python code in B4A.
Please note that B4A doesn't have a direct equivalent for Python's CSV reader or dictionary. So, we'll use Lists and Maps instead. Also, B4A doesn't support command-line arguments, so we'll use hardcoded filenames for simplicity.

runTransliteration.py:
Sub Process_Globals
    Dim vowels As Map
    Dim consonants As Map
    Dim content As List
    Dim str1 As String
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    vowels = LoadCSV("svar.csv")
    consonants = LoadCSV("vyanjan.csv")
    content = File.ReadList(File.DirRootExternal, "input.txt")
    str1 = ""
   
    For Each x As String In content
        Dim words() As String = Regex.Split(" ", x)
        For Each y As String In words
            For i = 0 To y.Length - 1
                Dim c As String = y.CharAt(i)
                Dim p As Int = 1
                If i + 1 < y.Length And y.CharAt(i + 1) = " ़" Then
                    c = c & y.CharAt(i + 1)
                    p = 2
                End If
                If vowels.ContainsKey(c) Then
                    str1 = str1 & vowels.Get(c)
                Else If consonants.ContainsKey(c) Then
                    If i + p < y.Length And consonants.ContainsKey(y.CharAt(i + p)) Then
                        If (c = "झ" And i <> 0) Or (i <> 0 And i + p + 1 < y.Length And vowels.ContainsKey(y.CharAt(i + p + 1))) Then
                            str1 = str1 & consonants.Get(c)
                        Else
                            str1 = str1 & consonants.Get(c) & "a"
                        End If
                    Else
                        str1 = str1 & consonants.Get(c)
                    End If
                Else If " \n\t!,।-:\\_?".Contains(c) Or IsAlphaNumeric(c) Then
                    str1 = str1 & c.Replace("।", ".")
                End If
            Next
            str1 = str1 & " "
        Next
        str1 = str1 & CRLF
    Next
   
    File.WriteString(File.DirRootExternal, "output.txt", str1.Trim)
    Log("DONE! Open output.txt")
End Sub

Sub LoadCSV(filename As String) As Map
    Dim Map1 As Map
    Map1.Initialize
    Dim lines As List
    lines = File.ReadList(File.DirRootExternal, filename)
    For Each line As String In lines
        Dim parts() As String = Regex.Split(",", line)
        Map1.Put(parts(0), parts(1))
    Next
    Return Map1
End Sub

Sub IsAlphaNumeric(c As String) As Boolean
    Return c >= "0" And c <= "9" Or c >= "A" And c <= "Z" Or c >= "a" And c <= "z"
End Sub




createDict.py:
Sub Process_Globals
    Dim vowels As Map
    Dim consonants As Map
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    vowels.Initialize
    consonants.Initialize

    vowels.Put("ँ", "n")
    vowels.Put("ं", "n")
    '... add the rest of the vowels here ...

    consonants.Put("क", "k")
    consonants.Put("ख", "kh")
    '... add the rest of the consonants here ...

    WriteCSV("svar.csv", vowels)
    WriteCSV("vyanjan.csv", consonants)
End Sub

Sub WriteCSV(filename As String, Map1 As Map)
    Dim sb As StringBuilder
    sb.Initialize
    For Each k As String In Map1.Keys
        sb.Append(k).Append(",").Append(Map1.Get(k)).Append(CRLF)
    Next
    File.WriteString(File.DirRootExternal, filename, sb.ToString)
End Sub
thank you
it works for me
 
Upvote 0
Top