Extract.kt 5.08 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * search-doc-service
 * Copyright (C) 2020 Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package org.memobase.helpers

21
import com.beust.klaxon.JsonArray
Jonas Waeber's avatar
Jonas Waeber committed
22
23
import com.beust.klaxon.JsonObject
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
24
import org.memobase.model.FacettedContainer
Jonas Waeber's avatar
Jonas Waeber committed
25
26
27
import org.memobase.model.LanguageContainer

object Extract {
Jonas Waeber's avatar
Jonas Waeber committed
28
    private val log = LogManager.getLogger("ExtractSearchDoc")
Jonas Waeber's avatar
Jonas Waeber committed
29

Jonas Waeber's avatar
Jonas Waeber committed
30
    private fun languageTag(entity: JsonObject, language: String): List<String> {
Jonas Waeber's avatar
Jonas Waeber committed
31
32
33
34
35
36
37
38
        return entity["@value"].let {
            if (entity["@language"] == language && it != null)
                listOf(it as String)
            else
                emptyList()
        }
    }

Jonas Waeber's avatar
Jonas Waeber committed
39
    fun languageContainer(parent: String, entity: Any?): List<LanguageContainer> {
Jonas Waeber's avatar
Jonas Waeber committed
40
41
42
43
44
        return when (entity) {
            is String -> listOf(LanguageContainer(emptyList(), emptyList(), emptyList(), listOf(entity)))
            is JsonObject ->
                listOf(
                    LanguageContainer(
Jonas Waeber's avatar
Jonas Waeber committed
45
46
47
                        de = languageTag(entity, "de"),
                        fr = languageTag(entity, "fr"),
                        it = languageTag(entity, "it"),
Jonas Waeber's avatar
Jonas Waeber committed
48
49
50
51
52
53
54
55
56
57
                        un = emptyList()
                    )
                )
            is List<*> ->
                listOf(entity.mapNotNull { subEntity ->
                    when (subEntity) {
                        is String ->
                            LanguageContainer(emptyList(), emptyList(), emptyList(), listOf(subEntity))
                        is JsonObject ->
                            LanguageContainer(
Jonas Waeber's avatar
Jonas Waeber committed
58
59
60
                                de = languageTag(subEntity, "de"),
                                fr = languageTag(subEntity, "fr"),
                                it = languageTag(subEntity, "it"),
Jonas Waeber's avatar
Jonas Waeber committed
61
62
63
64
65
66
67
68
69
70
                                un = emptyList()
                            )
                        else -> {
                            log.error("Could not extract language container from subEntity $subEntity in entity $entity.")
                            null
                        }
                    }
                }.reduce { acc, languageContainer ->
                    acc.merge(languageContainer)
                })
Jonas Waeber's avatar
Jonas Waeber committed
71
72
73
74
            null -> {
                log.info("No items in $parent found.")
                emptyList()
            }
Jonas Waeber's avatar
Jonas Waeber committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
            else -> {
                log.error("Could not extract language container from entity: $entity in parent $parent.")
                emptyList()
            }
        }
    }

    fun typedEntityByType(
        entities: List<JsonObject>,
        field: String,
        type: String,
        annotationField: String
    ): List<LanguageContainer> {
        return entities.filter { it[field] == type }.flatMap {
Jonas Waeber's avatar
Jonas Waeber committed
89
            languageContainer(it["@id"] as String, it[annotationField])
Jonas Waeber's avatar
Jonas Waeber committed
90
91
92
        }
    }

Jonas Waeber's avatar
Jonas Waeber committed
93
    fun facetEntity(entities: List<JsonObject>, property: String): List<FacettedContainer> {
Jonas Waeber's avatar
Jonas Waeber committed
94
        return entities.mapNotNull {
Jonas Waeber's avatar
Jonas Waeber committed
95
            val lang = languageContainer(it["@id"] as String, it[property])
Jonas Waeber's avatar
Jonas Waeber committed
96
97
98
            if (lang.isEmpty()) {
                null
            } else {
Jonas Waeber's avatar
Jonas Waeber committed
99
                FacettedContainer(lang.first(), lang.first().toList())
Jonas Waeber's avatar
Jonas Waeber committed
100
101
102
103
104
105
            }
        }
    }

    fun extractSourceId(entities: List<JsonObject>): String {
        return entities.first {
106
107
            it["type"] as String == "original"
        }["identifier"] as String? ?: error("Identifier contains no property rico:identifier: $entities.")
Jonas Waeber's avatar
Jonas Waeber committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    }

    fun listOfStrings(value: Any?): List<String> {
        return when (value) {
            is String -> listOf(value)
            is List<*> ->
                if (value[0] is String)
                    value as List<String>
                else
                    emptyList()
            else ->
                emptyList()
        }
    }
122

Jonas Waeber's avatar
Jonas Waeber committed
123
    fun identifiers(value: Any?): List<String> {
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
        return when (value) {
            is String -> listOf(value)
            is JsonObject -> value["@id"].let { if (it is String) listOf(it) else emptyList() }
            is JsonArray<*> ->
                value.mapNotNull { item ->
                    when (item) {
                        is String -> item
                        is JsonObject -> value["@id"].let { id: Any? ->
                            if (id is String)
                                id
                            else null
                        }
                        else -> null
                    }
                }
            else -> emptyList()
        }
    }
Jonas Waeber's avatar
Jonas Waeber committed
142
}