Akron | fc3bd27 | 2025-04-04 16:15:44 +0200 | [diff] [blame^] | 1 | package termmapper |
| 2 | |
| 3 | /* |
| 4 | $( => PUNCT PunctType=Brck ``, '', *RRB*, *LRB*, - |
| 5 | $, => PUNCT PunctType=Comm , |
| 6 | $. => PUNCT PunctType=Peri ., :, ?, ;, ! |
| 7 | ADJA => ADJ _ neuen, neue, deutschen, ersten, anderen |
| 8 | ADJD => ADJ Variant=Short gut, rund, knapp, deutlich, möglich |
| 9 | ADV => ADV _ auch, nur, noch, so, aber |
| 10 | APPO => ADP AdpType=Post zufolge, nach, gegenüber, wegen, über |
| 11 | APPR => ADP AdpType=Prep in, von, mit, für, auf |
| 12 | APPRART => ADP AdpType=Prep|PronType=Art im, am, zum, zur, vom |
| 13 | APZR => ADP AdpType=Circ an, hinaus, aus, her, heraus |
| 14 | ART => DET PronType=Art der, die, den, des, das |
| 15 | CARD => NUM NumType=Card 000, zwei, drei, vier, fünf |
| 16 | FM => X Foreign=Yes New, of, de, Times, the |
| 17 | ITJ => INTJ _ naja, Ach, äh, Na, piep |
| 18 | KOKOM => CCONJ ConjType=Comp als, wie, denn, wir |
| 19 | KON => CCONJ _ und, oder, sondern, sowie, aber |
| 20 | KOUI => SCONJ _ um, ohne, statt, anstatt, Ums |
| 21 | KOUS => SCONJ _ daß, wenn, weil, ob, als |
| 22 | NE => PROPN _ SPD, Deutschland, USA, dpa, Bonn |
| 23 | NN => NOUN _ Prozent, Mark, Millionen, November, Jahren |
| 24 | PAV => ADV PronType=Dem |
| 25 | PDAT => DET PronType=Dem dieser, diese, diesem, dieses, diesen |
| 26 | PDS => PRON PronType=Dem das, dies, die, diese, der |
| 27 | PIAT => DET PronType=Ind,Neg,Tot keine, mehr, alle, kein, beiden |
| 28 | PIDAT => DET AdjType=Pdt|PronType=Ind,Neg,Tot |
| 29 | PIS => PRON PronType=Ind,Neg,Tot man, allem, nichts, alles, mehr |
| 30 | PPER => PRON PronType=Prs es, sie, er, wir, ich |
| 31 | PPOSAT => DET Poss=Yes|PronType=Prs ihre, seine, seiner, ihrer, ihren |
| 32 | PPOSS => PRON Poss=Yes|PronType=Prs ihren, Seinen, seinem, unsrigen, meiner |
| 33 | PRELAT => DET PronType=Rel deren, dessen, die |
| 34 | PRELS => PRON PronType=Rel die, der, das, dem, denen |
| 35 | PRF => PRON PronType=Prs|Reflex=Yes sich, uns, mich, mir, dich |
| 36 | PTKA => PART _ zu, am, allzu, Um |
| 37 | PTKANT => PART PartType=Res nein, ja, bitte, Gewiß, Also |
| 38 | PTKNEG => PART Polarity=Neg nicht |
| 39 | PTKVZ => ADP PartType=Vbp an, aus, ab, vor, auf |
| 40 | PTKZU => PART PartType=Inf zu, zur, zum |
| 41 | PWAT => DET PronType=Int welche, welchen, welcher, wie, welchem |
| 42 | PWAV => ADV PronType=Int wie, wo, warum, wobei, wonach |
| 43 | PWS => PRON PronType=Int was, wer, wem, wen, welches |
| 44 | TRUNC => X Hyph=Yes Staats-, Industrie-, Finanz-, Öl-, Lohn- |
| 45 | VAFIN => AUX Mood=Ind|VerbForm=Fin ist, hat, wird, sind, sei |
| 46 | VAIMP => AUX Mood=Imp|VerbForm=Fin Seid, werde, Sei |
| 47 | VAINF => AUX VerbForm=Inf werden, sein, haben, worden, Dabeisein |
| 48 | VAPP => AUX Aspect=Perf|VerbForm=Part worden, gewesen, geworden, gehabt, werden |
| 49 | VMFIN => VERB Mood=Ind|VerbForm=Fin|VerbType=Mod kann, soll, will, muß, sollen |
| 50 | VMINF => VERB VerbForm=Inf|VerbType=Mod können, müssen, wollen, dürfen, sollen |
| 51 | VMPP => VERB Aspect=Perf|VerbForm=Part|VerbType=Mod gewollt |
| 52 | VVFIN => VERB Mood=Ind|VerbForm=Fin sagte, gibt, geht, steht, kommt |
| 53 | VVIMP => VERB Mood=Imp|VerbForm=Fin siehe, sprich, schauen, Sagen, gestehe |
| 54 | VVINF => VERB VerbForm=Inf machen, lassen, bleiben, geben, bringen |
| 55 | VVIZU => VERB VerbForm=Inf einzusetzen, durchzusetzen, aufzunehmen, abzubauen, umzusetzen |
| 56 | VVPP => VERB Aspect=Perf|VerbForm=Part gemacht, getötet, gefordert, gegeben, gestellt |
| 57 | XY => X _ dpa, ap, afp, rtr, wb |
| 58 | */ |
| 59 | |
| 60 | import ( |
| 61 | "strconv" |
| 62 | "strings" |
| 63 | |
| 64 | "github.com/rs/zerolog/log" |
| 65 | "github.com/tidwall/gjson" |
| 66 | "github.com/tidwall/sjson" |
| 67 | ) |
| 68 | |
| 69 | /* |
| 70 | import ( |
| 71 | "encoding/json" |
| 72 | "fmt" |
| 73 | "log" |
| 74 | "strings" |
| 75 | ) |
| 76 | |
| 77 | var mapping = map[string]string{ |
| 78 | "$(":"PUNCT", |
| 79 | } |
| 80 | |
| 81 | // Recursive function to turn the UPos query into a STTS query |
| 82 | func koralRewriteUpos2Stts(koralquery interface{}) interface{} { |
| 83 | switch v := koralquery.(type) { |
| 84 | case map[string]interface{}: |
| 85 | // Check for '@type' key and act accordingly |
| 86 | if typ, ok := v["@type"].(string); ok { |
| 87 | switch typ { |
| 88 | case "koral:term": |
| 89 | |
| 90 | // Modify the key to use STTS |
| 91 | // This may require to turn object into a koral:token with terms like: |
| 92 | |
| 93 | |
| 94 | if key, ok := v["key"].(string); ok { |
| 95 | v["key"] = "hallo-" + key |
| 96 | } |
| 97 | case "operation": |
| 98 | // Handle the 'operators' key by recursively modifying each operator |
| 99 | if operators, ok := v["operators"].([]interface{}); ok { |
| 100 | for i, operator := range operators { |
| 101 | operators[i] = modifyJSON(operator) |
| 102 | } |
| 103 | v["operators"] = operators |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | // Recursively modify any nested maps |
| 108 | for k, val := range v { |
| 109 | v[k] = modifyJSON(val) |
| 110 | } |
| 111 | return v |
| 112 | case []interface{}: |
| 113 | // Recursively modify elements of arrays |
| 114 | for i, item := range v { |
| 115 | v[i] = modifyJSON(item) |
| 116 | } |
| 117 | return v |
| 118 | } |
| 119 | return koralquery |
| 120 | } |
| 121 | |
| 122 | func main() { |
| 123 | // Sample JSON input string |
| 124 | jsonStr := `{ |
| 125 | "@type": "operation", |
| 126 | "operators": [ |
| 127 | { |
| 128 | "@type": "term", |
| 129 | "key": "example1" |
| 130 | }, |
| 131 | { |
| 132 | "@type": "term", |
| 133 | "key": "example2" |
| 134 | }, |
| 135 | { |
| 136 | "@type": "operation", |
| 137 | "operators": [ |
| 138 | { |
| 139 | "@type": "term", |
| 140 | "key": "nested" |
| 141 | } |
| 142 | ] |
| 143 | } |
| 144 | ] |
| 145 | }` |
| 146 | |
| 147 | // Parse the JSON string into a generic interface{} |
| 148 | var data interface{} |
| 149 | err := json.Unmarshal([]byte(jsonStr), &data) |
| 150 | if err != nil { |
| 151 | log.Fatal("Error unmarshaling JSON:", err) |
| 152 | } |
| 153 | |
| 154 | // Modify the JSON structure recursively |
| 155 | modifiedData := modifyJSON(data) |
| 156 | |
| 157 | // Marshal the modified data back into a JSON string |
| 158 | modifiedJSON, err := json.MarshalIndent(modifiedData, "", " ") |
| 159 | if err != nil { |
| 160 | log.Fatal("Error marshaling JSON:", err) |
| 161 | } |
| 162 | |
| 163 | // Output the modified JSON string |
| 164 | fmt.Println(string(modifiedJSON)) |
| 165 | } |
| 166 | |
| 167 | |
| 168 | |
| 169 | func turnupostostts(json string, targetFoundry string, targetLayer string) { |
| 170 | if targetLayer == "" { |
| 171 | targetLayer = "p" |
| 172 | } |
| 173 | |
| 174 | ldType := "@type" |
| 175 | |
| 176 | if ldType == "koral:span" { |
| 177 | next |
| 178 | } |
| 179 | if ldType == "koral:term" { |
| 180 | if foundry == if layer === key -> rewrite |
| 181 | } |
| 182 | |
| 183 | // Iterate through the query and whenever a term is requested without a foundry, and without a layser or layer p, |
| 184 | // change the key following the mapping |
| 185 | |
| 186 | |
| 187 | } |
| 188 | |
| 189 | func addupostooutput(json string, reffoundry string, foundry string) { |
| 190 | // https://universaldependencies.org/tagset-conversion/de-stts-uposf.html |
| 191 | // Iterate through all matches and add to all xml snippets a line of foundry |
| 192 | |
| 193 | } |
| 194 | |
| 195 | */ |
| 196 | |
| 197 | func Hui() string { |
| 198 | return "test" |
| 199 | } |
| 200 | |
| 201 | func Map2(json []byte) string { |
| 202 | /* |
| 203 | result := gjson.GetBytes(json, "query") |
| 204 | var raw []byte |
| 205 | if result.Index > 0 { |
| 206 | raw = json[result.Index:result.Index+len(result.Raw)] |
| 207 | } else { |
| 208 | raw = []byte(result.Raw) |
| 209 | } |
| 210 | |
| 211 | if result.IsObject() { |
| 212 | koralType := gjson.GetBytes(raw, "@type").String() |
| 213 | switch koralType { |
| 214 | case "koral:term": |
| 215 | |
| 216 | } |
| 217 | } |
| 218 | */ |
| 219 | |
| 220 | koralObj := gjson.ParseBytes(json) |
| 221 | |
| 222 | switch koralObj.Get("@type").String() { |
| 223 | case "koral:term": |
| 224 | { |
| 225 | if koralObj.Get("value").String() == "KOKOM" { |
| 226 | // TODO: Turn this in a token, if it isn't already! |
| 227 | newJson, _ := sjson.Set(string(json), "value", "CCONJ") |
| 228 | return newJson |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | case "koral:operation": |
| 233 | { |
| 234 | |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | /* |
| 239 | |
| 240 | var raw []byte |
| 241 | if result.Index > 0 { |
| 242 | raw = json[result.Index:result.Index+len(result.Raw)] |
| 243 | } else { |
| 244 | raw = []byte(result.Raw) |
| 245 | } |
| 246 | */ |
| 247 | return "jj" |
| 248 | } |
| 249 | |
| 250 | // token writes a token to the string builder |
| 251 | func token(strBuilder *strings.Builder, foundry string, layer string, keys []string) { |
| 252 | strBuilder.WriteString(`{"@type":"koral:token","wrap":`) |
| 253 | if len(keys) > 1 { |
| 254 | termGroup(strBuilder, foundry, layer, keys) |
| 255 | } else { |
| 256 | term(strBuilder, foundry, layer, keys[0], true) |
| 257 | } |
| 258 | strBuilder.WriteString(`}`) |
| 259 | } |
| 260 | |
| 261 | // termGroup writes a termGroup to the string builder |
| 262 | func termGroup(strBuilder *strings.Builder, foundry string, layer string, keys []string) { |
| 263 | strBuilder.WriteString(`{"@type":"koral:termGroup","relation":"relation:and","operation":"operation:and","operands":[`) |
| 264 | for i, key := range keys { |
| 265 | term(strBuilder, foundry, layer, key, true) // temporary |
| 266 | if i < len(keys)-1 { |
| 267 | strBuilder.WriteString(",") |
| 268 | } |
| 269 | } |
| 270 | strBuilder.WriteString(`]}`) |
| 271 | } |
| 272 | |
| 273 | // term writes a term to the string builder |
| 274 | func term(strBuilder *strings.Builder, foundry string, layer string, key string, match bool) { |
| 275 | |
| 276 | // TODO: May have ne!!!! |
| 277 | strBuilder.WriteString(`{"@type":"koral:term","match":"match:`) |
| 278 | if match { |
| 279 | strBuilder.WriteString("eq") |
| 280 | } else { |
| 281 | strBuilder.WriteString("ne") |
| 282 | } |
| 283 | strBuilder.WriteString(`","foundry":"`) |
| 284 | strBuilder.WriteString(foundry) |
| 285 | strBuilder.WriteString(`","layer":"`) |
| 286 | strBuilder.WriteString(layer) |
| 287 | strBuilder.WriteString(`","key":"`) |
| 288 | strBuilder.WriteString(key) |
| 289 | strBuilder.WriteString(`"}`) |
| 290 | } |
| 291 | |
| 292 | func flatten() { |
| 293 | |
| 294 | // if a termGroup isan operand in a termGroup with the same relation/operation: |
| 295 | // flatten the termGroup into the parent termGroup |
| 296 | |
| 297 | // if a termGroup has only a single term, remove the group |
| 298 | } |
| 299 | |
| 300 | func replaceWrappedTerm(jsonString string, foundry string, layer string, key string) string { |
| 301 | var err error |
| 302 | jsonString, err = sjson.Set(jsonString, "foundry", foundry) |
| 303 | if err != nil { |
| 304 | log.Error().Err(err).Msg("Error setting foundry") |
| 305 | } |
| 306 | jsonString, err = sjson.Set(jsonString, "layer", layer) |
| 307 | if err != nil { |
| 308 | log.Error().Err(err).Msg("Error setting layer") |
| 309 | } |
| 310 | jsonString, err = sjson.Set(jsonString, "key", key) |
| 311 | if err != nil { |
| 312 | log.Error().Err(err).Msg("Error setting key") |
| 313 | } |
| 314 | return jsonString |
| 315 | } |
| 316 | |
| 317 | func replaceGroupedTerm(jsonString string, op []int, foundry string, layer string, key string) string { |
| 318 | var err error |
| 319 | |
| 320 | strInt := "operands." + strconv.Itoa(op[0]) + "." |
| 321 | jsonString, err = sjson.Set(jsonString, strInt+"foundry", foundry) |
| 322 | if err != nil { |
| 323 | log.Error().Err(err).Msg("Error setting foundry") |
| 324 | } |
| 325 | jsonString, err = sjson.Set(jsonString, strInt+"layer", layer) |
| 326 | if err != nil { |
| 327 | log.Error().Err(err).Msg("Error setting layer") |
| 328 | } |
| 329 | jsonString, err = sjson.Set(jsonString, strInt+"key", key) |
| 330 | if err != nil { |
| 331 | log.Error().Err(err).Msg("Error setting key") |
| 332 | } |
| 333 | |
| 334 | if len(op) > 1 { |
| 335 | for i := 1; i < len(op); i++ { |
| 336 | jsonString, err = sjson.Delete(jsonString, "operands."+strconv.Itoa(op[i])) |
| 337 | if err != nil { |
| 338 | log.Error().Err(err).Msg("Error deleting operand") |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return jsonString |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | func replaceTermWithToken(jsonString string) string { |
| 348 | // Replace the term with the token |
| 349 | replacedString, err := sjson.Set(jsonString, "wrap.operands.0", token()) |
| 350 | if err != nil { |
| 351 | return jsonString // Return the original string in case of an error |
| 352 | } |
| 353 | return replacedString |
| 354 | |
| 355 | // case1: 1 -> 1 the term is an operand in a termGroup with the same relation/operation |
| 356 | // case2: 1 -> 1 the term is wrapped |
| 357 | // case3: 1 -> 1 the term is an operand in a termGroup with a different relation/operation |
| 358 | // case4: n -> 1 the term is an operand in a termGroup with the same relation/operation |
| 359 | // case5: n -> 1 the term is wrapped |
| 360 | // case6: n -> 1 the term is an operand in a termGroup with a different relation/operation |
| 361 | // case7: 1 -> n the term is an operand in a termGroup with the same relation/operation |
| 362 | // case8: 1 -> n the term is wrapped |
| 363 | // case9: 1 -> n the term is an operand in a termGroup with a different relation/operation |
| 364 | } |
| 365 | */ |
| 366 | |
| 367 | func Map(jsonStr string) string { |
| 368 | |
| 369 | obj := gjson.Get(jsonStr, "query") |
| 370 | |
| 371 | // value := gjson.Get(json, "name.last") |
| 372 | |
| 373 | /* |
| 374 | |
| 375 | // Modify the JSON structure recursively |
| 376 | modifiedData := modifyJSON(ast.NewAny(data)) |
| 377 | |
| 378 | // Marshal the modified data back into a JSON string |
| 379 | modifiedJSON, err := sonic.MarshalString(modifiedData) |
| 380 | |
| 381 | // Parse the JSON string into a generic interface{} |
| 382 | var data interface{} |
| 383 | |
| 384 | err := sonic.UnmarshalString(jsonStr, data) |
| 385 | |
| 386 | if err != nil { |
| 387 | log.Fatal("Error unmarshaling JSON:", err) |
| 388 | return "" |
| 389 | } |
| 390 | |
| 391 | |
| 392 | |
| 393 | if err != nil { |
| 394 | log.Fatal("Error marshaling JSON:", err) |
| 395 | } |
| 396 | */ |
| 397 | // Output the modified JSON string |
| 398 | return obj.String() //modifyJSON(obj) |
| 399 | } |
| 400 | |
| 401 | // Recursive function to modify JSON using Sonic library |
| 402 | //func modifyJSON(data gjson.Result) string { |
| 403 | |
| 404 | // Check if data is a map |
| 405 | // if data.IsObject() { |
| 406 | /* |
| 407 | dataMap := data.Map() |
| 408 | |
| 409 | koralType := dataMap["@type"].String() |
| 410 | |
| 411 | // Look for @type key |
| 412 | |
| 413 | switch koralType { |
| 414 | case "koral:term": |
| 415 | // Modify the key by adding 'hallo-' prefix |
| 416 | |
| 417 | // sjson.SetRaw(data.String()) |
| 418 | sjson.Set(data.Path(data.Bytes()), "key", "hallo-"+dataMap["key"].String()) |
| 419 | |
| 420 | dataMap["key"] = "hallo-" + dataMap["key"].String() |
| 421 | /* |
| 422 | if key, found := data.GetString("key"); found { |
| 423 | data.Set("key", "hallo-"+key) |
| 424 | } |
| 425 | */ |
| 426 | /* |
| 427 | case "koral:operation": |
| 428 | // Handle the 'operators' key by recursively modifying each operator |
| 429 | if operators, found := data.GetArray("operators"); found { |
| 430 | for i := range operators { |
| 431 | operators[i] = modifyJSON(operators[i]) |
| 432 | } |
| 433 | data.Set("operators", operators) |
| 434 | } |
| 435 | }*/ |
| 436 | /* |
| 437 | // Recursively modify any nested objects |
| 438 | data.ForEach(func(k string, v sonic.Any) { |
| 439 | data.Set(k, modifyJSON(v)) |
| 440 | }) |
| 441 | */ |
| 442 | //} |
| 443 | // Handle arrays by modifying elements recursively |
| 444 | /* |
| 445 | if data.IsArray() { |
| 446 | for i := range data.GetArray() { |
| 447 | data.Set(i, modifyJSON(data.GetArray()[i])) |
| 448 | } |
| 449 | } |
| 450 | */ |
| 451 | /* |
| 452 | return data |
| 453 | } |
| 454 | */ |