This class contains utility methods to output GATE documents in a JSON format which is (deliberately) close to the format used by Twitter to represent entities such as user mentions and hashtags in Tweets. { "text":"Text of the document", "entities":{ "Person":[ {
| 100 | * |
| 101 | */ |
| 102 | public class DocumentJsonUtils { |
| 103 | |
| 104 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 105 | |
| 106 | private static final JsonFactory JSON_FACTORY = new JsonFactory(); |
| 107 | |
| 108 | /** |
| 109 | * Write a GATE document to the specified OutputStream. The document |
| 110 | * text will be written as a property named "text" and the specified |
| 111 | * annotations will be written as "entities". |
| 112 | * |
| 113 | * @param doc the document to write |
| 114 | * @param annotationsMap annotations to write. |
| 115 | * @param out the {@link OutputStream} to write to. |
| 116 | * @throws JsonGenerationException if a problem occurs while |
| 117 | * generating the JSON |
| 118 | * @throws IOException if an I/O error occurs. |
| 119 | */ |
| 120 | public static void writeDocument(Document doc, |
| 121 | Map<String, Collection<Annotation>> annotationsMap, OutputStream out) |
| 122 | throws JsonGenerationException, IOException { |
| 123 | try(JsonGenerator jsonG = JSON_FACTORY.createGenerator(out)) { |
| 124 | writeDocument(doc, annotationsMap, jsonG); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Write a GATE document to the specified Writer. The document text |
| 130 | * will be written as a property named "text" and the specified |
| 131 | * annotations will be written as "entities". |
| 132 | * |
| 133 | * @param doc the document to write |
| 134 | * @param annotationsMap annotations to write. |
| 135 | * @param out the {@link Writer} to write to. |
| 136 | * @throws JsonGenerationException if a problem occurs while |
| 137 | * generating the JSON |
| 138 | * @throws IOException if an I/O error occurs. |
| 139 | */ |
| 140 | public static void writeDocument(Document doc, |
| 141 | Map<String, Collection<Annotation>> annotationsMap, Writer out) |
| 142 | throws JsonGenerationException, IOException { |
| 143 | try(JsonGenerator jsonG = JSON_FACTORY.createGenerator(out)) { |
| 144 | writeDocument(doc, annotationsMap, jsonG); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Write a GATE document to the specified File. The document text will |
| 150 | * be written as a property named "text" and the specified annotations |
| 151 | * will be written as "entities". |
| 152 | * |
| 153 | * @param doc the document to write |
| 154 | * @param annotationsMap annotations to write. |
| 155 | * @param out the {@link File} to write to. |
| 156 | * @throws JsonGenerationException if a problem occurs while |
| 157 | * generating the JSON |
| 158 | * @throws IOException if an I/O error occurs. |
| 159 | */ |