Support simple mappings (tested)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d654485
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+testdata/sandbox
diff --git a/termmapper.go b/termmapper.go
index 16ff596..791c112 100644
--- a/termmapper.go
+++ b/termmapper.go
@@ -27,6 +27,10 @@
PIAT => DET PronType=Ind,Neg,Tot keine, mehr, alle, kein, beiden
PIDAT => DET AdjType=Pdt|PronType=Ind,Neg,Tot
PIS => PRON PronType=Ind,Neg,Tot man, allem, nichts, alles, mehr
+
+
+PIS => PRON & PronType=[Ind|Neg|Tot]
+
PPER => PRON PronType=Prs es, sie, er, wir, ich
PPOSAT => DET Poss=Yes|PronType=Prs ihre, seine, seiner, ihrer, ihren
PPOSS => PRON Poss=Yes|PronType=Prs ihren, Seinen, seinem, unsrigen, meiner
@@ -253,7 +257,7 @@
func token(strBuilder *strings.Builder, terms []Term, positive bool) {
strBuilder.WriteString(`{"@type":"koral:token","wrap":`)
if len(terms) > 1 {
- termGroup(strBuilder, terms, positive)
+ termGroup(strBuilder, terms, true, positive)
} else {
term(strBuilder, terms[0], positive)
}
@@ -261,10 +265,10 @@
}
// termGroup writes a termGroup to the string builder
-func termGroup(strBuilder *strings.Builder, terms []Term, positive bool) {
+func termGroup(strBuilder *strings.Builder, terms []Term, operationAnd bool, positive bool) {
strBuilder.WriteString(`{"@type":"koral:termGroup",`)
- if positive {
+ if operationAnd {
strBuilder.WriteString(`"relation":"relation:and","operation":"operation:and",`)
} else {
strBuilder.WriteString(`"relation":"relation:or","operation":"operation:or",`)
@@ -281,10 +285,9 @@
}
// term writes a term to the string builder
-func term(strBuilder *strings.Builder, term Term, match bool) {
-
+func term(strBuilder *strings.Builder, term Term, positive bool) {
strBuilder.WriteString(`{"@type":"koral:term","match":"match:`)
- if match {
+ if positive {
strBuilder.WriteString("eq")
} else {
strBuilder.WriteString("ne")
@@ -306,9 +309,11 @@
// if a termGroup has only a single term, remove the group
}
+// replaceWrappedTerm replaces the wrapped term with the new term group
func replaceWrappedTerms(jsonString string, terms []Term) string {
var err error
+ // Replace with a single term
if len(terms) == 1 {
jsonString, err = sjson.Set(jsonString, "foundry", terms[0].Foundry)
if err != nil {
@@ -340,9 +345,10 @@
var strBuilder strings.Builder
if matchop == "match:ne" {
- termGroup(&strBuilder, terms, false)
+ // ! Make or-Group with nes
+ termGroup(&strBuilder, terms, false, false)
} else {
- termGroup(&strBuilder, terms, true)
+ termGroup(&strBuilder, terms, true, true)
}
return strBuilder.String()
@@ -378,6 +384,106 @@
return jsonString
}
+func replaceGroupedTerms(jsonString string, op []int, terms []Term) string {
+ var err error
+
+ positive := true
+ operationAnd := true
+
+ operation := gjson.Get(jsonString, "operation")
+ if operation.String() == "operation:or" {
+ operationAnd = false
+ }
+
+ // TODO:
+ // matchop := gjson.Get(jsonString, strInt).String()
+
+ if len(op) == 1 {
+ strInt := "operands." + strconv.Itoa(op[0]) + ".match"
+
+ matchop := gjson.Get(jsonString, strInt).String()
+
+ if matchop == "match:ne" {
+ positive = false
+ }
+
+ // Delete the first term
+ jsonString, err = sjson.Delete(jsonString, strInt)
+
+ if err != nil {
+ log.Error().Err(err).Msg("Error deleting match")
+ }
+ }
+
+ for i := 0; i < len(op); i++ {
+ jsonString, err = sjson.Delete(jsonString, "operands."+strconv.Itoa(op[i]))
+
+ if err != nil {
+ log.Error().Err(err).Msg("Error deleting operand")
+ }
+ }
+
+ // TODO:
+ // Check if the group has only a single operand!
+
+ // TODO:
+ // All terms in the group require the same match!
+ // It's not possible to deal with !a & b
+ /*
+ jsonString, err = sjson.Set(jsonString, strInt+"foundry", foundry)
+ if err != nil {
+ log.Error().Err(err).Msg("Error setting foundry")
+ }
+ jsonString, err = sjson.Set(jsonString, strInt+"layer", layer)
+ if err != nil {
+ log.Error().Err(err).Msg("Error setting layer")
+ }
+ jsonString, err = sjson.Set(jsonString, strInt+"key", key)
+ if err != nil {
+ log.Error().Err(err).Msg("Error setting key")
+ }
+
+ if len(op) > 1 {
+ for i := 1; i < len(op); i++ {
+ jsonString, err = sjson.Delete(jsonString, "operands."+strconv.Itoa(op[i]))
+ if err != nil {
+ log.Error().Err(err).Msg("Error deleting operand")
+ }
+ }
+ }
+ */
+
+ var strBuilder strings.Builder
+ // Embed a new termGroup
+ if !operationAnd {
+ termGroup(&strBuilder, terms, true, false)
+ jsonString, err = sjson.SetRaw(jsonString, "operands.-1", strBuilder.String())
+ if err != nil {
+ log.Error().Err(err).Msg("Error adding termGroup")
+ }
+ strBuilder.Reset()
+ } else if !positive {
+ termGroup(&strBuilder, terms, false, false)
+ jsonString, err = sjson.SetRaw(jsonString, "operands.-1", strBuilder.String())
+ if err != nil {
+ log.Error().Err(err).Msg("Error adding termGroup")
+ }
+ strBuilder.Reset()
+ } else {
+ for i := 0; i < len(terms); i++ {
+ term(&strBuilder, terms[i], positive)
+ jsonString, err = sjson.SetRaw(jsonString, "operands.-1", strBuilder.String())
+
+ if err != nil {
+ log.Error().Err(err).Msg("Error adding term")
+ }
+ strBuilder.Reset()
+ }
+ }
+
+ return jsonString
+}
+
/*
func replaceTermWithToken(jsonString string) string {
// Replace the term with the token
diff --git a/termmapper_test.go b/termmapper_test.go
index 259e08b..870e45e 100644
--- a/termmapper_test.go
+++ b/termmapper_test.go
@@ -144,9 +144,56 @@
assert.Equal(testStr, "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:or\",\"operation\":\"operation:or\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry1\",\"layer\":\"mylayer1\",\"key\":\"mykey1\"},{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}]}")
// case6: 1 -> n the term is an operand in a termGroup with the same relation/operation
- // [PPOSAT] -> [PRON & Poss=yes & PronType=Prs]
+ // [PPOSAT & ...] -> [PRON & Poss=yes & PronType=Prs]
+ testStr = replaceGroupedTerms(
+ "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey1\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}]}",
+ []int{0},
+ []Term{{
+ "myfoundry3",
+ "mylayer3",
+ "mykey3",
+ }, {
+ "myfoundry4",
+ "mylayer4",
+ "mykey4",
+ }},
+ )
+ assert.Equal(testStr, "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry3\",\"layer\":\"mylayer3\",\"key\":\"mykey3\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry4\",\"layer\":\"mylayer4\",\"key\":\"mykey4\"}]}")
// case7: 1 -> n the term is an operand in a termGroup with a different relation/operation
+ testStr = replaceGroupedTerms(
+
+ "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":["+
+ "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey1\"},"+
+ "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}"+
+ "]}",
+ []int{0},
+ []Term{{
+ "myfoundry3",
+ "mylayer3",
+ "mykey3",
+ }, {
+ "myfoundry4",
+ "mylayer4",
+ "mykey4",
+ }},
+ )
+
+ // TODO: Add a termGroup with reversed signs
+ assert.Equal(testStr,
+
+ "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":["+
+
+ "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"},"+
+
+ "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:or\",\"operation\":\"operation:or\",\"operands\":["+
+
+ "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry3\",\"layer\":\"mylayer3\",\"key\":\"mykey3\"},"+
+ "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry4\",\"layer\":\"mylayer4\",\"key\":\"mykey4\"}"+
+ "]}"+
+
+ "]"+
+ "}")
// case8: n -> n the term is an operand in a termGroup with the same relation/operation
// case9: n -> n the term is an operand in a termGroup with a different relation/operation