Date Parser - Again! 
Author Message
 Date Parser - Again!

Hi again, I've posted before about this date parser I have to make. I can
now get the tokeniser to work and can check if the input string is valid.
However, I am having problems with a couple of things still. Months can be
passed into the input as words instead of numbers e.g. July, july, JUL, jul
etc. So for the program initialised as:

go( '2 January 2195 11:20:59 PM', D ).

the output should be:
D = datetime(2,1,2195,23,20,59) ?;

As you can see the time should also be converted to 24hr. I have posted my
code below, with my attempts at solving this problem. Can anyone help
please? Thanks.

conc([], L, L).

conc([X | L1], L2, [X | L3]) :-
 conc(L1, L2, L3).

concat(A1 - Z1, Z1 - Z2, A1 - Z2).

go(DateTimeString, Output) :-
 name(DateTimeString, Input),    %Get the input string and use name/2 to
turn into ascii codes
 parse(Input, [], [], Output),
 datetime( Day, Month, Year, Hour, Min, Sec, Output, [] ), !,
 Actualhour is Hour,
 DateTimeFormatted = datetime( Day, Month, Year, Actualhour, Min, Sec )
 ;
 DateTimeFormatted = error.

parse([], CurrentTL, TempL, Output) :-
 append([CurrentTL], TempL, Temp),
 revert(Temp, [], Output).

parse([32 | Tail], CurrentTL, TempL, Output) :-   %Check if the next
character is a space
 delimiter(Tail, CurrentTL, TempL, Output).  %If it is a ' ', call cspace to
append CurrentTL to TempL

parse([44 | Tail], CurrentTL, TempL, Output) :-   %Check if the next
character is a comma
 delimiter(Tail, CurrentTL, TempL, Output).  %If it is a ' ', call cspace to
append CurrentTL to TempL

parse([58 | Tail], CurrentTL, TempL, Output) :-   %Check if the next
character is a colon
 append([CurrentTL], TempL, Temp),
 append([[58]], Temp, Temp2),
 parse(Tail, [], Temp2, Output).

parse([58 | Tail], CurrentTL, TempL, Output) :-   %Check if the next
character is a slash
 append([CurrentTL], TempL, Temp),
 append([[58]], Temp, Temp2),
 parse(Tail, [], Temp2, Output).

parse([Head | Tail], CurrentTL, TempL, Output) :-
 append(CurrentTL, [Head], TempCTL),  %take the head and the saved list and
% append([Head], CurrentTL, TempCTL),
 parse(Tail, TempCTL, TempL, Output).

delimiter(Tail, [], Temp, Output):-
 parse(Tail, [], Temp, Output).

delimiter(Tail, CurrentTL, TempL, Output) :-
 append([CurrentTL], TempL, Temp),
 parse(Tail, [], Temp, Output).

revert([Head | Tail], Var, Output) :-
 name(X, Head),
 append([X], Var, Temper),
 revert(Tail, Temper, Output).

revert([], Temp, Output):-
 Output = Temp.

%cspace takes these arguments

%Head - in order to check if the current code is for a space
%CurrentTL - If the current code is for a space the CurrentTL needs to be
stored somewhere
%Tail - Once check has been called, space is called using Tail as the first
argument, so that the space is omitted
%TempL - This is the list where the CurrentTL will be stored
%Output - This will be left uninstantiated so that the complete list can be
output
%The TempL and Output must both both be saved by passing them in the space
predicate
%In order to save everything, all arguments from space are passed to check

%This version has no List

%dt(DT) -->
% time(Hrs,Min,Sec),
% date(Day,Month,Year),
% { DT = datetime(Day,Month,Year,Hrs,Min,Sec) }.

%time(Hrs,Min,Sec) -->
% [num(Hrs),colon,num(Min),colon,num(Sec)].

%date(day,month,year) -->
% [str(Month),num(Day),num(Year)].

%hrs(H)  -->[H], {H >= 0, H < 24 }.
%mins(M)  -->[M], {M >=0, M <60 }.
%year(Y)  -->[Y], {integer(Year)}.
%secs(S)  -->[S], {S >=0, S <60}.
%day(D)  -->[D], {D <32, D >= 1}.

% pos( X, List, Pos )

pos( X, [X | _], 1 ).

pos( X, [_ | Rest], Pos ) :-
 pos( X, Rest, Restpos ),
 Pos is Restpos + 1.

convertmonth( Textmonth, Nummonth ) :-
 pos( Textmonth, [ jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov,
dec,
   'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT',
'NOV', 'DEC',
   january, february, march, april, may, june, july, august, september,
october, november, december,
   'January', 'February', 'March', 'April', 'May', 'June', 'July',
   'August', 'September', 'October', 'November', 'December'        ],
  Position),
 Monthnumber is Position mod 12,
 Monthnumber > 0, !,
 Nummonth = Monthnumber.

convertmonth( Textmonth, 12 ) :-
 member( Textmonth, [ jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov,
dec,
   'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT',
'NOV', 'DEC',
   january, february, march, april, may, june, july, august, september,
october, november, december,
   'January', 'February', 'March', 'April', 'May', 'June', 'July',
   'August', 'September', 'October', 'November', 'December'        ]).

datetime(Day, Nummonth, Year, Hour, Min, Sec) --> date(Day, Nummonth, Year),
time(Hour, Min, Sec).
datetime(Day, Nummonth, Year, Hour, Min, Sec) --> time(Hour, Min, Sec),
date(Day, Nummonth, Year).

time(Hour, Min, Sec) --> timestring(Hour, Min, Sec).
time(Hour, Min, Sec) --> timestring(Hour, Min, Sec), ['AM'], {integer(Hour),
!, Hour =< 12, Hour >= 1}.
time(Hour24, Min, Sec) --> timestring(Hour, Min, Sec), ['PM'],
{integer(Hour), !, Hour =< 12, Hour >= 1, Hour24 = Hour + 12}.

timestring(Hour, Min, Sec) --> hour(Hour), [:], min(Min), [:], sec(Sec).
timestring(Hour, Min, 0) --> hour(Hour), [:], min(Min).

date(Day, Nummonth, Year) --> day(Day), textmonth(Nummonth), year(Year).
date(Day, Nummonth, Year) --> textmonth(Nummonth), day(Day), year(Year).
date(Day, Nummonth, Year) --> year(Year), textmonth(Nummonth), day(Day).
date(Day, Nummonth, Year) --> year(Year), day(Day), textmonth(Nummonth).
date(Day, Nummonth, Year) --> day(Day), ['/'], nummonth(Nummonth), ['/'],
year(Year).

hour(Hour) --> [Hour], {integer( Hour ), !, Hour =< 23, Hour >= 0}.

min(Min) --> [Min], {integer( Min ), !, Min < 60, Min >= 0}.

sec(Sec) --> [Sec], {integer(Sec), !, Sec < 60, Sec >= 0}.

day(Day) --> [Day], {integer( Day ), !, Day <32, Day >= 1}.

nummonth(Nummonth) --> [Nummonth], {integer( Nummonth), !, Nummonth =< 12,
Nummonth >= 1}.

year(Year) --> [Year], {integer( Year )}.

textmonth(Nummonth) --> [Textmonth], {convertmonth( Textmonth, Nummonth )}.



Fri, 28 May 2004 02:47:46 GMT  
 Date Parser - Again!

Quote:

> [...]
> As you can see the time should also be converted to 24hr. I have posted my
> code below, with my attempts at solving this problem. Can anyone help
> please? Thanks.
> [...]
> convertmonth( Textmonth, Nummonth ) :-
>  pos( Textmonth, [ jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov,
> [...]
> {integer(Hour), !, Hour =< 12, Hour >= 1, Hour24 = Hour + 12}.

It's probably easier to use a table, eg:

month(jan,1).
month(feb,2).
month('January',1).
[...]

Addition in Prolog looks like this:

| Hour24 is Hour + 12

`=' means unification.

/Tomas



Fri, 28 May 2004 16:46:13 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Scheme vs ML again and again and again and again

2. Scheme vs ML again and again and again and again

3. Date Parser Tokeniser

4. Date Parser

5. date parser

6. date's ones again

7. date conversion again ...

8. New SIOD update, with -*-parser:fasl-*- and -*-parser:read-*-

9. New SIOD update, with -*-parser:fasl-*- and -*-parser:read-*-

10. APL and J (again and again) (long)

11. APL and J (again and again)

12. 'SQL Server Login': Again, again, ...

 

 
Powered by phpBB® Forum Software