| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap; |
| 2 | import java.util.*; |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 3 | import java.io.*; |
| 4 | |
| Nils Diewald | a115a33 | 2014-01-07 13:59:09 +0000 | [diff] [blame] | 5 | import java.lang.StringBuffer; |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 6 | import java.nio.ByteBuffer; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 7 | |
| 8 | import com.fasterxml.jackson.annotation.*; |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 9 | import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 10 | import com.fasterxml.jackson.databind.ObjectMapper; |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 11 | import com.fasterxml.jackson.databind.JsonNode; |
| 12 | import com.fasterxml.jackson.databind.node.*; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 13 | |
| 14 | import de.ids_mannheim.korap.index.PositionsToOffset; |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 15 | import de.ids_mannheim.korap.index.SearchContext; |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 16 | import de.ids_mannheim.korap.document.KorapPrimaryData; |
| 17 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 18 | import static de.ids_mannheim.korap.util.KorapHTML.*; |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 19 | import de.ids_mannheim.korap.index.MatchIdentifier; |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 20 | import de.ids_mannheim.korap.index.PosIdentifier; |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 21 | import de.ids_mannheim.korap.query.SpanElementQuery; |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 22 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 26 | import org.apache.lucene.index.AtomicReaderContext; |
| 27 | import org.apache.lucene.index.Term; |
| 28 | import org.apache.lucene.index.TermContext; |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 29 | import org.apache.lucene.util.FixedBitSet; |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 30 | import org.apache.lucene.util.Bits; |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 31 | import org.apache.lucene.document.Document; |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 32 | import org.apache.lucene.search.spans.Spans; |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 33 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 34 | /* |
| 35 | Todo: The implemented classes and private names are horrible! |
| 36 | Refactor, future-me! |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 37 | |
| 38 | The number based Highlighttype is ugly - UGLY! |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 39 | */ |
| 40 | |
| 41 | /** |
| 42 | * Representation of Matches in a KorapResult. |
| 43 | * |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 44 | * @author Nils Diewald |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 45 | * @see KorapResult |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 46 | */ |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 47 | @JsonInclude(Include.NON_NULL) |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 48 | public class KorapMatch extends KorapDocument { |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 49 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 50 | // Logger |
| 51 | private final static Logger log = LoggerFactory.getLogger(KorapMatch.class); |
| 52 | |
| 53 | // This advices the java compiler to ignore all loggings |
| Nils Diewald | 8493437 | 2014-05-20 13:48:18 +0000 | [diff] [blame] | 54 | public static final boolean DEBUG = false; |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 55 | |
| 56 | // Mapper for JSON serialization |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 57 | ObjectMapper mapper = new ObjectMapper(); |
| 58 | |
| 59 | // Snippet information |
| 60 | @JsonIgnore |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 61 | public SearchContext context; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 62 | |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 63 | // Should be deprecated, but used wildly in tests! |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 64 | @JsonIgnore |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 65 | public int startPos, endPos; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 66 | |
| 67 | @JsonIgnore |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 68 | public int potentialStartPosChar = -1, |
| 69 | potentialEndPosChar = -1; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 70 | |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 71 | private String error = null; |
| Nils Diewald | cdd465b | 2014-02-24 18:47:38 +0000 | [diff] [blame] | 72 | private String version; |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 73 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 74 | // TEMPORARILY |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 75 | @JsonIgnore |
| 76 | public int localDocID = -1; |
| 77 | |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 78 | HashMap<Integer, String> annotationNumber = new HashMap<>(16); |
| 79 | HashMap<Integer, Relation> relationNumber = new HashMap<>(16); |
| 80 | HashMap<Integer, Integer> identifierNumber = new HashMap<>(16); |
| 81 | |
| 82 | // -1 is match highlight |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 83 | int annotationNumberCounter = 256; |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 84 | int relationNumberCounter = 2048; |
| 85 | int identifierNumberCounter = -2; |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 86 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 87 | private String tempSnippet, |
| 88 | snippetHTML, |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 89 | snippetBrackets, |
| 90 | identifier; |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 91 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 92 | private HighlightCombinator snippetStack; |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 93 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 94 | public boolean startMore = true, |
| 95 | endMore = true; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 96 | |
| 97 | private Collection<byte[]> payload; |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 98 | private ArrayList<Highlight> highlight; |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 99 | private LinkedList<int[]> span; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 100 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 101 | private PositionsToOffset positionsToOffset; |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 102 | private boolean processed = false; |
| 103 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 104 | /** |
| 105 | * Constructs a new KorapMatch object. |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 106 | * Todo: Maybe that's not necessary! |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 107 | * |
| 108 | * @param pto The PositionsToOffset object, containing relevant |
| 109 | * positional information for highlighting |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 110 | * @param localDocID Document ID based on the atomic reader. |
| 111 | * @param startPos Start position of the match in the document. |
| 112 | * @param endPos End position of the match in the document. |
| 113 | * |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 114 | * @see #snippetHTML() |
| 115 | * @see #snippetBrackets() |
| 116 | * @see PositionsToOffset |
| 117 | */ |
| 118 | public KorapMatch (PositionsToOffset pto, int localDocID, int startPos, int endPos) { |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 119 | this.positionsToOffset = pto; |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 120 | this.localDocID = localDocID; |
| 121 | this.startPos = startPos; |
| 122 | this.endPos = endPos; |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 125 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 126 | /** |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 127 | * Constructs a new KorapMatch object. |
| 128 | */ |
| 129 | public KorapMatch () {}; |
| 130 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 131 | |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 132 | /** |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 133 | * Constructs a new KorapMatch object. |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 134 | * |
| 135 | * @param idString Match identifier string as provided by KorapResult. |
| 136 | * @param includeHighlights Boolean value indicating if possible provided |
| 137 | * highlight information should be ignored or not. |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 138 | */ |
| 139 | public KorapMatch (String idString, boolean includeHighlights) { |
| 140 | MatchIdentifier id = new MatchIdentifier(idString); |
| 141 | this.setCorpusID(id.getCorpusID()); |
| 142 | this.setDocID(id.getDocID()); |
| 143 | this.setStartPos(id.getStartPos()); |
| 144 | this.setEndPos(id.getEndPos()); |
| 145 | |
| 146 | if (includeHighlights) |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 147 | for (int[] pos : id.getPos()) { |
| 148 | if (pos[0] < id.getStartPos() || pos[1] > id.getEndPos()) |
| 149 | continue; |
| 150 | |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 151 | this.addHighlight(pos[0], pos[1], pos[2]); |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 152 | }; |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * Private class of highlights. |
| 158 | */ |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 159 | private class Highlight { |
| 160 | public int start, end; |
| 161 | public int number = -1; |
| 162 | |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 163 | // Relational highlight |
| 164 | public Highlight (int start, int end, String annotation, int ref) { |
| 165 | this.start = start; |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 166 | this.end = end; |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 167 | // TODO: This can overflow! |
| 168 | this.number = relationNumberCounter++; |
| 169 | relationNumber.put(this.number, new Relation(annotation, ref)); |
| 170 | }; |
| 171 | |
| 172 | // Span highlight |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 173 | public Highlight (int start, int end, String annotation) { |
| 174 | this.start = start; |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 175 | this.end = end; |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 176 | // TODO: This can overflow! |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 177 | if (annotationNumberCounter < 2048) { |
| 178 | this.number = annotationNumberCounter++; |
| 179 | annotationNumber.put(this.number, annotation); |
| 180 | }; |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 183 | // Simple highlight |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 184 | public Highlight (int start, int end, int number) { |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 185 | this.start = start; |
| 186 | this.end = end; |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 187 | this.number = number; |
| 188 | }; |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * Private class of relations. |
| 194 | */ |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 195 | private class Relation { |
| 196 | public int ref; |
| 197 | public String annotation; |
| 198 | public Relation (String annotation, int ref) { |
| 199 | this.annotation = annotation; |
| 200 | this.ref = ref; |
| 201 | }; |
| 202 | }; |
| 203 | |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 204 | |
| 205 | /** |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 206 | * Insert a highlight for the snippet view by means of positional |
| 207 | * offsets and an optional class number. |
| 208 | * |
| 209 | * @param start Integer value of a span's positional start offset. |
| 210 | * @param end Integer value of a span's positional end offset. |
| 211 | * @param number Optional class number of the highlight. |
| 212 | */ |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 213 | public void addHighlight (int start, int end) { |
| 214 | this.addHighlight(new Highlight(start, end, (int) 0)); |
| 215 | }; |
| 216 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 217 | public void addHighlight (int start, int end, byte number) { |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 218 | this.addHighlight(new Highlight(start, end, (int) number)); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | public void addHighlight (int start, int end, short number) { |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 222 | this.addHighlight(new Highlight(start, end, (int) number)); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | public void addHighlight (int start, int end, int number) { |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 226 | this.addHighlight(new Highlight(start, end, number)); |
| 227 | }; |
| 228 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * Insert a highlight for the snippet view. |
| 232 | * |
| 233 | * @param hl A highlight object to add to the match. |
| 234 | */ |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 235 | public void addHighlight (Highlight hl) { |
| 236 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 237 | if (this.highlight == null) |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 238 | this.highlight = new ArrayList<Highlight>(16); |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 239 | |
| 240 | if (DEBUG) |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 241 | log.trace("Add highlight from pos {}-{} of class {}", |
| 242 | hl.start, hl.end, hl.number); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 243 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 244 | // Reset the fetched match data |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 245 | this._reset(); |
| 246 | |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 247 | this.highlight.add(hl); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 248 | }; |
| 249 | |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 250 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 251 | /** |
| 252 | * Insert a textual annotation for the snippet view by |
| 253 | * means of positional offsets and an annotation string. |
| 254 | * |
| 255 | * @param start Integer value of a span's positional start offset. |
| 256 | * @param end Integer value of a span's positional end offset. |
| 257 | * @param annotation Annotation string. |
| 258 | */ |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 259 | public void addAnnotation (int start, int end, String annotation) { |
| 260 | this.addHighlight(new Highlight(start, end, annotation)); |
| 261 | }; |
| 262 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 263 | |
| 264 | /** |
| 265 | * Insert an annotated relation for the snippet view by |
| 266 | * means of relational participant positions and an annotation string. |
| 267 | * |
| 268 | * @param src Integer value of a span's positional source object. |
| 269 | * @param target Integer value of a span's positional target object. |
| 270 | * @param annotation Annotation string. |
| 271 | */ |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 272 | public void addRelation (int src, int target, String annotation) { |
| 273 | this.addHighlight(new Highlight(src, src, annotation, target)); |
| 274 | int id = identifierNumberCounter--; |
| 275 | identifierNumber.put(id, target); |
| 276 | this.addHighlight(new Highlight(target, target, id)); |
| 277 | }; |
| 278 | |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 279 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 280 | /** |
| 281 | * Populate document meta information with information coming from the index. |
| 282 | * |
| 283 | * @param doc Document object. |
| 284 | * @param field Primary data field. |
| 285 | * @param fields Hash object with all supported fields. |
| 286 | */ |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 287 | public void populateDocument (Document doc, String field, HashSet<String> fields) { |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 288 | this.setField(field); |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 289 | this.setPrimaryData( new KorapPrimaryData(doc.get(field)) ); |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 290 | if (fields.contains("corpusID")) |
| 291 | this.setCorpusID(doc.get("corpusID")); |
| 292 | if (fields.contains("ID")) |
| 293 | this.setDocID(doc.get("ID")); |
| 294 | if (fields.contains("author")) |
| 295 | this.setAuthor(doc.get("author")); |
| 296 | if (fields.contains("textClass")) |
| 297 | this.setTextClass(doc.get("textClass")); |
| 298 | if (fields.contains("title")) |
| 299 | this.setTitle(doc.get("title")); |
| 300 | if (fields.contains("subTitle")) |
| 301 | this.setSubTitle(doc.get("subTitle")); |
| 302 | if (fields.contains("pubDate")) |
| 303 | this.setPubDate(doc.get("pubDate")); |
| 304 | if (fields.contains("pubPlace")) |
| 305 | this.setPubPlace(doc.get("pubPlace")); |
| 306 | |
| 307 | // Temporary (later meta fields in term vector) |
| 308 | if (fields.contains("foundries")) |
| 309 | this.setFoundries(doc.get("foundries")); |
| 310 | if (fields.contains("tokenization")) |
| 311 | this.setTokenization(doc.get("tokenization")); |
| 312 | if (fields.contains("layerInfo")) |
| 313 | this.setLayerInfo(doc.get("layerInfo")); |
| 314 | }; |
| 315 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 316 | |
| 317 | /** |
| 318 | * Get document id. |
| 319 | */ |
| Nils Diewald | 010c10f | 2013-12-17 01:58:31 +0000 | [diff] [blame] | 320 | @JsonProperty("docID") |
| 321 | public String getDocID () { |
| 322 | return super.getID(); |
| 323 | }; |
| 324 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 325 | |
| 326 | /** |
| 327 | * Set document id. |
| 328 | * |
| 329 | * @param id String representation of document ID. |
| 330 | */ |
| Nils Diewald | 364eb64 | 2013-12-22 15:03:01 +0000 | [diff] [blame] | 331 | public void setDocID (String id) { |
| 332 | super.setID(id); |
| 333 | }; |
| 334 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * Set version of the index |
| 338 | */ |
| 339 | @JsonIgnore |
| 340 | public String getVersion () { |
| 341 | if (this.version == null) |
| 342 | return null; |
| 343 | StringBuilder sb = new StringBuilder("lucene-backend-"); |
| 344 | return sb.append(this.version).toString(); |
| 345 | }; |
| 346 | |
| 347 | |
| 348 | /** |
| 349 | * Set version number. |
| 350 | * |
| 351 | * @param version The version number of the index as |
| 352 | * a string representation. |
| 353 | */ |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 354 | @JsonIgnore |
| Nils Diewald | cdd465b | 2014-02-24 18:47:38 +0000 | [diff] [blame] | 355 | public void setVersion (String version) { |
| 356 | this.version = version; |
| 357 | }; |
| 358 | |
| Nils Diewald | cdd465b | 2014-02-24 18:47:38 +0000 | [diff] [blame] | 359 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 360 | /** |
| 361 | * Get the positional start offset of the match. |
| 362 | */ |
| Nils Diewald | cdd465b | 2014-02-24 18:47:38 +0000 | [diff] [blame] | 363 | @JsonIgnore |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 364 | public int getStartPos() { |
| 365 | return this.startPos; |
| 366 | }; |
| 367 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 368 | |
| 369 | /** |
| 370 | * Set the positional start offset of the match. |
| 371 | * |
| 372 | * @param pos The positional offset. |
| 373 | */ |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 374 | @JsonIgnore |
| 375 | public void setStartPos(int pos) { |
| 376 | this.startPos = pos; |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 377 | }; |
| 378 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 379 | |
| 380 | /** |
| 381 | * Get the positional end offset of the match. |
| 382 | */ |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 383 | @JsonIgnore |
| 384 | public int getEndPos() { |
| 385 | return this.endPos; |
| 386 | }; |
| 387 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 388 | |
| 389 | /** |
| 390 | * Set the positional end offset of the match. |
| 391 | * |
| 392 | * @param pos The positional offset. |
| 393 | */ |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 394 | @JsonIgnore |
| 395 | public void setEndPos(int pos) { |
| 396 | this.endPos = pos; |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 397 | }; |
| 398 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 399 | |
| 400 | /** |
| 401 | * Get the local (i.e. Lucene given) ID of the document. |
| 402 | */ |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 403 | @JsonIgnore |
| 404 | public int getLocalDocID () { |
| 405 | return this.localDocID; |
| 406 | }; |
| 407 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 408 | |
| 409 | /** |
| 410 | * Set the local (i.e. Lucene given) ID of the document. |
| 411 | * |
| 412 | * @param id The id of the document. |
| 413 | */ |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 414 | @JsonIgnore |
| 415 | public void setLocalDocID (int id) { |
| 416 | this.localDocID = id; |
| 417 | }; |
| 418 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 419 | |
| 420 | /** |
| 421 | * Get the PositionsToOffset object. |
| 422 | * |
| 423 | * @see PositionsToOffset |
| 424 | */ |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 425 | @JsonIgnore |
| 426 | public PositionsToOffset getPositionsToOffset () { |
| 427 | return this.positionsToOffset; |
| 428 | }; |
| 429 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 430 | |
| 431 | /** |
| 432 | * Set the PositionsToOffset object. |
| 433 | * |
| 434 | * @param pto The PositionsToOffset object |
| 435 | * @see PositionsToOffset |
| 436 | */ |
| 437 | @JsonIgnore |
| 438 | public void setPositionsToOffset (PositionsToOffset pto) { |
| 439 | this.positionsToOffset = pto; |
| 440 | }; |
| 441 | |
| 442 | |
| 443 | /** |
| 444 | * Get match ID (for later retrieval). |
| 445 | * |
| 446 | * @see MatchIdentifier |
| 447 | */ |
| Nils Diewald | 010c10f | 2013-12-17 01:58:31 +0000 | [diff] [blame] | 448 | @Override |
| 449 | @JsonProperty("ID") |
| 450 | public String getID () { |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 451 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 452 | // Identifier already given |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 453 | if (this.identifier != null) |
| 454 | return this.identifier; |
| 455 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 456 | // No, nada, nix |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 457 | if (this.localDocID == -1) |
| 458 | return null; |
| 459 | |
| 460 | MatchIdentifier id = new MatchIdentifier(); |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 461 | |
| 462 | // Get prefix string corpus/doc |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 463 | id.setCorpusID(this.getCorpusID()); |
| 464 | id.setDocID(this.getDocID()); |
| 465 | id.setStartPos(startPos); |
| 466 | id.setEndPos(endPos); |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 467 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 468 | // There are highlights to integrate |
| Nils Diewald | a115a33 | 2014-01-07 13:59:09 +0000 | [diff] [blame] | 469 | if (this.highlight != null) { |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 470 | for (Highlight h : this.highlight) { |
| 471 | if (h.number >= 256) |
| 472 | continue; |
| 473 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 474 | // Add highlight to the snippet |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 475 | id.addPos(h.start, h.end, h.number); |
| Nils Diewald | a115a33 | 2014-01-07 13:59:09 +0000 | [diff] [blame] | 476 | }; |
| 477 | }; |
| 478 | |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 479 | return (this.identifier = id.toString()); |
| Nils Diewald | 010c10f | 2013-12-17 01:58:31 +0000 | [diff] [blame] | 480 | }; |
| 481 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 482 | |
| 483 | /** |
| 484 | * Get identifier for a specific position. |
| 485 | * |
| 486 | * @param int Position to get identifier on. |
| 487 | */ |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 488 | @JsonIgnore |
| 489 | public String getPosID (int pos) { |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 490 | |
| 491 | // Identifier already given |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 492 | if (this.identifier != null) |
| 493 | return this.identifier; |
| 494 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 495 | // Nothing here |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 496 | if (this.localDocID == -1) |
| 497 | return null; |
| 498 | |
| 499 | PosIdentifier id = new PosIdentifier(); |
| 500 | |
| 501 | // Get prefix string corpus/doc |
| 502 | id.setCorpusID(this.getCorpusID()); |
| 503 | id.setDocID(this.getDocID()); |
| 504 | id.setPos(pos); |
| 505 | |
| 506 | return id.toString(); |
| 507 | }; |
| 508 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 509 | /** |
| 510 | * Get possible error message. |
| 511 | */ |
| 512 | // Identical to KorapResult |
| 513 | public String getError () { |
| 514 | return this.error; |
| 515 | }; |
| 516 | |
| 517 | /** |
| 518 | * Set error message. |
| 519 | * |
| 520 | * @param msg The error message. |
| 521 | */ |
| 522 | public void setError (String msg) { |
| 523 | this.error = msg; |
| 524 | }; |
| 525 | |
| 526 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 527 | public KorapMatch setContext (SearchContext context) { |
| 528 | this.context = context; |
| 529 | return this; |
| 530 | }; |
| 531 | |
| 532 | @JsonIgnore |
| 533 | public SearchContext getContext () { |
| 534 | if (this.context == null) |
| 535 | this.context = new SearchContext(); |
| 536 | return this.context; |
| 537 | }; |
| 538 | |
| 539 | |
| 540 | // Expand the context to a span |
| 541 | public int[] expandContextToSpan (String element) { |
| 542 | |
| 543 | // TODO: THE BITS HAVE TO BE SET! |
| 544 | |
| 545 | if (this.positionsToOffset != null) |
| 546 | return this.expandContextToSpan( |
| 547 | this.positionsToOffset.getAtomicReader(), |
| 548 | (Bits) null, |
| 549 | "tokens", |
| 550 | element |
| 551 | ); |
| 552 | return new int[]{0,0,0,0}; |
| 553 | }; |
| 554 | |
| 555 | // Expand the context to a span |
| Nils Diewald | 8493437 | 2014-05-20 13:48:18 +0000 | [diff] [blame] | 556 | // THIS IS NOT VERY CLEVER - MAKE IT MORE CLEVER! |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 557 | public int[] expandContextToSpan (AtomicReaderContext atomic, |
| 558 | Bits bitset, |
| 559 | String field, |
| 560 | String element) { |
| 561 | |
| 562 | try { |
| 563 | // Store character offsets in ByteBuffer |
| 564 | ByteBuffer bb = ByteBuffer.allocate(8); |
| 565 | |
| 566 | SpanElementQuery cquery = |
| 567 | new SpanElementQuery(field, element); |
| 568 | |
| 569 | Spans contextSpans = cquery.getSpans( |
| 570 | atomic, |
| 571 | bitset, |
| 572 | new HashMap<Term, TermContext>() |
| 573 | ); |
| 574 | |
| 575 | int newStart = -1, |
| 576 | newEnd = -1; |
| 577 | int newStartChar = -1, |
| 578 | newEndChar = -1; |
| 579 | |
| 580 | if (DEBUG) |
| 581 | log.trace("Extend match to context boundary with {} in {}", |
| 582 | cquery.toString(), |
| 583 | this.localDocID); |
| 584 | |
| 585 | while (true) { |
| 586 | |
| 587 | // Game over |
| 588 | if (contextSpans.next() != true) |
| 589 | break; |
| 590 | |
| 591 | if (contextSpans.doc() != this.localDocID) { |
| 592 | contextSpans.skipTo(this.localDocID); |
| 593 | if (contextSpans.doc() != this.localDocID) |
| 594 | break; |
| 595 | }; |
| 596 | |
| 597 | // There's a <context> found -- I'm curious, |
| 598 | // if it's closer to the match than everything before |
| 599 | if (contextSpans.start() <= this.getStartPos() && |
| 600 | contextSpans.end() >= this.getStartPos()) { |
| 601 | |
| 602 | // Set as newStart |
| 603 | newStart = contextSpans.start() > newStart ? |
| 604 | contextSpans.start() : newStart; |
| 605 | |
| Nils Diewald | 8493437 | 2014-05-20 13:48:18 +0000 | [diff] [blame] | 606 | if (DEBUG) |
| 607 | log.trace("NewStart is at {}", newStart); |
| 608 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 609 | // Get character offset (start) |
| 610 | if (contextSpans.isPayloadAvailable()) { |
| 611 | try { |
| 612 | bb.rewind(); |
| 613 | for (byte[] b : contextSpans.getPayload()) { |
| 614 | |
| 615 | // Not an element span |
| 616 | if (b.length != 8) |
| 617 | continue; |
| 618 | |
| 619 | bb.put(b); |
| 620 | bb.rewind(); |
| 621 | newStartChar = bb.getInt(); |
| 622 | newEndChar = bb.getInt(); |
| 623 | break; |
| 624 | }; |
| 625 | } |
| 626 | catch (Exception e) { |
| 627 | log.warn(e.getMessage()); |
| 628 | }; |
| 629 | }; |
| 630 | } |
| 631 | else { |
| 632 | // Has to be resettet to avoid multiple readings of the payload |
| 633 | newEndChar = 0; |
| 634 | }; |
| 635 | |
| 636 | // There's an s found, that ends after the match |
| 637 | if (contextSpans.end() >= this.getEndPos()) { |
| 638 | newEnd = contextSpans.end(); |
| 639 | |
| 640 | // Get character offset (end) |
| 641 | if (newEndChar == 0 && contextSpans.isPayloadAvailable()) { |
| 642 | try { |
| 643 | bb.rewind(); |
| 644 | for (byte[] b : contextSpans.getPayload()) { |
| 645 | |
| 646 | // Not an element span |
| 647 | if (b.length != 8) |
| 648 | continue; |
| 649 | |
| 650 | bb.put(b); |
| 651 | bb.rewind(); |
| 652 | newEndChar = bb.getInt(1); |
| 653 | break; |
| 654 | }; |
| 655 | } |
| 656 | catch (Exception e) { |
| 657 | log.warn(e.getMessage()); |
| 658 | }; |
| 659 | }; |
| 660 | break; |
| 661 | }; |
| 662 | }; |
| 663 | |
| 664 | // We have a new match surrounding |
| 665 | if (DEBUG) |
| 666 | log.trace("New match spans from {}-{}/{}-{}", newStart, newEnd, newStartChar, newEndChar); |
| 667 | |
| 668 | return new int[]{newStart, newEnd, newStartChar, newEndChar}; |
| 669 | } |
| 670 | catch (IOException e) { |
| 671 | log.error(e.getMessage()); |
| 672 | }; |
| 673 | |
| 674 | return new int[]{-1,-1,-1,-1}; |
| 675 | }; |
| 676 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 677 | |
| 678 | // Reset all internal data |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 679 | private void _reset () { |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 680 | this.processed = false; |
| 681 | this.snippetHTML = null; |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 682 | this.snippetBrackets = null; |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 683 | this.identifier = null; |
| 684 | |
| 685 | // Delete all spans |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 686 | if (this.span != null) |
| 687 | this.span.clear(); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 688 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 689 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 690 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 691 | // Start building highlighted snippets |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 692 | private boolean _processHighlight () { |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 693 | if (processed) |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 694 | return true; |
| 695 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 696 | // Relevant details are missing |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 697 | if (this.positionsToOffset == null || this.localDocID == -1) { |
| 698 | log.warn("You have to define " + |
| 699 | "positionsToOffset and localDocID first " + |
| 700 | "before"); |
| 701 | return false; |
| 702 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 703 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 704 | if (DEBUG) |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 705 | log.trace("--- Start highlight processing ..."); |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 706 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 707 | // Get pto object |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 708 | PositionsToOffset pto = this.positionsToOffset; |
| 709 | pto.add(this.localDocID, this.getStartPos()); |
| 710 | pto.add(this.localDocID, this.getEndPos() - 1); |
| 711 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 712 | if (DEBUG) |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 713 | log.trace("PTO will retrieve {} & {} (Match boundary)", |
| 714 | this.getStartPos(), |
| 715 | this.getEndPos()); |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 716 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 717 | // Add all highlights for character retrieval |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 718 | if (this.highlight != null) { |
| 719 | for (Highlight hl : this.highlight) { |
| 720 | pto.add(this.localDocID, hl.start); |
| 721 | pto.add(this.localDocID, hl.end); |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 722 | |
| 723 | if (DEBUG) |
| 724 | log.trace("PTO will retrieve {} & {} (Highlight boundary)", |
| 725 | hl.start, hl.end); |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 726 | }; |
| 727 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 728 | |
| 729 | // Get the list of spans for matches and highlighting |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 730 | if (this.span == null || this.span.size() == 0) { |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 731 | if (!this._processHighlightSpans()) |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 732 | return false; |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 733 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 734 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 735 | // Create a stack for highlighted elements |
| 736 | // (opening and closing elements) |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 737 | ArrayList<int[]> stack = this._processHighlightStack(); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 738 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 739 | if (DEBUG) |
| 740 | log.trace("The snippet is {}", this.tempSnippet); |
| 741 | |
| 742 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 743 | // The temporary snippet is empty, nothing to do |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 744 | if (this.tempSnippet == null) { |
| 745 | processed = true; |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 746 | return false; |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 747 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 748 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 749 | // Merge the element stack with the primary textual data |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 750 | this._processHighlightSnippet(this.tempSnippet, stack); |
| 751 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 752 | // Match is processed - done |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 753 | return (processed = true); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 754 | }; |
| 755 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 756 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 757 | /* |
| 758 | Comparator class for opening tags |
| 759 | */ |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 760 | private class OpeningTagComparator implements Comparator<int[]> { |
| 761 | @Override |
| 762 | public int compare (int[] arg0, int[] arg1) { |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 763 | // Check start positions |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 764 | if (arg0[0] > arg1[0]) { |
| 765 | return 1; |
| 766 | } |
| 767 | else if (arg0[0] == arg1[0]) { |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 768 | // Check endpositions |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 769 | if (arg0[1] > arg1[1]) |
| 770 | return -1; |
| 771 | return 1; |
| 772 | }; |
| 773 | return -1; |
| 774 | }; |
| 775 | }; |
| 776 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 777 | /* |
| 778 | Comparator class for closing tags |
| 779 | */ |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 780 | private class ClosingTagComparator implements Comparator<int[]> { |
| 781 | @Override |
| 782 | public int compare (int[] arg0, int[] arg1) { |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 783 | // Check end positions |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 784 | if (arg0[1] > arg1[1]) { |
| 785 | return 1; |
| 786 | } |
| 787 | else if (arg0[1] == arg1[1]) { |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 788 | // Check start positions |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 789 | if (arg0[0] < arg1[0]) |
| 790 | return 1; |
| 791 | return -1; |
| 792 | }; |
| 793 | return -1; |
| 794 | }; |
| 795 | }; |
| 796 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 797 | /* |
| 798 | Private class for elements with highlighting information |
| 799 | */ |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 800 | private class HighlightCombinatorElement { |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 801 | |
| 802 | // Type 0: Textual data |
| 803 | // Type 1: Opening |
| 804 | // Type 2: Closing |
| 805 | private byte type; |
| 806 | |
| 807 | private int number = 0; |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 808 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 809 | private String characters; |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 810 | private boolean terminal = true; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 811 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 812 | // Constructor for highlighting elements |
| 813 | public HighlightCombinatorElement (byte type, int number) { |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 814 | this.type = type; |
| 815 | this.number = number; |
| 816 | }; |
| 817 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 818 | // Constructor for highlighting elements, |
| 819 | // that may not be terminal, i.e. they were closed and will |
| 820 | // be reopened for overlapping issues. |
| 821 | public HighlightCombinatorElement (byte type, int number, boolean terminal) { |
| 822 | this.type = type; |
| 823 | this.number = number; |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 824 | this.terminal = terminal; |
| 825 | }; |
| 826 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 827 | // Constructor for textual data |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 828 | public HighlightCombinatorElement (String characters) { |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 829 | this.type = (byte) 0; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 830 | this.characters = characters; |
| 831 | }; |
| 832 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 833 | // Return html fragment for this combinator element |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 834 | public String toHTML (KorapMatch match, FixedBitSet level, byte[] levelCache) { |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 835 | // Opening |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 836 | if (this.type == 1) { |
| 837 | StringBuilder sb = new StringBuilder(); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 838 | if (this.number == -1) { |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 839 | sb.append("<span class=\"match\">"); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 840 | } |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 841 | |
| 842 | else if (this.number < -1) { |
| 843 | sb.append("<span xml:id=\"") |
| 844 | .append(match.getPosID( |
| 845 | identifierNumber.get(this.number))) |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 846 | .append("\">"); |
| 847 | } |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 848 | |
| 849 | else if (this.number >= 256) { |
| 850 | sb.append("<span "); |
| 851 | if (this.number < 2048) { |
| 852 | sb.append("title=\"") |
| 853 | .append(annotationNumber.get(this.number)) |
| 854 | .append('"'); |
| 855 | } |
| 856 | else { |
| 857 | Relation rel = relationNumber.get(this.number); |
| 858 | sb.append("xlink:title=\"") |
| 859 | .append(rel.annotation) |
| 860 | .append('"'); |
| 861 | sb.append(" xlink:type=\"simple\""); |
| 862 | sb.append(" xlink:href=\"#"); |
| 863 | sb.append(match.getPosID(rel.ref)); |
| 864 | sb.append('"'); |
| 865 | }; |
| 866 | sb.append('>'); |
| 867 | } |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 868 | else { |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 869 | // Get the first free level slot |
| 870 | byte pos; |
| 871 | if (levelCache[this.number] != '\0') { |
| 872 | pos = levelCache[this.number]; |
| 873 | } |
| 874 | else { |
| 875 | pos = (byte) level.nextSetBit(0); |
| 876 | level.clear(pos); |
| 877 | levelCache[this.number] = pos; |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 878 | }; |
| 879 | sb.append("<em class=\"class-") |
| 880 | .append(this.number) |
| 881 | .append(" level-") |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 882 | .append(pos) |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 883 | .append("\">"); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 884 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 885 | return sb.toString(); |
| 886 | } |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 887 | // Closing |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 888 | else if (this.type == 2) { |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 889 | if (this.number <= -1 || this.number >= 256) |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 890 | return "</span>"; |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 891 | |
| 892 | if (this.terminal) |
| 893 | level.set((int) levelCache[this.number]); |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 894 | return "</em>"; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 895 | }; |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 896 | |
| 897 | // HTML encode primary data |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 898 | return encodeHTML(this.characters); |
| 899 | }; |
| 900 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 901 | // Return bracket fragment for this combinator element |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 902 | public String toBrackets () { |
| 903 | if (this.type == 1) { |
| 904 | StringBuilder sb = new StringBuilder(); |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 905 | |
| 906 | // Match |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 907 | if (this.number == -1) { |
| 908 | sb.append("["); |
| 909 | } |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 910 | |
| 911 | // Identifier |
| 912 | else if (this.number < -1) { |
| 913 | sb.append("{#"); |
| 914 | sb.append(identifierNumber.get(this.number)); |
| 915 | sb.append(':'); |
| 916 | } |
| 917 | |
| 918 | // Highlight, Relation, Span |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 919 | else { |
| 920 | sb.append("{"); |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 921 | if (this.number >= 256) { |
| 922 | if (this.number < 2048) |
| 923 | sb.append(annotationNumber.get(this.number)); |
| 924 | else { |
| 925 | Relation rel = relationNumber.get(this.number); |
| 926 | sb.append(rel.annotation); |
| 927 | sb.append('>').append(rel.ref); |
| 928 | }; |
| 929 | sb.append(':'); |
| 930 | } |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 931 | else if (this.number != 0) |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 932 | sb.append(this.number).append(':'); |
| 933 | }; |
| 934 | return sb.toString(); |
| 935 | } |
| 936 | else if (this.type == 2) { |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 937 | if (this.number == -1) |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 938 | return "]"; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 939 | return "}"; |
| 940 | }; |
| 941 | return this.characters; |
| 942 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 943 | }; |
| 944 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 945 | /* |
| 946 | Private class for combining highlighting elements |
| 947 | */ |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 948 | private class HighlightCombinator { |
| 949 | private LinkedList<HighlightCombinatorElement> combine; |
| 950 | private LinkedList<Integer> balanceStack = new LinkedList<>(); |
| 951 | private ArrayList<Integer> tempStack = new ArrayList<>(32); |
| 952 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 953 | // Empty constructor |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 954 | public HighlightCombinator () { |
| 955 | this.combine = new LinkedList<>(); |
| 956 | }; |
| 957 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 958 | // Return the combination stack |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 959 | public LinkedList<HighlightCombinatorElement> stack () { |
| 960 | return this.combine; |
| 961 | }; |
| 962 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 963 | // get the first element (without removing) |
| Nils Diewald | f3b30ae | 2013-11-27 17:42:37 +0000 | [diff] [blame] | 964 | public HighlightCombinatorElement getFirst () { |
| 965 | return this.combine.getFirst(); |
| 966 | }; |
| 967 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 968 | // get the last element (without removing) |
| Nils Diewald | f3b30ae | 2013-11-27 17:42:37 +0000 | [diff] [blame] | 969 | public HighlightCombinatorElement getLast () { |
| 970 | return this.combine.getLast(); |
| 971 | }; |
| 972 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 973 | // get an element by index (without removing) |
| Nils Diewald | f3b30ae | 2013-11-27 17:42:37 +0000 | [diff] [blame] | 974 | public HighlightCombinatorElement get (int index) { |
| 975 | return this.combine.get(index); |
| 976 | }; |
| 977 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 978 | // Get the size of te combinator stack |
| Nils Diewald | f3b30ae | 2013-11-27 17:42:37 +0000 | [diff] [blame] | 979 | public short size () { |
| 980 | return (short) this.combine.size(); |
| 981 | }; |
| 982 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 983 | // Add primary data to the stack |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 984 | public void addString (String characters) { |
| 985 | this.combine.add(new HighlightCombinatorElement(characters)); |
| 986 | }; |
| 987 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 988 | // Add opening highlight combinator to the stack |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 989 | public void addOpen (int number) { |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 990 | this.combine.add(new HighlightCombinatorElement((byte) 1, number)); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 991 | this.balanceStack.add(number); |
| 992 | }; |
| 993 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 994 | // Add closing highlight combinator to the stack |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 995 | public void addClose (int number) { |
| 996 | HighlightCombinatorElement lastComb; |
| 997 | this.tempStack.clear(); |
| Nils Diewald | 4fca3ff | 2013-12-29 22:59:13 +0000 | [diff] [blame] | 998 | |
| Nils Diewald | 20607ab | 2014-03-20 23:28:36 +0000 | [diff] [blame] | 999 | // Shouldn't happen |
| 1000 | if (this.balanceStack.size() == 0) { |
| 1001 | if (DEBUG) |
| 1002 | log.trace("The balance stack is empty"); |
| 1003 | return; |
| Nils Diewald | 4fca3ff | 2013-12-29 22:59:13 +0000 | [diff] [blame] | 1004 | }; |
| Nils Diewald | 20607ab | 2014-03-20 23:28:36 +0000 | [diff] [blame] | 1005 | |
| 1006 | if (DEBUG) { |
| 1007 | StringBuilder sb = new StringBuilder( |
| 1008 | "Stack for checking with class " |
| 1009 | ); |
| 1010 | sb.append(number).append(" is "); |
| 1011 | for (int s : this.balanceStack) { |
| 1012 | sb.append('[').append(s).append(']'); |
| 1013 | }; |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 1014 | log.trace(sb.toString()); |
| Nils Diewald | 20607ab | 2014-03-20 23:28:36 +0000 | [diff] [blame] | 1015 | }; |
| Nils Diewald | 4fca3ff | 2013-12-29 22:59:13 +0000 | [diff] [blame] | 1016 | |
| 1017 | // class number of the last element |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1018 | int eold = this.balanceStack.removeLast(); |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1019 | |
| 1020 | // the closing element is not balanced |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1021 | while (eold != number) { |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Retrieve last combinator on stack |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1024 | lastComb = this.combine.peekLast(); |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1025 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 1026 | if (DEBUG) |
| 1027 | log.trace("Closing element is unbalanced - {} " + |
| 1028 | "!= {} with lastComb {}|{}|{}", |
| 1029 | eold, |
| 1030 | number, |
| 1031 | lastComb.type, |
| 1032 | lastComb.number, |
| 1033 | lastComb.characters); |
| Nils Diewald | 010c10f | 2013-12-17 01:58:31 +0000 | [diff] [blame] | 1034 | |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1035 | // combinator is opening and the number is not equal to the last |
| 1036 | // element on the balanceStack |
| Nils Diewald | 4fca3ff | 2013-12-29 22:59:13 +0000 | [diff] [blame] | 1037 | if (lastComb.type == 1 && lastComb.number == eold) { |
| 1038 | |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1039 | // Remove the last element - it's empty and uninteresting! |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1040 | this.combine.removeLast(); |
| 1041 | } |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1042 | |
| 1043 | // combinator is either closing (??) or another opener |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1044 | else { |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1045 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 1046 | if (DEBUG) |
| 1047 | log.trace("close element a) {}", eold); |
| Nils Diewald | 4fca3ff | 2013-12-29 22:59:13 +0000 | [diff] [blame] | 1048 | |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1049 | // Add a closer for the old element (this has following elements) |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 1050 | this.combine.add(new HighlightCombinatorElement((byte) 2, eold, false)); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1051 | }; |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1052 | |
| 1053 | // add this element number temporarily on the stack |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1054 | tempStack.add(eold); |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1055 | |
| 1056 | // Check next element |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1057 | eold = this.balanceStack.removeLast(); |
| 1058 | }; |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1059 | |
| 1060 | // Get last combinator on the stack |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1061 | lastComb = this.combine.peekLast(); |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1062 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 1063 | if (DEBUG) { |
| 1064 | log.trace("LastComb: " + lastComb.type + '|' + lastComb.number + '|' + lastComb.characters + " for " + number); |
| 1065 | log.trace("Stack for checking 2: {}|{}|{}|{}", lastComb.type, lastComb.number, lastComb.characters, number); |
| 1066 | }; |
| Nils Diewald | 010c10f | 2013-12-17 01:58:31 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (lastComb.type == 1 && lastComb.number == number) { |
| 1069 | while (lastComb.type == 1 && lastComb.number == number) { |
| 1070 | // Remove the damn thing - It's empty and uninteresting! |
| 1071 | this.combine.removeLast(); |
| 1072 | lastComb = this.combine.peekLast(); |
| 1073 | }; |
| 1074 | } |
| 1075 | else { |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 1076 | if (DEBUG) |
| 1077 | log.trace("close element b) {}", number); |
| Nils Diewald | 4fca3ff | 2013-12-29 22:59:13 +0000 | [diff] [blame] | 1078 | |
| Nils Diewald | 010c10f | 2013-12-17 01:58:31 +0000 | [diff] [blame] | 1079 | // Add a closer |
| 1080 | this.combine.add(new HighlightCombinatorElement((byte) 2, number)); |
| 1081 | }; |
| 1082 | |
| 1083 | |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1084 | // Fetch everything from the tempstack and reopen it |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1085 | for (int e : tempStack) { |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 1086 | if (DEBUG) |
| 1087 | log.trace("Reopen element {}", e); |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 1088 | combine.add(new HighlightCombinatorElement((byte) 1, e)); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1089 | balanceStack.add(e); |
| 1090 | }; |
| 1091 | }; |
| 1092 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 1093 | // Get all combined elements as a string |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1094 | public String toString () { |
| 1095 | StringBuilder sb = new StringBuilder(); |
| 1096 | for (HighlightCombinatorElement e : combine) { |
| 1097 | sb.append(e.toString()).append("\n"); |
| 1098 | }; |
| 1099 | return sb.toString(); |
| 1100 | }; |
| 1101 | }; |
| 1102 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1103 | private void _processHighlightSnippet (String clean, |
| 1104 | ArrayList<int[]> stack) { |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1105 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 1106 | if (DEBUG) |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1107 | log.trace("--- Process Highlight snippet"); |
| 1108 | |
| 1109 | int pos = 0, |
| 1110 | oldPos = 0; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1111 | |
| 1112 | this.snippetStack = new HighlightCombinator(); |
| 1113 | |
| 1114 | for (int[] element : stack) { |
| 1115 | pos = element[3] != 0 ? element[0] : element[1]; |
| 1116 | |
| Nils Diewald | f3b30ae | 2013-11-27 17:42:37 +0000 | [diff] [blame] | 1117 | if (pos > oldPos) { |
| Nils Diewald | da1722b | 2014-02-17 00:12:05 +0000 | [diff] [blame] | 1118 | |
| Nils Diewald | 23bf460 | 2014-02-18 15:47:20 +0000 | [diff] [blame] | 1119 | if (pos > clean.length()) { |
| Nils Diewald | da1722b | 2014-02-17 00:12:05 +0000 | [diff] [blame] | 1120 | pos = clean.length() - 1; |
| 1121 | }; |
| 1122 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1123 | snippetStack.addString(clean.substring(oldPos, pos)); |
| 1124 | |
| 1125 | oldPos = pos; |
| 1126 | }; |
| 1127 | |
| 1128 | if (element[3] != 0) { |
| 1129 | snippetStack.addOpen(element[2]); |
| 1130 | } |
| 1131 | else { |
| 1132 | snippetStack.addClose(element[2]); |
| 1133 | }; |
| 1134 | }; |
| 1135 | |
| Nils Diewald | f3b30ae | 2013-11-27 17:42:37 +0000 | [diff] [blame] | 1136 | if (clean.length() > pos) { |
| 1137 | snippetStack.addString(clean.substring(pos)); |
| 1138 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1139 | }; |
| 1140 | |
| 1141 | @Deprecated |
| 1142 | public String snippetHTML () { |
| 1143 | return this.getSnippetHTML(); |
| 1144 | }; |
| 1145 | |
| 1146 | @JsonProperty("snippet") |
| 1147 | public String getSnippetHTML () { |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 1148 | |
| 1149 | if (!this._processHighlight()) |
| 1150 | return null; |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1151 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 1152 | if (this.processed && this.snippetHTML != null) |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1153 | return this.snippetHTML; |
| 1154 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 1155 | if (DEBUG) |
| 1156 | log.trace("Create HTML Snippet"); |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 1157 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1158 | StringBuilder sb = new StringBuilder(); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1159 | |
| Nils Diewald | f3b30ae | 2013-11-27 17:42:37 +0000 | [diff] [blame] | 1160 | short start = (short) 0; |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1161 | short end = this.snippetStack.size(); |
| Nils Diewald | 8c22178 | 2013-12-13 19:52:58 +0000 | [diff] [blame] | 1162 | FixedBitSet level = new FixedBitSet(16); |
| 1163 | level.set(0, 15); |
| 1164 | byte[] levelCache = new byte[16]; |
| Nils Diewald | f3b30ae | 2013-11-27 17:42:37 +0000 | [diff] [blame] | 1165 | |
| 1166 | HighlightCombinatorElement elem = this.snippetStack.getFirst(); |
| 1167 | |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1168 | // Create context |
| 1169 | sb.append("<span class=\"context-left\">"); |
| 1170 | if (startMore) |
| 1171 | sb.append("<span class=\"more\"></span>"); |
| 1172 | |
| 1173 | if (elem.type == 0) { |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 1174 | sb.append(elem.toHTML(this, level, levelCache)); |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1175 | start++; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1176 | }; |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1177 | sb.append("</span>"); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1178 | |
| Nils Diewald | f3b30ae | 2013-11-27 17:42:37 +0000 | [diff] [blame] | 1179 | elem = this.snippetStack.getLast(); |
| 1180 | |
| 1181 | StringBuilder rightContext = new StringBuilder(); |
| 1182 | |
| 1183 | // Create context, if trhere is any |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1184 | rightContext.append("<span class=\"context-right\">"); |
| 1185 | if (elem != null && elem.type == 0) { |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 1186 | rightContext.append(elem.toHTML(this, level, levelCache)); |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1187 | end--; |
| Nils Diewald | f3b30ae | 2013-11-27 17:42:37 +0000 | [diff] [blame] | 1188 | }; |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1189 | if (endMore) |
| 1190 | rightContext.append("<span class=\"more\"></span>"); |
| 1191 | rightContext.append("</span>"); |
| Nils Diewald | f3b30ae | 2013-11-27 17:42:37 +0000 | [diff] [blame] | 1192 | |
| 1193 | for (short i = start; i < end; i++) { |
| Nils Diewald | 345bdc0 | 2014-01-21 21:48:57 +0000 | [diff] [blame] | 1194 | sb.append(this.snippetStack.get(i).toHTML(this, level,levelCache)); |
| Nils Diewald | f3b30ae | 2013-11-27 17:42:37 +0000 | [diff] [blame] | 1195 | }; |
| 1196 | |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1197 | sb.append(rightContext); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1198 | |
| 1199 | return (this.snippetHTML = sb.toString()); |
| 1200 | }; |
| 1201 | |
| 1202 | @Deprecated |
| 1203 | public String snippetBrackets () { |
| 1204 | return this.getSnippetBrackets(); |
| 1205 | }; |
| 1206 | |
| 1207 | @JsonIgnore |
| 1208 | public String getSnippetBrackets () { |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1209 | |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 1210 | if (!this._processHighlight()) |
| 1211 | return null; |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1212 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 1213 | if (this.processed && this.snippetBrackets != null) |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1214 | return this.snippetBrackets; |
| 1215 | |
| 1216 | StringBuilder sb = new StringBuilder(); |
| 1217 | |
| 1218 | if (startMore) |
| 1219 | sb.append("... "); |
| 1220 | |
| 1221 | for (HighlightCombinatorElement hce : this.snippetStack.stack()) { |
| 1222 | sb.append(hce.toBrackets()); |
| 1223 | }; |
| 1224 | |
| 1225 | if (endMore) |
| 1226 | sb.append(" ..."); |
| 1227 | |
| 1228 | return (this.snippetBrackets = sb.toString()); |
| 1229 | }; |
| 1230 | |
| 1231 | |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1232 | // This sorts all highlight and match spans to make them nesting correctly, |
| 1233 | // even in case they overlap |
| 1234 | // TODO: Not very fast - improve! |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 1235 | private ArrayList<int[]> _processHighlightStack () { |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 1236 | if (DEBUG) |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1237 | log.trace("--- Process Highlight stack"); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1238 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1239 | LinkedList<int[]> openList = new LinkedList<int[]>(); |
| 1240 | LinkedList<int[]> closeList = new LinkedList<int[]>(); |
| 1241 | |
| Nils Diewald | d216a03 | 2014-04-30 17:40:19 +0000 | [diff] [blame] | 1242 | // Filter multiple identifiers, that may be introduced and would |
| 1243 | // result in invalid xml |
| Nils Diewald | 50389b0 | 2014-04-11 16:27:52 +0000 | [diff] [blame] | 1244 | this._filterMultipleIdentifiers(); |
| 1245 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1246 | // Add highlight spans to balance lists |
| 1247 | openList.addAll(this.span); |
| 1248 | closeList.addAll(this.span); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1249 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1250 | // Sort balance lists |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1251 | Collections.sort(openList, new OpeningTagComparator()); |
| 1252 | Collections.sort(closeList, new ClosingTagComparator()); |
| 1253 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1254 | // New stack array |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1255 | ArrayList<int[]> stack = new ArrayList<>(openList.size() * 2); |
| 1256 | |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1257 | // Create stack unless both lists are empty |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1258 | while (!openList.isEmpty() || !closeList.isEmpty()) { |
| 1259 | |
| 1260 | if (openList.isEmpty()) { |
| 1261 | stack.addAll(closeList); |
| 1262 | break; |
| Nils Diewald | 20607ab | 2014-03-20 23:28:36 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | // Not sure about this, but it can happen |
| 1266 | else if (closeList.isEmpty()) { |
| 1267 | break; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1268 | }; |
| 1269 | |
| 1270 | if (openList.peekFirst()[0] < closeList.peekFirst()[1]) { |
| 1271 | int[] e = openList.removeFirst().clone(); |
| 1272 | e[3] = 1; |
| 1273 | stack.add(e); |
| 1274 | } |
| 1275 | else { |
| 1276 | stack.add(closeList.removeFirst()); |
| 1277 | }; |
| 1278 | }; |
| 1279 | return stack; |
| 1280 | }; |
| 1281 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1282 | /** |
| 1283 | * This will retrieve character offsets for all spans. |
| 1284 | */ |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 1285 | private boolean _processHighlightSpans () { |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1286 | |
| 1287 | if (DEBUG) |
| 1288 | log.trace("--- Process Highlight spans"); |
| 1289 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1290 | // Local document ID |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1291 | int ldid = this.localDocID; |
| 1292 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 1293 | int startPosChar = -1, endPosChar = -1; |
| 1294 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1295 | // No positionsToOffset object found |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 1296 | if (this.positionsToOffset == null) |
| 1297 | return false; |
| 1298 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1299 | // Match position |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1300 | startPosChar = this.positionsToOffset.start(ldid, this.startPos); |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1301 | |
| Nils Diewald | 20607ab | 2014-03-20 23:28:36 +0000 | [diff] [blame] | 1302 | if (DEBUG) |
| 1303 | log.trace("Unaltered startPosChar is {}", startPosChar); |
| 1304 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1305 | // Check potential differing start characters |
| 1306 | // e.g. from element spans |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1307 | if (potentialStartPosChar != -1 && |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 1308 | (startPosChar > this.potentialStartPosChar)) |
| 1309 | startPosChar = this.potentialStartPosChar; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1310 | |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1311 | endPosChar = this.positionsToOffset.end(ldid, this.endPos - 1); |
| Nils Diewald | 20607ab | 2014-03-20 23:28:36 +0000 | [diff] [blame] | 1312 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1313 | if (DEBUG) |
| Nils Diewald | 20607ab | 2014-03-20 23:28:36 +0000 | [diff] [blame] | 1314 | log.trace("Unaltered endPosChar is {}", endPosChar); |
| 1315 | |
| 1316 | // Potential end characters may come from spans with |
| 1317 | // defined character offsets like sentences including .", ... etc. |
| 1318 | if (endPosChar < potentialEndPosChar) |
| 1319 | endPosChar = potentialEndPosChar; |
| 1320 | |
| 1321 | if (DEBUG) |
| 1322 | log.trace("Refined: Match offset is pos {}-{} (chars {}-{})", |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1323 | this.startPos, |
| 1324 | this.endPos, |
| 1325 | startPosChar, |
| 1326 | endPosChar); |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 1327 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 1328 | this.identifier = null; |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1329 | |
| 1330 | // No spans yet |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 1331 | if (this.span == null) |
| 1332 | this.span = new LinkedList<int[]>(); |
| 1333 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 1334 | // Process offset char findings |
| 1335 | int[] intArray = this._processOffsetChars(ldid, startPosChar, endPosChar); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1336 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 1337 | // Recalculate startOffsetChar |
| 1338 | int startOffsetChar = startPosChar - intArray[0]; |
| Nils Diewald | 20607ab | 2014-03-20 23:28:36 +0000 | [diff] [blame] | 1339 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1340 | // Add match span |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 1341 | this.span.add(intArray); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1342 | |
| 1343 | // highlights |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 1344 | // -- I'm not sure about this. |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1345 | if (this.highlight != null) { |
| Nils Diewald | 20607ab | 2014-03-20 23:28:36 +0000 | [diff] [blame] | 1346 | if (DEBUG) |
| 1347 | log.trace("There are highlights!"); |
| Nils Diewald | 50389b0 | 2014-04-11 16:27:52 +0000 | [diff] [blame] | 1348 | |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 1349 | for (Highlight highlight : this.highlight) { |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1350 | int start = this.positionsToOffset.start( |
| 1351 | ldid, highlight.start |
| 1352 | ); |
| 1353 | |
| 1354 | int end = this.positionsToOffset.end( |
| 1355 | ldid, |
| 1356 | highlight.end |
| 1357 | ); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1358 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1359 | if (DEBUG) |
| 1360 | log.trace("PTO has retrieved {}-{} for class {}", |
| 1361 | start, |
| 1362 | end, |
| 1363 | highlight.number); |
| 1364 | |
| 1365 | start -= startOffsetChar; |
| 1366 | end -= startOffsetChar; |
| 1367 | |
| Nils Diewald | 3ef9a47 | 2013-12-02 16:06:09 +0000 | [diff] [blame] | 1368 | if (start < 0 || end < 0) |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1369 | continue; |
| 1370 | |
| Nils Diewald | 498d598 | 2014-03-03 20:09:22 +0000 | [diff] [blame] | 1371 | // Create intArray for highlight |
| Nils Diewald | 3ef9a47 | 2013-12-02 16:06:09 +0000 | [diff] [blame] | 1372 | intArray = new int[]{ |
| 1373 | start, |
| 1374 | end, |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 1375 | highlight.number, |
| Nils Diewald | 3ef9a47 | 2013-12-02 16:06:09 +0000 | [diff] [blame] | 1376 | 0 // Dummy value for later |
| 1377 | }; |
| 1378 | |
| Nils Diewald | 2cd1c3d | 2014-01-08 22:53:08 +0000 | [diff] [blame] | 1379 | this.span.add(intArray); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1380 | }; |
| 1381 | }; |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 1382 | return true; |
| 1383 | }; |
| 1384 | |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 1385 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 1386 | // Pass the local docid to retrieve character positions for the offset |
| 1387 | private int[] _processOffsetChars (int ldid, int startPosChar, int endPosChar) { |
| 1388 | |
| 1389 | int startOffsetChar = -1, endOffsetChar = -1; |
| 1390 | int startOffset = -1, endOffset = -1; |
| 1391 | |
| 1392 | // The offset is defined by a span |
| 1393 | if (this.getContext().isSpanDefined()) { |
| 1394 | |
| 1395 | if (DEBUG) |
| 1396 | log.trace("Try to expand to <{}>", |
| 1397 | this.context.getSpanContext()); |
| 1398 | |
| 1399 | this.startMore = false; |
| 1400 | this.endMore = false; |
| 1401 | |
| 1402 | int [] spanContext = this.expandContextToSpan( |
| 1403 | this.positionsToOffset.getAtomicReader(), |
| 1404 | (Bits) null, |
| 1405 | "tokens", |
| 1406 | this.context.getSpanContext() |
| 1407 | ); |
| 1408 | startOffset = spanContext[0]; |
| 1409 | endOffset = spanContext[1]; |
| 1410 | startOffsetChar = spanContext[2]; |
| 1411 | endOffsetChar = spanContext[3]; |
| 1412 | if (DEBUG) |
| 1413 | log.trace("Got context is based from span {}-{}/{}-{}", |
| 1414 | startOffset, endOffset, startOffsetChar, endOffsetChar); |
| 1415 | }; |
| 1416 | |
| 1417 | // The offset is defined by tokens or characters |
| 1418 | if (endOffset == -1) { |
| 1419 | |
| 1420 | PositionsToOffset pto = this.positionsToOffset; |
| 1421 | |
| 1422 | // The left offset is defined by tokens |
| 1423 | if (this.context.left.isToken()) { |
| 1424 | startOffset = this.startPos - this.context.left.getLength(); |
| 1425 | if (DEBUG) |
| 1426 | log.trace("PTO will retrieve {} (Left context)", startOffset); |
| 1427 | pto.add(ldid, startOffset); |
| 1428 | } |
| 1429 | |
| 1430 | // The left offset is defined by characters |
| 1431 | else { |
| 1432 | startOffsetChar = startPosChar - this.context.left.getLength(); |
| 1433 | }; |
| 1434 | |
| 1435 | // The right context is defined by tokens |
| 1436 | if (this.context.right.isToken()) { |
| 1437 | endOffset = this.endPos + this.context.right.getLength() -1; |
| 1438 | if (DEBUG) |
| 1439 | log.trace("PTO will retrieve {} (Right context)", endOffset); |
| 1440 | pto.add(ldid, endOffset); |
| 1441 | |
| 1442 | } |
| 1443 | |
| 1444 | // The right context is defined by characters |
| 1445 | else { |
| 1446 | endOffsetChar = (endPosChar == -1) ? -1 : |
| 1447 | endPosChar + this.context.right.getLength(); |
| 1448 | }; |
| 1449 | |
| 1450 | if (startOffset != -1) |
| 1451 | startOffsetChar = pto.start(ldid, startOffset); |
| 1452 | |
| 1453 | if (endOffset != -1) |
| 1454 | endOffsetChar = pto.end(ldid, endOffset); |
| 1455 | }; |
| 1456 | |
| 1457 | if (DEBUG) |
| 1458 | log.trace("Premature found offsets at {}-{}", |
| 1459 | startOffsetChar, |
| 1460 | endOffsetChar); |
| 1461 | |
| 1462 | |
| 1463 | // This can happen in case of non-token characters |
| 1464 | // in the match and null offsets |
| 1465 | if (startOffsetChar > startPosChar) |
| 1466 | startOffsetChar = startPosChar; |
| 1467 | else if (startOffsetChar < 0) |
| 1468 | startOffsetChar = 0; |
| 1469 | |
| 1470 | // No "..." at the beginning |
| 1471 | if (startOffsetChar == 0) |
| 1472 | this.startMore = false; |
| 1473 | |
| 1474 | if (endOffsetChar != -1 && endOffsetChar < endPosChar) |
| 1475 | endOffsetChar = endPosChar; |
| 1476 | |
| 1477 | if (DEBUG) |
| 1478 | log.trace("The context spans from chars {}-{}", |
| 1479 | startOffsetChar, endOffsetChar); |
| 1480 | |
| 1481 | // Get snippet information from the primary data |
| 1482 | if (endOffsetChar > -1 && |
| 1483 | (endOffsetChar < this.getPrimaryDataLength())) { |
| 1484 | this.tempSnippet = this.getPrimaryData( |
| 1485 | startOffsetChar, |
| 1486 | endOffsetChar |
| 1487 | ); |
| 1488 | } |
| 1489 | else { |
| 1490 | this.tempSnippet = this.getPrimaryData(startOffsetChar); |
| 1491 | this.endMore = false; |
| 1492 | }; |
| 1493 | |
| 1494 | if (DEBUG) |
| 1495 | log.trace("Snippet: '" + this.tempSnippet + "'"); |
| 1496 | |
| 1497 | if (DEBUG) |
| 1498 | log.trace("The match entry is {}-{} ({}-{}) with absolute offsetChars {}-{}", |
| 1499 | startPosChar - startOffsetChar, |
| 1500 | endPosChar - startOffsetChar, |
| 1501 | startPosChar, |
| 1502 | endPosChar, |
| 1503 | startOffsetChar, |
| 1504 | endOffsetChar); |
| 1505 | |
| 1506 | // TODO: Simplify |
| 1507 | return new int[]{ |
| 1508 | startPosChar - startOffsetChar, |
| 1509 | endPosChar - startOffsetChar, |
| 1510 | -1, |
| 1511 | 0}; |
| 1512 | }; |
| 1513 | |
| 1514 | |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 1515 | // Identical to KorapResult! |
| 1516 | public String toJSON () { |
| 1517 | ObjectNode json = (ObjectNode) mapper.valueToTree(this); |
| 1518 | |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 1519 | // Match was no match |
| 1520 | if (json.size() == 0) |
| 1521 | return "{}"; |
| 1522 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 1523 | json.put("context", this.getContext().toJSON()); |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 1524 | |
| Nils Diewald | cdd465b | 2014-02-24 18:47:38 +0000 | [diff] [blame] | 1525 | if (this.version != null) |
| 1526 | json.put("version", this.getVersion()); |
| 1527 | |
| Nils Diewald | bfe554b | 2014-01-09 19:35:05 +0000 | [diff] [blame] | 1528 | try { |
| 1529 | return mapper.writeValueAsString(json); |
| 1530 | } |
| 1531 | catch (Exception e) { |
| 1532 | log.warn(e.getLocalizedMessage()); |
| 1533 | }; |
| 1534 | |
| 1535 | return "{}"; |
| 1536 | }; |
| Nils Diewald | 50389b0 | 2014-04-11 16:27:52 +0000 | [diff] [blame] | 1537 | |
| 1538 | |
| 1539 | // Remove duplicate identifiers |
| 1540 | // Yeah ... I mean ... why not? |
| 1541 | private void _filterMultipleIdentifiers () { |
| 1542 | ArrayList<Integer> removeDuplicate = new ArrayList<>(10); |
| 1543 | HashSet<Integer> identifiers = new HashSet<>(20); |
| 1544 | for (int i = 0; i < this.span.size(); i++) { |
| 1545 | // span is an int array: [Start, End, Number, Dummy] |
| 1546 | int highlightNumber = this.span.get(i)[2]; |
| 1547 | |
| Nils Diewald | d216a03 | 2014-04-30 17:40:19 +0000 | [diff] [blame] | 1548 | // Number is an identifier |
| Nils Diewald | 50389b0 | 2014-04-11 16:27:52 +0000 | [diff] [blame] | 1549 | if (highlightNumber < -1) { |
| Nils Diewald | d216a03 | 2014-04-30 17:40:19 +0000 | [diff] [blame] | 1550 | |
| 1551 | // Get the real identifier |
| Nils Diewald | 50389b0 | 2014-04-11 16:27:52 +0000 | [diff] [blame] | 1552 | int idNumber = identifierNumber.get(highlightNumber); |
| 1553 | if (identifiers.contains(idNumber)) { |
| 1554 | removeDuplicate.add(i); |
| 1555 | } |
| 1556 | else { |
| 1557 | identifiers.add(idNumber); |
| 1558 | }; |
| 1559 | }; |
| 1560 | }; |
| 1561 | |
| Nils Diewald | d216a03 | 2014-04-30 17:40:19 +0000 | [diff] [blame] | 1562 | // Order the duplicates to filter from the tail |
| Nils Diewald | 50389b0 | 2014-04-11 16:27:52 +0000 | [diff] [blame] | 1563 | Collections.sort(removeDuplicate); |
| 1564 | Collections.reverse(removeDuplicate); |
| 1565 | |
| 1566 | // Delete all duplicate identifiers |
| 1567 | for (int delete : removeDuplicate) { |
| 1568 | this.span.remove(delete); |
| 1569 | }; |
| 1570 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1571 | }; |