Skip to content

Option to clamp to words, sentences or other tokens #86

@JorenSix

Description

@JorenSix

Hi Simon,

First of all thanks for developing this in the open! I am evaluating the text-annotator-js component to see if it fits for a project I am working on. It looks very promising.

One of the requirements for my project is that annotations should clamp to word boundaries. I have included the code below in the trimRange() method also mentioned in #66. Conceptually perhaps not the worst place to place such code. In relation to this I have a few questions:

  • Is there already a way to clamp to words/sentences I perhaps overlooked?
  • Is clamping functionality common enough to include in the annotator?
  • Is there a better place to modify a range based on application rules?
  • Would it be a good idea to add an optional callback to the annotator component which would then be used to clamp a range according to application rules?

Thanks in advance for considering these questions.

const word_boundaries = ['.',',',';',':','?','!',' ','\n','-'];
 
var charBeforeStart = startContainer.nodeValue.charAt(range.startOffset-1);
var newStartOffset = range.startOffset;
while(!word_boundaries.includes(charBeforeStart) && newStartOffset >= 0){
  newStartOffset = newStartOffset-1;
  charBeforeStart = startContainer.nodeValue.charAt(newStartOffset);
}
range.setStart(startContainer, newStartOffset);

var charAfterEnd = endContainer.nodeValue.charAt(range.endOffset);
var newEndOffset = range.endOffset;
while(!word_boundaries.includes(charAfterEnd) && newEndOffset < endContainer.nodeValue.length){
  newEndOffset = newEndOffset+1;
  charAfterEnd = endContainer.nodeValue.charAt(newEndOffset);
}
range.setEnd(endContainer, newEndOffset);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions