blob: 259e08bea9b4de0ba9e2b058001e1424123f0a76 [file] [log] [blame]
Akronfc3bd272025-04-04 16:15:44 +02001package termmapper
2
3import (
4 "strings"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8)
9
Akronfc3bd272025-04-04 16:15:44 +020010// KoralPipe-TermMapping
11
12func TestTokenBuilder(t *testing.T) {
13
14 assert := assert.New(t)
15
16 var strBuilder strings.Builder
Akronf4614232025-04-08 11:17:53 +020017 term(&strBuilder, Term{"myfoundry", "mylayer", "mykey1"}, true)
Akronfc3bd272025-04-04 16:15:44 +020018 assert.Equal(strBuilder.String(), `{"@type":"koral:term","match":"match:eq","foundry":"myfoundry","layer":"mylayer","key":"mykey1"}`)
19 strBuilder.Reset()
20
Akronf4614232025-04-08 11:17:53 +020021 token(&strBuilder, []Term{{"myfoundry", "mylayer", "mykey1"}, {"myfoundry", "mylayer", "mykey2"}}, true)
Akronfc3bd272025-04-04 16:15:44 +020022 assert.Equal(strBuilder.String(), "{\"@type\":\"koral:token\",\"wrap\":{\"@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\"}]}}")
23 strBuilder.Reset()
24
Akronf4614232025-04-08 11:17:53 +020025 token(&strBuilder, []Term{{"myfoundry", "mylayer", "mykey2"}}, true)
Akronfc3bd272025-04-04 16:15:44 +020026 assert.Equal(strBuilder.String(), "{\"@type\":\"koral:token\",\"wrap\":{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}}")
27 strBuilder.Reset()
28}
29
30/*
31 jsonStr := `{
32 "query": {
33 "@type": "koral:operation",
34 "operators": [
35 {
36 "@type": "koral:term",
37 "key": "example1"
38 },
39 {
40 "@type": "koral:term",
41 "key": "example2"
42 },
43 {
44 "@type": "koral:operation",
45 "operators": [
46 {
47 "@type": "koral:term",
48 "key": "nested"
49 }
50 ]
51 }
52 ]
53 }
54 }`
55*/
56
57func TestTermReplacement(t *testing.T) {
58
59 assert := assert.New(t)
60
Akron08aa2632025-04-07 17:38:28 +020061 // case1: 1 -> 1 the term is wrapped with eq
62 // case1: 1 -> 1 the term is wrapped with ne
63 // [ADV] -> [ADV]
64 testStr := replaceWrappedTerms(
65 "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}",
66 []Term{{"myfoundry2",
67 "mylayer2",
68 "mykey2",
69 }},
70 )
Akronfc3bd272025-04-04 16:15:44 +020071 assert.Equal(testStr, "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}")
72
Akron08aa2632025-04-07 17:38:28 +020073 testStr = replaceWrappedTerms(
74 "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}",
75 []Term{{"myfoundry2",
76 "mylayer2",
77 "mykey2",
78 }},
79 )
80 assert.Equal(testStr, "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}")
81
Akronfc3bd272025-04-04 16:15:44 +020082 // case2: 1 -> 1 the term is an operand in a termGroup with the same relation/operation
Akron08aa2632025-04-07 17:38:28 +020083 // [ADV & ...] -> [ADV]
Akronfc3bd272025-04-04 16:15:44 +020084 // case3: 1 -> 1 the term is an operand in a termGroup with a different relation/operation
Akron08aa2632025-04-07 17:38:28 +020085 // [ADV | ...] -> [ADV]
Akronfc3bd272025-04-04 16:15:44 +020086 testStr = replaceGroupedTerm(
87 "{\"@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\"}]}",
88 []int{0},
89 "myfoundryX",
90 "mylayerX",
91 "mykeyX",
92 )
93 assert.Equal(testStr, "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundryX\",\"layer\":\"mylayerX\",\"key\":\"mykeyX\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}]}")
94
95 testStr = replaceGroupedTerm(
96 "{\"@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\"}]}",
97 []int{0},
98 "myfoundryX",
99 "mylayerX",
100 "mykeyX",
101 )
102 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\"}]}")
103
104 // case4: n -> 1 the term is an operand in a termGroup with the same relation/operation
Akron08aa2632025-04-07 17:38:28 +0200105 // [PRON & Poss=yes & PronType=Prs] -> [PPOSAT]
Akronfc3bd272025-04-04 16:15:44 +0200106 testStr = replaceGroupedTerm(
107 "{\"@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\"}]}",
108 []int{0, 1},
109 "myfoundryX",
110 "mylayerX",
111 "mykeyX",
112 )
113 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\"}]}")
114
Akron08aa2632025-04-07 17:38:28 +0200115 // case5: 1 -> n the term is wrapped
116 // [PPOSAT] -> [PRON & Poss=yes & PronType=Prs]
117 testStr = replaceWrappedTerms(
118 "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}",
119 []Term{{
120 "myfoundry1",
121 "mylayer1",
122 "mykey1",
123 }, {
124 "myfoundry2",
125 "mylayer2",
126 "mykey2",
127 }},
128 )
129 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\"}]}")
130
131 // [!PPOSAT] -> [!PRON | !Poss=yes | !PronType=Prs]
132 testStr = replaceWrappedTerms(
133 "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}",
134 []Term{{
135 "myfoundry1",
136 "mylayer1",
137 "mykey1",
138 }, {
139 "myfoundry2",
140 "mylayer2",
141 "mykey2",
142 }},
143 )
144 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\"}]}")
145
146 // case6: 1 -> n the term is an operand in a termGroup with the same relation/operation
147 // [PPOSAT] -> [PRON & Poss=yes & PronType=Prs]
148
149 // case7: 1 -> n the term is an operand in a termGroup with a different relation/operation
150 // case8: n -> n the term is an operand in a termGroup with the same relation/operation
151 // case9: n -> n the term is an operand in a termGroup with a different relation/operation
152
Akronfc3bd272025-04-04 16:15:44 +0200153}