Android Question BCTextEngine parser: any word as keyword

peacemaker

Expert
Licensed User
Longtime User
HI, All

I try to make some words as the links by this parser class.

B4X:
Public Sub Initialize (TextEngine As BCTextEngine, kwords As List)
    mTextEngine = TextEngine
    Keywords = B4XCollections.CreateSet
    For Each keyword As String In kwords
        Keywords.Add(keyword.ToLowerCase)
    Next
End Sub

Custom keywords are used, but... some of them are the root of some words, part of a word. So i need to parse (make as the clickable link) some part of a word. Say, "word" keyword is used to be marked in the text "some words about it".
Without separators to be checked.
How to do it ?
 

peacemaker

Expert
Licensed User
Longtime User
Parsing is solved:
It's now searching the keywords inside each word, but initially is was otherwise.
Now it needs to update the text to have the parsed keywords into the links.

B4X:
Private Sub DecideWordType (Line As String, Word As String, i As Int, Runs As List) As Int
    Dim WordStart As Int = i - Word.Length
    Dim char0 As String = Word.CharAt(0)
    If char0 = "'" Then
        CreateRun(Line.SubString2(WordStart, Line.Length), TOKEN_COMMENT, Runs)
        i = Line.Length
    Else If char0 = """" Then
        Dim i2 As Int = WordStart + 1
        Do While i2 < Line.Length
            If Line.CharAt(i2) = """" Then
                If i2 = Line.Length - 1 Or Line.CharAt(i2 + 1) <> """" Then
                    Exit
                Else If i2 < Line.Length - 1 Then
                    i2 = i2 + 1
                End If
            End If
            i2 = i2 + 1
        Loop
        CreateRun(Line.SubString2(WordStart, i2 + 1), TOKEN_STRING, Runs)
        i = i2 + 1
    Else If Regex.IsMatch("\d", char0) Then
        Dim m As Matcher = Regex.Matcher("(0[Xx])([0123456789aAbBcCdDeEfF])+|(\d+([Ee][+-]?\d+)?(dip|%[xX]|%[yY])?)", Line.SubString(WordStart))
        If m.Find And m.GetStart(0) = 0 Then
            i = WordStart + m.GetEnd(0)
            Dim DotOffset As Int = 0
            If WordStart > 0 And Line.CharAt(WordStart - 1) = "." Then
                DotOffset = 1
                Runs.RemoveAt(Runs.Size - 1)
            End If
            CreateRun(Line.SubString2(WordStart - DotOffset, WordStart + m.GetEnd(0)), TOKEN_NUMBER, Runs)
        End If
    Else If Regex.IsMatch("\w", char0) Then
        Dim Added As Boolean
        Dim Found As Boolean
        Dim KeywordsList As List = Keywords.AsList
        For j = 0 To KeywordsList.Size - 1
            If Word.ToLowerCase.Contains(KeywordsList.Get(j)) Then
                Found = True
                Exit
            End If
        Next
        If Found Then
            CreateRun(Word, TOKEN_KEYWORD, Runs)
            Added = True
        Else If Runs.Size > 1 Then
            Dim prev As BCTextRun = Runs.Get(Runs.Size - 2)
            If prev.Tag = TOKEN_KEYWORD Then
                Dim PrevLower As String = prev.Text.ToLowerCase
                If PrevLower = "as" Or PrevLower = "is" Or PrevLower = "type" Then
                    CreateRun(Word, TOKEN_TYPE, Runs)
                    Added = True
                Else If PrevLower = "sub" Then
                    CreateRun(Word, TOKEN_SUBNAME, Runs)
                    Added = True
                End If
            End If
        End If
        If Added = False Then CreateRun(Word, TOKEN_IDENTIFIER, Runs)
    Else If char0 = "#" Then
        CreateRun(Word, TOKEN_ATTRIBUTE, Runs)
        CreateRun(Line.SubString(WordStart + Word.Length), TOKEN_STRING, Runs)
        i = Line.Length
    Else
        If Runs.Size > 0 Then
            Dim prev As BCTextRun = Runs.Get(Runs.Size - 1)
            If prev.Tag = TOKEN_DEFAULT Then
                Runs.RemoveAt(Runs.Size - 1)
                Word = prev.Text & Word
            End If
        End If
        CreateRun(Word, TOKEN_DEFAULT, Runs)
    End If
    Return i
End Sub
 
Last edited:
Upvote 0
Top