ReferenceTagger { public static String markRefs(final String rawText) { StringBuilder stb = new StringBuilder(rawText); Matcher clmMatcher = CLAIM_REF.matcher(rawText); int additionalChars = 0; while (clmMatcher.find()) { String fullMatch = clmMatcher.group(0); StringBuilder claimStb = new StringBuilder(); claimStb.append("<a class=\"claim\" idref=\"CLM-").append(Strings.padStart(clmMatcher.group(2), 4, '0')); if (" or ".equals(clmMatcher.group(3))) { claimStb.append("\">").append("claim ").append(clmMatcher.group(2)).append("</a>"); claimStb.append(clmMatcher.group(3)).append("<a class=\"claim\" idref=\"CLM-").append(Strings.padStart(clmMatcher.group(4), 4, '0')); claimStb.append("\">").append("claim ").append(clmMatcher.group(4)).append("</a>"); } else if (clmMatcher.group(3) != null) { claimStb.append(" - ").append("CLM-").append(Strings.padStart(clmMatcher.group(4), 4, '0')); claimStb.append("\">").append(fullMatch).append("</a>"); } else { claimStb.append("\">").append("claim ").append(clmMatcher.group(2)).append("</a>"); } String newStr = claimStb.toString(); stb.replace(clmMatcher.start() + additionalChars, clmMatcher.end() + additionalChars, newStr); additionalChars = additionalChars + (newStr.length() - fullMatch.length()); } String htmlText = stb.toString(); stb = new StringBuilder(htmlText); Matcher figMatcher = PATENT_FIG.matcher(htmlText); additionalChars = 0; while (figMatcher.find()) { String fullMatch = figMatcher.group(0); StringBuilder figStb = new StringBuilder(); figStb.append("<a class=\"figref\" idref=\"FIG-").append(figMatcher.group(2)); if (" and ".equals(figMatcher.group(3))) { figStb.append("\">").append("FIG-").append(figMatcher.group(2)).append("</a>"); figStb.append(figMatcher.group(3)).append("<a class=\"figref\" idref=\"FIG-").append(figMatcher.group(4)); figStb.append("\">").append("FIG-").append(figMatcher.group(4)).append("</a>"); } else if (figMatcher.group(3) != null) { figStb.append(" - ").append("FIG-").append(figMatcher.group(4)); figStb.append("\">").append(fullMatch).append("</a>"); } else { figStb.append("\">").append("FIG-").append(figMatcher.group(2)).append("</a>"); } String newStr = figStb.toString(); stb.replace(figMatcher.start() + additionalChars, figMatcher.end() + additionalChars, newStr); additionalChars = additionalChars + (newStr.length() - fullMatch.length()); } return stb.toString(); }  static String markRefs(final String rawText); static String createClaimId(final String text); static String createFigId(final String text); static String normRefs(final String text);  }
@Test public void markRefs() { for (Entry<String, String> entry : text2Anno.entrySet()) { String actual = ReferenceTagger.markRefs(entry.getKey()); assertEquals(entry.getValue(), actual); } }