1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
Ousía
Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _MODEL_TEST_ADVANCED_HPP_
#define _MODEL_TEST_ADVANCED_HPP_
#include <core/model/Document.hpp>
#include <core/model/Domain.hpp>
#include <core/model/Typesystem.hpp>
#include "TestDocumentBuilder.hpp"
namespace ousia {
static Rooted<StructuredClass> resolveDescriptor(Handle<Domain> domain,
const std::string &className)
{
// use the actual resolve method.
std::vector<ResolutionResult> resolved =
domain->resolve(&RttiTypes::StructuredClass, className);
// take the first valid result.
for (auto &r : resolved) {
return r.node.cast<StructuredClass>();
}
// if no valid result exists, return nullptr.
return {nullptr};
}
/**
* This constructs the "heading" domain given the book domain.
*/
static Rooted<Domain> constructHeadingDomain(Manager &mgr,
Handle<SystemTypesystem> sys,
Handle<Domain> bookDomain,
Logger &logger)
{
// set up domain node.
Rooted<Domain> domain{new Domain(mgr, sys, "headings")};
// set up cardinality (every section may have at most one heading).
Cardinality card;
card.merge({0, 1});
// set up heading StructuredClass.
Rooted<StructuredClass> heading{
new StructuredClass(mgr, "heading", domain, card, {nullptr}, true)};
// as field want to reference the field of paragraph.
Rooted<StructuredClass> p = resolveDescriptor(bookDomain, "paragraph");
heading->addFieldDescriptor(p->getFieldDescriptor(), logger);
// create a new field for headings in each section type.
std::vector<std::string> secclasses{"book", "section", "subsection",
"paragraph"};
for (auto &s : secclasses) {
Rooted<StructuredClass> desc = resolveDescriptor(bookDomain, s);
Rooted<FieldDescriptor> heading_field =
desc->createFieldDescriptor(logger,
FieldDescriptor::FieldType::SUBTREE,
"heading", true).first;
heading_field->addChild(heading);
}
return domain;
}
/**
* This constructs the "list" domain given the book domain.
*/
static Rooted<Domain> constructListDomain(Manager &mgr,
Handle<SystemTypesystem> sys,
Handle<Domain> bookDomain,
Logger &logger)
{
// set up domain node.
Rooted<Domain> domain{new Domain(mgr, sys, "list")};
// get book.paragraph
Rooted<StructuredClass> p = resolveDescriptor(bookDomain, "paragraph");
// set up item StructuredClass;
Rooted<StructuredClass> item{new StructuredClass(
mgr, "item", domain, Cardinality::any(), {nullptr}, false)};
// as field we want to reference the field of paragraph.
item->addFieldDescriptor(p->getFieldDescriptor(), logger);
// set up list StructuredClasses.
std::vector<std::string> listTypes{"ol", "ul"};
for (auto &listType : listTypes) {
Rooted<StructuredClass> list{new StructuredClass(
mgr, listType, domain, Cardinality::any(), p, false)};
Rooted<FieldDescriptor> list_field{new FieldDescriptor(mgr, list)};
list_field->addChild(item);
}
return domain;
}
/**
* This constructs the "emphasis" domain.
*/
static Rooted<Domain> constructEmphasisDomain(Manager &mgr,
Handle<SystemTypesystem> sys,
Logger &logger)
{
// set up domain node.
Rooted<Domain> domain{new Domain(mgr, sys, "emphasis")};
// create AnnotationClasses
Rooted<AnnotationClass> em{new AnnotationClass(mgr, "emphasized", domain)};
Rooted<AnnotationClass> strong{new AnnotationClass(mgr, "strong", domain)};
return domain;
}
static bool addText(Logger &logger, Handle<Document> doc,
Handle<StructuredEntity> parent, const std::string &content)
{
// Add the text.
Rooted<StructuredEntity> text =
buildStructuredEntity(doc, logger, parent, {"text"});
if (text.isNull()) {
return false;
}
// And the primitive content
Variant content_var{content.c_str()};
Rooted<DocumentPrimitive> primitive{new DocumentPrimitive(
parent->getManager(), text, content_var, DEFAULT_FIELD_NAME)};
return true;
}
static bool addHeading(Logger &logger, Handle<Document> doc,
Handle<StructuredEntity> parent, const std::string &text)
{
// Add the heading.
Rooted<StructuredEntity> heading = buildStructuredEntity(
doc, logger, parent, {"heading"}, "heading", Variant::mapType{}, "");
if (heading.isNull()) {
return false;
}
// Add its text.
if (!addText(logger, doc, heading, text)) {
return false;
}
return true;
}
// Only works for non-overlapping annotations!
static bool addAnnotation(Logger &logger, Handle<Document> doc,
Handle<StructuredEntity> parent,
const std::string &text, const std::string &annoClass)
{
Manager &mgr = parent->getManager();
Rooted<Anchor> start{new Anchor(mgr, parent)};
if (!addText(logger, doc, parent, text)) {
return false;
}
Rooted<Anchor> end{new Anchor(mgr, parent)};
Rooted<AnnotationEntity> anno =
buildAnnotationEntity(doc, logger, {annoClass}, start, end);
if (anno.isNull()) {
return false;
}
return true;
}
/**
* This constructs a more advanced book document using not only the book
* domain but also headings, emphasis and lists.
*/
static Rooted<Document> constructAdvancedDocument(Manager &mgr, Logger &logger,
Handle<Domain> bookDom,
Handle<Domain> headingDom,
Handle<Domain> listDom,
Handle<Domain> emphasisDom)
{
// Start with the (empty) document.
Rooted<Document> doc{new Document(mgr, "kant_was_ist_aufklaerung.oxd")};
doc->referenceDomains({bookDom, headingDom, listDom, emphasisDom});
// Add the root.
Rooted<StructuredEntity> book =
buildRootStructuredEntity(doc, logger, {"book"});
if (book.isNull()) {
return {nullptr};
}
// Add the heading.
{
Rooted<StructuredEntity> heading = buildStructuredEntity(
doc, logger, book, {"heading"}, "heading", Variant::mapType{}, "");
if (heading.isNull()) {
return {nullptr};
}
if (!addText(logger, doc, heading, "Beantwortung der Frage: ")) {
return {nullptr};
}
if (!addAnnotation(logger, doc, heading, "Was ist Aufklärung?",
"emphasized")) {
return {nullptr};
}
}
// Add the main section.
Rooted<StructuredEntity> sec =
buildStructuredEntity(doc, logger, book, {"section"});
if (sec.isNull()) {
return {nullptr};
}
// Add the heading.
if (!addHeading(logger, doc, sec, "Was ist Aufklärung?")) {
return {nullptr};
}
// Add paragraph with main text.
{
Rooted<StructuredEntity> p =
buildStructuredEntity(doc, logger, sec, {"paragraph"});
if (p.isNull()) {
return {nullptr};
}
// Add its text.
{
if (!addAnnotation(logger, doc, p,
"Aufklärung ist der Ausgang des Menschen aus "
"seiner selbstverschuldeten Unmündigkeit!",
"strong")) {
return {nullptr};
}
if (!addAnnotation(logger, doc, p, "Unmündigkeit", "emphasized")) {
return {nullptr};
}
if (!addText(logger, doc, p,
"ist das Unvermögen, sich seines Verstandes ohne "
"Leitung eines anderen zu bedienen. ")) {
return {nullptr};
}
if (!addAnnotation(logger, doc, p, "Selbstverschuldet",
"emphasized")) {
return {nullptr};
}
if (!addText(logger, doc, p,
" ist diese Unmündigkeit, wenn die Ursache derselben "
"nicht am Mangel des Verstandes, sondern der "
"Entschließung und des Mutes liegt, sich seiner ohne "
"Leitung eines andern zu bedienen.")) {
return {nullptr};
}
if (!addAnnotation(logger, doc, p,
"Sapere aude! Habe Mut, dich deines eigenen "
"Verstandes zu bedienen!",
"emphasized")) {
return {nullptr};
}
if (!addText(logger, doc, p,
" ist also der Wahlspruch der Aufklärung.")) {
return {nullptr};
}
}
}
// Add the "Lesarten" section
Rooted<StructuredEntity> lesarten =
buildStructuredEntity(doc, logger, book, {"section"});
if (lesarten.isNull()) {
return {nullptr};
}
// Add the heading.
if (!addHeading(logger, doc, lesarten, "Lesarten")) {
return {nullptr};
}
// Add list with citations
{
Rooted<StructuredEntity> ul =
buildStructuredEntity(doc, logger, lesarten, {"ul"});
if (ul.isNull()) {
return {nullptr};
}
std::vector<std::string> citations{
"Berlinische Monatsschrift. Dezember-Heft 1784. S. 481–494.",
"Kant. Kleine Schriften. Neuwied 1793. Haupt. 8o. S. 34–50.",
"I. Kant. Zerstreute Aufsätze. Frankfurt und Leipzig 1793. 8o. S. "
"25–37.",
"I. Kant. Sämmtliche kleine Schriften. 4 Bände. 1797–98. 8o. "
"Königsberg u. Leipzig (Voigt, Jena). Nachdruck. Bd. III, S. "
"159–172.",
" I. Kant's vermischte Schriften. 3 Bände. Halle 1799. "
"(Tieftrunk). Bd. II. S. 687–700.",
"Kant. Vorzügliche kleine Schriften und Aufsätze, hrsg. mit Noten "
"von F. Ch. Starke. 2 Bände. Leipzig 1833 und Quedlinburg 1838. "
"Bd. I, S. 75–84."};
for (auto &cit : citations) {
Rooted<StructuredEntity> item =
buildStructuredEntity(doc, logger, ul, {"item"});
if (item.isNull()) {
return {nullptr};
}
if (!addText(logger, doc, item, cit)) {
return {nullptr};
}
}
}
return doc;
}
}
#endif /* _TEST_DOCUMENT_HPP_ */
|