Introduce term struct
diff --git a/termmapper.go b/termmapper.go
index b2d41fe..5ddb6b8 100644
--- a/termmapper.go
+++ b/termmapper.go
@@ -194,6 +194,12 @@
*/
+type Term struct {
+ Foundry string
+ Layer string
+ Key string
+}
+
func Hui() string {
return "test"
}
@@ -270,6 +276,26 @@
strBuilder.WriteString(`]}`)
}
+// termGroup2 writes a termGroup to the string builder
+func termGroup2(strBuilder *strings.Builder, terms []Term, positive bool) {
+ strBuilder.WriteString(`{"@type":"koral:termGroup",`)
+
+ if positive {
+ strBuilder.WriteString(`"relation":"relation:and","operation":"operation:and",`)
+ } else {
+ strBuilder.WriteString(`"relation":"relation:or","operation":"operation:or",`)
+ }
+
+ strBuilder.WriteString(`"operands":[`)
+ for i, term := range terms {
+ term2(strBuilder, term, positive)
+ if i < len(terms)-1 {
+ strBuilder.WriteString(",")
+ }
+ }
+ strBuilder.WriteString(`]}`)
+}
+
// term writes a term to the string builder
func term(strBuilder *strings.Builder, foundry string, layer string, key string, match bool) {
@@ -289,6 +315,25 @@
strBuilder.WriteString(`"}`)
}
+// term writes a term to the string builder
+func term2(strBuilder *strings.Builder, term Term, match bool) {
+
+ // TODO: May have ne!!!!
+ strBuilder.WriteString(`{"@type":"koral:term","match":"match:`)
+ if match {
+ strBuilder.WriteString("eq")
+ } else {
+ strBuilder.WriteString("ne")
+ }
+ strBuilder.WriteString(`","foundry":"`)
+ strBuilder.WriteString(term.Foundry)
+ strBuilder.WriteString(`","layer":"`)
+ strBuilder.WriteString(term.Layer)
+ strBuilder.WriteString(`","key":"`)
+ strBuilder.WriteString(term.Key)
+ strBuilder.WriteString(`"}`)
+}
+
func flatten() {
// if a termGroup isan operand in a termGroup with the same relation/operation:
@@ -297,21 +342,47 @@
// if a termGroup has only a single term, remove the group
}
-func replaceWrappedTerm(jsonString string, foundry string, layer string, key string) string {
+func replaceWrappedTerms(jsonString string, terms []Term) string {
var err error
- jsonString, err = sjson.Set(jsonString, "foundry", foundry)
- if err != nil {
- log.Error().Err(err).Msg("Error setting foundry")
+
+ if len(terms) == 1 {
+ jsonString, err = sjson.Set(jsonString, "foundry", terms[0].Foundry)
+ if err != nil {
+ log.Error().Err(err).Msg("Error setting foundry")
+ }
+ jsonString, err = sjson.Set(jsonString, "layer", terms[0].Layer)
+ if err != nil {
+ log.Error().Err(err).Msg("Error setting layer")
+ }
+ jsonString, err = sjson.Set(jsonString, "key", terms[0].Key)
+ if err != nil {
+ log.Error().Err(err).Msg("Error setting key")
+ }
+
+ return jsonString
}
- jsonString, err = sjson.Set(jsonString, "layer", layer)
- if err != nil {
- log.Error().Err(err).Msg("Error setting layer")
+
+ matchop := gjson.Get(jsonString, "match").String()
+
+ /*
+ foundry := gjson.Get(jsonString, "foundry").String()
+ layer := gjson.Get(jsonString, "layer").String()
+ key := gjson.Get(jsonString, "key").String()
+ term := Term{foundry, layer, key}
+
+
+ terms = append(terms, term)
+ */
+
+ var strBuilder strings.Builder
+ if matchop == "match:ne" {
+ termGroup2(&strBuilder, terms, false)
+ } else {
+ termGroup2(&strBuilder, terms, true)
}
- jsonString, err = sjson.Set(jsonString, "key", key)
- if err != nil {
- log.Error().Err(err).Msg("Error setting key")
- }
- return jsonString
+
+ return strBuilder.String()
+
}
func replaceGroupedTerm(jsonString string, op []int, foundry string, layer string, key string) string {
diff --git a/termmapper_test.go b/termmapper_test.go
index 0492c35..679ac25 100644
--- a/termmapper_test.go
+++ b/termmapper_test.go
@@ -63,12 +63,31 @@
assert := assert.New(t)
- // case1: 1 -> 1 the term is wrapped
- testStr := replaceWrappedTerm("{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}", "myfoundry2", "mylayer2", "mykey2")
+ // case1: 1 -> 1 the term is wrapped with eq
+ // case1: 1 -> 1 the term is wrapped with ne
+ // [ADV] -> [ADV]
+ testStr := replaceWrappedTerms(
+ "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}",
+ []Term{{"myfoundry2",
+ "mylayer2",
+ "mykey2",
+ }},
+ )
assert.Equal(testStr, "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}")
+ testStr = replaceWrappedTerms(
+ "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}",
+ []Term{{"myfoundry2",
+ "mylayer2",
+ "mykey2",
+ }},
+ )
+ assert.Equal(testStr, "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}")
+
// case2: 1 -> 1 the term is an operand in a termGroup with the same relation/operation
+ // [ADV & ...] -> [ADV]
// case3: 1 -> 1 the term is an operand in a termGroup with a different relation/operation
+ // [ADV | ...] -> [ADV]
testStr = replaceGroupedTerm(
"{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}]}",
[]int{0},
@@ -88,6 +107,7 @@
assert.Equal(testStr, "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundryX\",\"layer\":\"mylayerX\",\"key\":\"mykeyX\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}]}")
// case4: n -> 1 the term is an operand in a termGroup with the same relation/operation
+ // [PRON & Poss=yes & PronType=Prs] -> [PPOSAT]
testStr = replaceGroupedTerm(
"{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}]}",
[]int{0, 1},
@@ -97,10 +117,42 @@
)
assert.Equal(testStr, "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundryX\",\"layer\":\"mylayerX\",\"key\":\"mykeyX\"}]}")
- // case5: n -> 1 the term is an operand in a termGroup with a different relation/operation
- // case6: 1 -> n the term is wrapped
- // case7: 1 -> n the term is an operand in a termGroup with the same relation/operation
- // case8: 1 -> n the term is an operand in a termGroup with a different relation/operation
- // case9: n -> n the term is an operand in a termGroup with the same relation/operation
- // case10: n -> n the term is an operand in a termGroup with a different relation/operation
+ // case5: 1 -> n the term is wrapped
+ // [PPOSAT] -> [PRON & Poss=yes & PronType=Prs]
+ testStr = replaceWrappedTerms(
+ "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}",
+ []Term{{
+ "myfoundry1",
+ "mylayer1",
+ "mykey1",
+ }, {
+ "myfoundry2",
+ "mylayer2",
+ "mykey2",
+ }},
+ )
+ assert.Equal(testStr, "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry1\",\"layer\":\"mylayer1\",\"key\":\"mykey1\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}]}")
+
+ // [!PPOSAT] -> [!PRON | !Poss=yes | !PronType=Prs]
+ testStr = replaceWrappedTerms(
+ "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}",
+ []Term{{
+ "myfoundry1",
+ "mylayer1",
+ "mykey1",
+ }, {
+ "myfoundry2",
+ "mylayer2",
+ "mykey2",
+ }},
+ )
+ 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]
+
+ // case7: 1 -> n the term is an operand in a termGroup with a different relation/operation
+ // 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
+
}