AgdistisWrapper { public String createAgdistisString(final Document doc) { List<Span> spans = new ArrayList<>(); for (Span span : doc.getMarkings(Span.class)) { if (span.getIsWord()) { spans.add(span); } } Collections.sort(spans, new Comparator<Span>() { @Override public int compare(final Span o1, final Span o2) { return o1.getStartPosition() - o2.getStartPosition(); } }); List<Span> notProcessedSpans = new ArrayList<>(spans); StringBuilder docStr = new StringBuilder(doc.getText()); for (Span span : spans) { notProcessedSpans.remove(span); int spanStart = span.getStartPosition(); int spanLength = span.getLength(); String word = docStr.substring(spanStart, spanStart + spanLength); String insertionString = "<entity>" + word + "</entity>"; int offsetForFollowing = insertionString.length() - word.length(); for (Span it : notProcessedSpans) { it.setStartPosition(it.getStartPosition() + offsetForFollowing); } docStr.replace(spanStart, spanStart + spanLength, insertionString); } return docStr.toString(); }  @RequestMapping(method = RequestMethod.GET) String getAgdistis(); @RequestMapping(method = RequestMethod.POST) String postAgdistis(@RequestBody final String input); String process(final String input); List<Marking> requestForStringAgdistis(final String sentence); String createAgdistisString(final Document doc);  }
@Test public void createAgdistisString() { String qEntity = "Who are the successors of <entity>Barack Obama</entity> and <entity>Michelle Obama</entity>?"; String processed = agdistis.createAgdistisString(testDoc); Assert.assertTrue("Should be: \n" + qEntity + "\n but was\n" + processed, qEntity.equals(processed)); }