Segmentation: More fixes related to references at end of sentence

References need special handling because it can appear after a period
but still part of previous sentences.

It is also possible that they come repeated. example: "...end.[1][2]"

To handle this repeated reference, we are undoing the last segment close.

But there were some bugs in resetting the inReference flag causing the
"undo end sentence" deleting actual content

This patch fixes them and add more tests

Change-Id: Icc4d1b6ddad804d5365a0001c7b93f3ac78c9b23