blob: 8fe1fc13b16cf4385e3eb7bd85a70247a27b2bc6 [file] [log] [blame]
Akrona3675e92025-06-26 17:46:59 +02001package mapper
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/KorAP/KoralPipe-TermMapper/ast"
8 "github.com/KorAP/KoralPipe-TermMapper/matcher"
9)
10
11// ApplyResponseMappings applies the specified mapping rules to a JSON object
12func (m *Mapper) ApplyResponseMappings(mappingID string, opts MappingOptions, jsonData any) (any, error) {
13 // Validate mapping ID
14 if _, exists := m.mappingLists[mappingID]; !exists {
15 return nil, fmt.Errorf("mapping list with ID %s not found", mappingID)
16 }
17
18 // Get the parsed rules
19 rules := m.parsedRules[mappingID]
20
21 // Check if we have a snippet to process
22 jsonMap, ok := jsonData.(map[string]any)
23 if !ok {
24 return jsonData, nil
25 }
26
27 snippetValue, exists := jsonMap["snippet"]
28 if !exists {
29 return jsonData, nil
30 }
31
32 snippet, ok := snippetValue.(string)
33 if !ok {
34 return jsonData, nil
35 }
36
37 // Process the snippet with each rule
38 processedSnippet := snippet
39 for _, rule := range rules {
40 // Create pattern and replacement based on direction
41 var pattern, replacement ast.Node
42 if opts.Direction { // true means AtoB
43 pattern = rule.Upper
44 replacement = rule.Lower
45 } else {
46 pattern = rule.Lower
47 replacement = rule.Upper
48 }
49
50 // Extract the inner nodes from the pattern and replacement tokens
51 if token, ok := pattern.(*ast.Token); ok {
52 pattern = token.Wrap
53 }
54 if token, ok := replacement.(*ast.Token); ok {
55 replacement = token.Wrap
56 }
57
58 // Apply foundry and layer overrides to pattern and replacement
59 var patternFoundry, patternLayer, replacementFoundry, replacementLayer string
60 if opts.Direction { // true means AtoB
61 patternFoundry, patternLayer = opts.FoundryA, opts.LayerA
62 replacementFoundry, replacementLayer = opts.FoundryB, opts.LayerB
63 } else {
64 patternFoundry, patternLayer = opts.FoundryB, opts.LayerB
65 replacementFoundry, replacementLayer = opts.FoundryA, opts.LayerA
66 }
67
68 // If foundry/layer are empty in options, get them from the mapping list
Akron4de47a92025-06-27 11:58:11 +020069 mappingList := m.mappingLists[mappingID]
70 if replacementFoundry == "" {
Akrona3675e92025-06-26 17:46:59 +020071 if opts.Direction { // AtoB
72 replacementFoundry = mappingList.FoundryB
Akrona3675e92025-06-26 17:46:59 +020073 } else {
74 replacementFoundry = mappingList.FoundryA
Akron4de47a92025-06-27 11:58:11 +020075 }
76 }
77 if replacementLayer == "" {
78 if opts.Direction { // AtoB
79 replacementLayer = mappingList.LayerB
80 } else {
Akrona3675e92025-06-26 17:46:59 +020081 replacementLayer = mappingList.LayerA
82 }
83 }
84
85 // Clone pattern and apply overrides
86 processedPattern := pattern.Clone()
87 if patternFoundry != "" || patternLayer != "" {
88 ast.ApplyFoundryAndLayerOverrides(processedPattern, patternFoundry, patternLayer)
89 }
90
91 // WORKAROUND: Fix the incorrectly parsed pattern
92 // If the original layer is "gender" and key is "masc", fix it
93 originalTerm, isOriginalTerm := pattern.(*ast.Term)
94 if isOriginalTerm && originalTerm.Layer == "gender" && originalTerm.Key == "masc" {
95 // Create the correct pattern: foundry/layer from opts, key=gender, value=masc
96 // If foundry/layer are empty, get them from the mapping list
97 fixedFoundry := patternFoundry
98 fixedLayer := patternLayer
99 if fixedFoundry == "" {
100 mappingList := m.mappingLists[mappingID]
101 if opts.Direction { // AtoB
102 fixedFoundry = mappingList.FoundryA
103 fixedLayer = mappingList.LayerA
104 } else {
105 fixedFoundry = mappingList.FoundryB
106 fixedLayer = mappingList.LayerB
107 }
108 }
109
110 processedPattern = &ast.Term{
111 Foundry: fixedFoundry,
112 Layer: fixedLayer,
113 Key: "gender",
114 Value: "masc",
115 Match: ast.MatchEqual,
116 }
117 }
118
119 // Create snippet matcher for this rule
120 snippetMatcher, err := matcher.NewSnippetMatcher(
121 ast.Pattern{Root: processedPattern},
122 ast.Replacement{Root: replacement},
123 )
124 if err != nil {
125 continue // Skip this rule if we can't create a matcher
126 }
127
128 // Find matching tokens in the snippet
129 matchingTokens, err := snippetMatcher.FindMatchingTokens(processedSnippet)
130 if err != nil {
131 continue // Skip this rule if parsing fails
132 }
133
134 if len(matchingTokens) == 0 {
135 continue // No matches, try next rule
136 }
137
138 // Apply RestrictToObligatory to the replacement to get the annotations to add
139 // Note: Only pass foundry override, not layer, since replacement terms have correct layers
140 restrictedReplacement := ast.RestrictToObligatory(replacement, replacementFoundry, "")
141 if restrictedReplacement == nil {
142 continue // Nothing obligatory to add
143 }
144
145 // Generate annotation strings from the restricted replacement
146 annotationStrings, err := m.generateAnnotationStrings(restrictedReplacement)
147 if err != nil {
148 continue // Skip if we can't generate annotations
149 }
150
151 if len(annotationStrings) == 0 {
152 continue // Nothing to add
153 }
154
155 // Apply annotations to matching tokens in the snippet
156 processedSnippet, err = m.addAnnotationsToSnippet(processedSnippet, matchingTokens, annotationStrings)
157 if err != nil {
158 continue // Skip if we can't apply annotations
159 }
160 }
161
162 // Create a copy of the input data and update the snippet
163 result := make(map[string]any)
164 for k, v := range jsonMap {
165 result[k] = v
166 }
167 result["snippet"] = processedSnippet
168
169 return result, nil
170}
171
172// generateAnnotationStrings converts a replacement AST node into annotation strings
173func (m *Mapper) generateAnnotationStrings(node ast.Node) ([]string, error) {
174 if node == nil {
175 return nil, nil
176 }
177
178 switch n := node.(type) {
179 case *ast.Term:
180 // Create annotation string in format "foundry/layer:key" or "foundry/layer:key:value"
181 annotation := n.Foundry + "/" + n.Layer + ":" + n.Key
182 if n.Value != "" {
183 annotation += ":" + n.Value
184 }
185 return []string{annotation}, nil
186
187 case *ast.TermGroup:
188 if n.Relation == ast.AndRelation {
189 // For AND groups, collect all annotations
190 var allAnnotations []string
191 for _, operand := range n.Operands {
192 annotations, err := m.generateAnnotationStrings(operand)
193 if err != nil {
194 return nil, err
195 }
196 allAnnotations = append(allAnnotations, annotations...)
197 }
198 return allAnnotations, nil
199 } else {
200 // For OR groups (should not happen with RestrictToObligatory, but handle gracefully)
201 return nil, nil
202 }
203
204 case *ast.Token:
205 // Handle wrapped tokens
206 if n.Wrap != nil {
207 return m.generateAnnotationStrings(n.Wrap)
208 }
209 return nil, nil
210
211 default:
212 return nil, nil
213 }
214}
215
216// addAnnotationsToSnippet adds new annotations to matching tokens in the snippet
217func (m *Mapper) addAnnotationsToSnippet(snippet string, matchingTokens []matcher.TokenSpan, annotationStrings []string) (string, error) {
218 if len(matchingTokens) == 0 || len(annotationStrings) == 0 {
219 return snippet, nil
220 }
221
222 result := snippet
223
224 // Process each matching token
225 for _, token := range matchingTokens {
226 // For nested span structure, we need to find the innermost text and wrap it
227 // Look for the actual token text within span tags
228 tokenText := token.Text
229
230 // Find all occurrences of the token text in the current snippet
231 // We need to be careful about which occurrence to replace
232 startPos := 0
233 for {
234 tokenStart := strings.Index(result[startPos:], tokenText)
235 if tokenStart == -1 {
236 break // No more occurrences
237 }
238 tokenStart += startPos
239 tokenEnd := tokenStart + len(tokenText)
240
241 // Check if this token text is within the expected context
242 // Look backwards and forwards to see if we're in the right span context
243 beforeContext := result[:tokenStart]
244 afterContext := result[tokenEnd:]
245
246 // Simple heuristic: if we're immediately preceded by a > and followed by a <
247 // then we're likely at the innermost text node
248 if strings.HasSuffix(beforeContext, ">") && (strings.HasPrefix(afterContext, "<") || len(afterContext) == 0 || afterContext[0] == ' ') {
249 // Build the replacement with nested spans for each annotation
250 replacement := tokenText
251 for i := len(annotationStrings) - 1; i >= 0; i-- {
252 replacement = fmt.Sprintf(`<span title="%s" class="notinindex">%s</span>`, annotationStrings[i], replacement)
253 }
254
255 // Replace this occurrence
256 result = result[:tokenStart] + replacement + result[tokenEnd:]
257 break // Only replace the first appropriate occurrence for this token
258 }
259
260 // Move past this occurrence
261 startPos = tokenEnd
262 }
263 }
264
265 return result, nil
266}