Android Question Parsing a date with complex conditions using AI

Sergey_New

Well-Known Member
Licensed User
Longtime User
The date is represented as, for example, "BET 1 JAN 1852 AND 31 DEC 1852".
I decided to try using DeepSeek neural network.
The task was to parse a date and date range using the B4A language.
Conditions:
The year of the date contains 4 digits. To calculate the number of digits, leading zeros should not be counted.
The month name must be one of the 3 specified Latin characters in uppercase.
The day of the date must be from 1 to 31. Leap years must be taken into account.
The date may have prefixes.
For a single date: ABT, CAL, EST, AFT, BEF, FROM.
For the first date of the date range: ABT, CAL, EST, AFT, BEF, BET, FROM.
For the second date of the date range: TO, AND.
The BET prefix of the first date must be used with the AND prefix of the second date.
The FROM prefix of the first date must be used with the TO prefix of the second date.
For each date, five string values must be returned: prefix, day, month, year, and the parsing success result. If any value is missing, a blank is returned.

The problem was almost completely solved in the few hours it took me to clarify the requirements and test the code.
I received the parser and activity codes with comments in the native language.
Ultimately, the AI was unable to correctly attribute the parsing error of the second date prefix to the second date, assigning it to the first date in the range.
If you are interested in the resulting code, let me know, and I'll post it.
 

aeric

Expert
Licensed User
Longtime User
If you make it complex then it will become complex.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Without AI, here is my human idea.
1. Split the string to 2 if there is occurrence for " AND " or " TO ".
If size of array > 1 then we have 2 dates.
2. Process the first date prefix by searching for the keywords such as "ABT", ..., "BET", "FROM".
3. Parse the date(s).
4. Unmatched conditions return empty.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Some times, it is faster to think for a solution and better to train ourselves not to rely on AI.
I'm more interested in working on the program's functionality and not being distracted by routine procedures that AI can handle.
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
Does BEF JAN 1 2026 include 01-JAN-2026?

Does AFT JAN 1 2026 include 01-JAN-2026?

Does FROM JAN 1 2026 include 01-JAN-2026?
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Are you interested in knowing all the conditions for determining the date period?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Are you interested in knowing all the conditions for determining the date period?
You are wondering if there is anyone interested to know how you have solved this problem.
I think not much people will be interested because your requirement is very specific to your use case.
Meanwhile I guess you are also not interested on other better solutions.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
I was talking about the solution I had received, not about not being interested in other solutions. Perhaps Google Translate misinterpreted what I said.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I was talking about the solution I had received, not about not being interested in other solutions. Perhaps Google Translate misinterpreted what I said.
You can ask the members for alternate solutions even you don't want to disclose the AI answer.
I can suggest a solution.
And I already did.
You want the code?
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
This is the resulting code. I wrote about the comment language in the first post.
 

Attachments

  • testDate.zip
    9.3 KB · Views: 15
Upvote 0

aeric

Expert
Licensed User
Longtime User
I attach a B4J test project.
It should also work in B4A.

By the way, my keyboard is spoilt. 🥲 I was taking time to create this project.

Note: Requires DateUtils library for GetMonthName.

B4X:
Sub Process_Globals
    Private Prefixes As List = Array As String("ABT", "CAL", "EST", "AFT", "BEF", "BET", "FROM")
    Private PrefixesWithInfix As List = Array As String("BET", "FROM")
    Private Infixes As List = Array As String("AND", "TO")
    Type MyDate (Prefix As String, Day As String, Month As String, Year As String, Text As String)
End Sub

Sub AppStart (Args() As String)
    DateTime.DateFormat = "dd MMM yyyy"
    ParseText("BET 1 JAN 1852 AND 31 DEC 1852")
    ParseText("ABT 3 SEP 1852") ' Note: SEP is not parseable in UK english locale
    ParseText("FROM 31 DEC 1852")
    ParseText("FROM 1 FEB 2025 AND 30 NOV 2025")
    ParseText("BET 1 FEB 2026 TO 30 NOV 2026")
    ParseText("FROM 1 FEB 2025 TO 30 NOV 2025")
    ParseText("BET 1 FEB 2026 AND 30 NOV 2026")
End Sub

Private Sub ParseText (Text As String)
    Dim Date1, Date2 As MyDate
    Dim Prefix As String
    Dim Infix As String = GetInfix(Text)
    If Infix <> "" Then
        Dim DateParts() As String = Regex.Split($" ${Infix} "$, Text)
        Prefix = GetPrefix(DateParts(0))
      
        ' Checking matching prefix to infix with simple if-else
        If Prefix = "FROM" And Infix <> "TO" Then
            Log("Invalid date! " & Text)
            Return
        End If
        If Prefix = "BET" And Infix <> "AND" Then
            Log("Invalid date! " & Text)
            Return
        End If
      
        DateParts(0) = DateParts(0).SubString(Prefix.Length + 1)
        Date1 = ParseDate(Prefix, DateParts(0))
        Log($"Date1=(${Date1.Prefix}, ${Date1.Day}, ${Date1.Month}, ${Date1.Year}, ${Date1.Text})"$)
      
        Date2 = ParseDate(Infix, DateParts(1))
        Log($"Date2=(${Date2.Prefix}, ${Date2.Day}, ${Date2.Month}, ${Date2.Year}, ${Date2.Text})"$)
    Else     
        Prefix = GetPrefix(Text)
        If PrefixesWithInfix.IndexOf(Prefix) > -1 Then
            Log("Invalid date! " & Text)
            Return
        End If
        Dim DateParts() As String = Array As String(Text.SubString(Prefix.Length + 1))
        Date1 = ParseDate(Prefix, DateParts(0))
        If Date1.IsInitialized Then
            Log($"Date1=(${Date1.Prefix}, ${Date1.Day}, ${Date1.Month}, ${Date1.Year}, ${Date1.Text})"$)
        Else
            Log("Invalid date! " & Text)
        End If
    End If
End Sub

Private Sub GetInfix (Text As String) As String
    For Each Infix As String In Infixes
        If Text.Contains($" ${Infix} "$) Then
            Return Infix
        End If
    Next
    Return ""
End Sub

Private Sub GetPrefix (Text As String) As String
    For Each Prefix As String In Prefixes
        If Text.StartsWith(Prefix) Then
            Return Prefix
        End If
    Next
    Return ""
End Sub

Private Sub ParseDate (Fix As String, DateString As String) As MyDate
    Dim TestDate As MyDate
    Try
        ' Fix Sept
        DateString = DateString.Replace(" SEP ", " SEPT ")
        Dim Ticks As Long =  DateTime.DateParse(DateString)
        DateString = DateString.Replace(" SEPT ", " SEP ")
        Return CreateMyDate(Fix, GetDay(Ticks), GetMonth(Ticks), GetYear(Ticks), DateString)
    Catch
        Log(LastException)
        Return TestDate
    End Try
End Sub

Private Sub GetDay (DateTicks As Long) As String
    Return DateTime.GetDayOfMonth(DateTicks)
End Sub

Private Sub GetMonth (DateTicks As Long) As String
    'Return DateTime.GetMonth(DateTicks)
    Return DateUtils.GetMonthName(DateTicks).SubString2(0, 3).ToUpperCase
End Sub

Private Sub GetYear (DateTicks As Long) As String
    Return DateTime.GetYear(DateTicks)
End Sub

Public Sub CreateMyDate (Prefix As String, Day As String, Month As String, Year As String, Text As String) As MyDate
    Dim t1 As MyDate
    t1.Initialize
    t1.Prefix = Prefix
    t1.Day = Day
    t1.Month = Month
    t1.Year = Year
    t1.Text = Text
    Return t1
End Sub

Edit: Zip updated with a bug in line 38 code above.
 

Attachments

  • NotComplexDateParse.zip
    1.8 KB · Views: 15
Last edited:
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Your code should work like this
These date values are correct:
ParseText("BET 1 JAN 1852 AND 31 DEC 1852")
ParseText("ABT 3 SEP 1852")
ParseText("FROM 31 DEC 1852")
These date values are incorrect:
ParseText("FROM 1 FEB 2025 AND 30 NOV 2025")
ParseText("BET 1 FEB 2026 TO 30 NOV 2026")
These date values are correct:
ParseText("FROM 1 FEB 2025 TO 30 NOV 2025")
ParseText("BET 1 FEB 2026 AND 30 NOV 2026")
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
These date values are incorrect:
ParseText("FROM 1 FEB 2025 AND 30 NOV 2025")
ParseText("BET 1 FEB 2026 TO 30 NOV 2026")
yes, this is expected right?

FROM ... AND .. ❌
BET ... TO .. ❌

FROM ... TO .. ✅
BET ... AND .. ✅
 
Upvote 0
Top