I am having some problem to implement a predicate that do some operation on a list.
I explain what I have to do with a practical example:
The predicate have to work on a list that have the following form:
TokenList =[t(1, "word1"), t(2, "Word2"), t(3, "Word3"), t(4, "word4")]
This is a list of tokens. For me a token is something like: t(1, "word1") so a token have a "functor" t and into it there are two arguments: a number (not necessarily progressive, in some cases I have that this value can be -1) and a string that reppresent a word.
As you can see some word can have the first letter that is uppercase (in this example the second and the third token)
I have to build a predicate that when find that two contiguous tokens have the strings values that begin with uppercases words have to collapse them into a unique token.
For example the preious tokens list have to be transformed into:
TokenList =[t(1, "word1"), t(2, "Word2 Word3"), t(4, "word4")]
As you can see the content of the second and of the third tokens (the words) aree put into a unique token having "Word2 Word3" as content field
I have some problem to implement this predicate. I was trying to do something like this:
collapseToken([],[]).
collapseToken([t(Number1, String1),t(Number2, String2)|TokenTail],[NewToken|TaggedTokenList]) :-
String1 = [Head1|Tail1],
String2 = [Head2|Tail2],
char_type(Head1, upper),
char_type(Head2, upper),
!,
append(String1, String2, NewString),
NewToken = t(Number1, NewString),
collapseToken(TokenTail, TaggedTokenList).
collapseToken([Head1|Tail1], [Head2|Tail2]) :- collapseToken(Tail1, Tail2).
My collapseToken/2 predicate have in the head two list: the first one is the original list and the second one is the result list
The original list is composed by two token that are: t(Number1, String1) and t(Number2, String2), so String1 is the word of the first token and String2 is the word of the second token.
So it is true that the result string [NewToken|TaggedTokenList] (where NewToken is something like **t(Number1, [String1|String2]): a token having the number of the first token and as content the word of the first token followed by the word of the second token) if it is TRUE that String1 and String2 booth begins with an uppercase character so create a **NewString that is the concatenation of String1 and String2, then create the NewToken to put in the head of my result list). Finnally recursivly call itself
Then I have created an other rule that is applied when this is not true that there are two contigous tag having both the content words that begin on an uppercase character
But there is something wrong and don't work. Are my general logic right?