blob: 679ac254bd4e5d7a257f592e7f2e0c3d8952f278 [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
10func TestConstants(t *testing.T) {
11 assert := assert.New(t)
12 assert.Equal(Hui(), "test")
13}
14
15// KoralPipe-TermMapping
16
17func TestTokenBuilder(t *testing.T) {
18
19 assert := assert.New(t)
20
21 var strBuilder strings.Builder
22 term(&strBuilder, "myfoundry", "mylayer", "mykey1", true)
23 assert.Equal(strBuilder.String(), `{"@type":"koral:term","match":"match:eq","foundry":"myfoundry","layer":"mylayer","key":"mykey1"}`)
24 strBuilder.Reset()
25
26 token(&strBuilder, "myfoundry", "mylayer", []string{"mykey1", "mykey2"})
27 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\"}]}}")
28 strBuilder.Reset()
29
30 token(&strBuilder, "myfoundry", "mylayer", []string{"mykey2"})
31 assert.Equal(strBuilder.String(), "{\"@type\":\"koral:token\",\"wrap\":{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}}")
32 strBuilder.Reset()
33}
34
35/*
36 jsonStr := `{
37 "query": {
38 "@type": "koral:operation",
39 "operators": [
40 {
41 "@type": "koral:term",
42 "key": "example1"
43 },
44 {
45 "@type": "koral:term",
46 "key": "example2"
47 },
48 {
49 "@type": "koral:operation",
50 "operators": [
51 {
52 "@type": "koral:term",
53 "key": "nested"
54 }
55 ]
56 }
57 ]
58 }
59 }`
60*/
61
62func TestTermReplacement(t *testing.T) {
63
64 assert := assert.New(t)
65
Akron08aa2632025-04-07 17:38:28 +020066 // case1: 1 -> 1 the term is wrapped with eq
67 // case1: 1 -> 1 the term is wrapped with ne
68 // [ADV] -> [ADV]
69 testStr := replaceWrappedTerms(
70 "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}",
71 []Term{{"myfoundry2",
72 "mylayer2",
73 "mykey2",
74 }},
75 )
Akronfc3bd272025-04-04 16:15:44 +020076 assert.Equal(testStr, "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}")
77
Akron08aa2632025-04-07 17:38:28 +020078 testStr = replaceWrappedTerms(
79 "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}",
80 []Term{{"myfoundry2",
81 "mylayer2",
82 "mykey2",
83 }},
84 )
85 assert.Equal(testStr, "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}")
86
Akronfc3bd272025-04-04 16:15:44 +020087 // case2: 1 -> 1 the term is an operand in a termGroup with the same relation/operation
Akron08aa2632025-04-07 17:38:28 +020088 // [ADV & ...] -> [ADV]
Akronfc3bd272025-04-04 16:15:44 +020089 // case3: 1 -> 1 the term is an operand in a termGroup with a different relation/operation
Akron08aa2632025-04-07 17:38:28 +020090 // [ADV | ...] -> [ADV]
Akronfc3bd272025-04-04 16:15:44 +020091 testStr = replaceGroupedTerm(
92 "{\"@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\"}]}",
93 []int{0},
94 "myfoundryX",
95 "mylayerX",
96 "mykeyX",
97 )
98 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\"}]}")
99
100 testStr = replaceGroupedTerm(
101 "{\"@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\"}]}",
102 []int{0},
103 "myfoundryX",
104 "mylayerX",
105 "mykeyX",
106 )
107 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\"}]}")
108
109 // case4: n -> 1 the term is an operand in a termGroup with the same relation/operation
Akron08aa2632025-04-07 17:38:28 +0200110 // [PRON & Poss=yes & PronType=Prs] -> [PPOSAT]
Akronfc3bd272025-04-04 16:15:44 +0200111 testStr = replaceGroupedTerm(
112 "{\"@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\"}]}",
113 []int{0, 1},
114 "myfoundryX",
115 "mylayerX",
116 "mykeyX",
117 )
118 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\"}]}")
119
Akron08aa2632025-04-07 17:38:28 +0200120 // case5: 1 -> n the term is wrapped
121 // [PPOSAT] -> [PRON & Poss=yes & PronType=Prs]
122 testStr = replaceWrappedTerms(
123 "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}",
124 []Term{{
125 "myfoundry1",
126 "mylayer1",
127 "mykey1",
128 }, {
129 "myfoundry2",
130 "mylayer2",
131 "mykey2",
132 }},
133 )
134 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\"}]}")
135
136 // [!PPOSAT] -> [!PRON | !Poss=yes | !PronType=Prs]
137 testStr = replaceWrappedTerms(
138 "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}",
139 []Term{{
140 "myfoundry1",
141 "mylayer1",
142 "mykey1",
143 }, {
144 "myfoundry2",
145 "mylayer2",
146 "mykey2",
147 }},
148 )
149 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\"}]}")
150
151 // case6: 1 -> n the term is an operand in a termGroup with the same relation/operation
152 // [PPOSAT] -> [PRON & Poss=yes & PronType=Prs]
153
154 // case7: 1 -> n the term is an operand in a termGroup with a different relation/operation
155 // case8: n -> n the term is an operand in a termGroup with the same relation/operation
156 // case9: n -> n the term is an operand in a termGroup with a different relation/operation
157
Akronfc3bd272025-04-04 16:15:44 +0200158}