Results 1 to 1 of 1

Thread: [twinBASIC] Using Windows ELS API to recognize language of text

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,279

    [twinBASIC] Using Windows ELS API to recognize language of text

    Name:  rl1.jpg
Views: 329
Size:  19.1 KB Name:  rl2.jpg
Views: 320
Size:  18.2 KB


    This is just a small snippet of basic language recognition with the built in Windows API MappingRecognizeText.

    It returns codes like en for English and fr for French.

    Requirements
    Windows 7+
    Package: "Windows Development Library for twinBASIC" (WinDevLib) v8.12.530+
    (Project->References->Available Packages tab)

    Code:
         Private Function RecognizeLanguage(ByVal sText As String, pMatches As String()) As Long
            Dim EnumOptions As MAPPING_ENUM_OPTIONS
            Dim prgServices As LongPtr 'PMAPPING_SERVICE_INFO   
            Dim dwServicesCount As Long
            Dim hr As Long
            Dim gSvc As UUID
            
            gSvc = ELS_GUID_LANGUAGE_DETECTION
            EnumOptions.Size = LenB(EnumOptions)
            EnumOptions.pGuid = VarPtr(gSvc)
            
            hr = MappingGetServices(EnumOptions, prgServices, dwServicesCount)
            
            If SUCCEEDED(hr) Then
                Dim bag As MAPPING_PROPERTY_BAG
                Dim pService As MAPPING_SERVICE_INFO = CType(Of MAPPING_SERVICE_INFO)(prgServices)
                
                bag.Size = LenB(bag)
                hr = MappingRecognizeText(pService, sText, Len(sText), 0, vbNullPtr, bag)
                If SUCCEEDED(hr) Then
                    Dim pRange As MAPPING_DATA_RANGE = CType(Of MAPPING_DATA_RANGE)(bag.prgResultRanges)
                    Dim cch As LongPtr
                    Dim offset As LongPtr
                    Dim sRes As String, nRes As Long
                    Do
                        cch = wcslen(pRange.pData + offset)
                        If cch = 0 Then Exit Do
                        sRes = LPWSTRtoStr(pRange.pData + offset, False)
                        ReDim Preserve pMatches(nRes)
                        pMatches(nRes) = sRes
                        nRes += 1
                        offset += cch * 2 + 2
                    Loop
                    MappingFreePropertyBag(bag)
                Else
                    Debug.Print "MappingRecognizeText error 0x" & Hex$(hr) & ", " & GetSystemErrorString(hr)
                End If
                MappingFreeServices(prgServices)
            Else
                Debug.Print "MappingGetServices error 0x" & Hex$(hr) & ", " & GetSystemErrorString(hr)
            End If
            Return nRes
        End Function
    Usage example:


    Code:
        Private Sub Command1_Click() Handles Command1.Click
            Dim sLang() As String
            Dim sOut As String
            If RecognizeLanguage(Text1.Text, sLang) Then
                For i As Long = 0 To UBound(sLang)
                    If i = 0 Then
                        sOut = "Best match: " & sLang(0)
                    Else
                        sOut &= vbCrLf & "Other result " & i & ": " & sLang(i)
                    End If
                Next
                Text2.Text = sOut
            End If
        End Sub
    Download sample project in Pic from GitHub repo
    Last edited by fafalone; May 11th, 2025 at 11:33 AM.

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