Adding sentence number for each word (update temp variable)

Hello all,

I'm trying to modify a code by Michael Hunger. What I want is to add a sentence number to each word.
{text} is a list of words.
S is initialized by 1 and it will be used to update the sentence number.
If the current WORDS[i] is a dot (full stop), then increment S.
My code as follows:

         WITH {text} AS WORDS, 1 AS S
         UNWIND range(0, size(WORDS)-2) AS i
         WITH WORDS, i, (CASE WHEN WORDS[i] CONTAINS "." THEN S+1 ELSE S END ) AS S
         
         MERGE (w1:Word {name: WORDS[i]})
            ON CREATE SET w1.count = 1, w1.position = [i+1], w1.sentence = [S] 
            ON MATCH SET w1.count = w1.count + 1, w1.position = w1.position + [i+1], w1.sentence = w1.sentence + [S]

         MERGE (w2:Word {name: WORDS[i+1]})
            ON CREATE SET w2.count = 1
            ON MATCH SET w2.count = w2.count + (case when i = size(WORDS)-2 then 1 else 0 end)

         MERGE (w1)-[r:NEXT]->(w2)
            ON CREATE SET r.count = 1
            ON MATCH SET r.count = r.count+1

The problem is:

  • The sentence number for each word still equals to 1. It is incremented only for the word after a full stop and then returns to 1.

Anyone can help?!
Thank you.