File size: 9,859 Bytes
46df5f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"""
Terminology consistency checker.

Validates:
- Consistent spelling of the same term
- Consistent hyphenation
- Consistent capitalization of technical terms
"""
import re
from typing import List, Dict, Set
from collections import defaultdict

from .base import BaseChecker, CheckResult, CheckSeverity


class ConsistencyChecker(BaseChecker):
    """Check terminology and spelling consistency."""
    
    name = "consistency"
    display_name = "Consistency"
    description = "Check for inconsistent terminology and spelling"
    
    # Known variant pairs (canonical -> variants)
    KNOWN_VARIANTS = {
        # Hyphenation variants
        'self-supervised': ['self supervised', 'selfsupervised'],
        'pre-trained': ['pre trained', 'pretrained'],
        'fine-tuned': ['fine tuned', 'finetuned'],
        'state-of-the-art': ['state of the art', 'stateoftheart'],
        'real-world': ['real world', 'realworld'],
        'end-to-end': ['end to end', 'endtoend', 'e2e'],
        'large-scale': ['large scale', 'largescale'],
        'long-term': ['long term', 'longterm'],
        'short-term': ['short term', 'shortterm'],
        'multi-task': ['multi task', 'multitask'],
        'multi-modal': ['multi modal', 'multimodal'],
        'cross-lingual': ['cross lingual', 'crosslingual'],
        'zero-shot': ['zero shot', 'zeroshot'],
        'few-shot': ['few shot', 'fewshot'],
        'in-context': ['in context', 'incontext'],
        
        # American vs British English (comprehensive list)
        # -or/-our endings
        'color': ['colour'],
        'behavior': ['behaviour'],
        'favor': ['favour'],
        'honor': ['honour'],
        'labor': ['labour'],
        'neighbor': ['neighbour'],
        'rumor': ['rumour'],
        'vapor': ['vapour'],
        
        # -ize/-ise endings
        'analyze': ['analyse'],
        'characterize': ['characterise'],
        'generalize': ['generalise'],
        'initialize': ['initialise'],
        'maximize': ['maximise'],
        'minimize': ['minimise'],
        'normalize': ['normalise'],
        'optimize': ['optimise'],
        'organize': ['organise'],
        'realize': ['realise'],
        'recognize': ['recognise'],
        'specialize': ['specialise'],
        'standardize': ['standardise'],
        'summarize': ['summarise'],
        'utilize': ['utilise'],
        'visualize': ['visualise'],
        'categorize': ['categorise'],
        'emphasize': ['emphasise'],
        'hypothesize': ['hypothesise'],
        'prioritize': ['prioritise'],
        'synchronize': ['synchronise'],
        
        # -ization/-isation endings
        'generalization': ['generalisation'],
        'initialization': ['initialisation'],
        'maximization': ['maximisation'],
        'minimization': ['minimisation'],
        'normalization': ['normalisation'],
        'optimization': ['optimisation'],
        'organization': ['organisation'],
        'realization': ['realisation'],
        'regularization': ['regularisation'],
        'specialization': ['specialisation'],
        'standardization': ['standardisation'],
        'summarization': ['summarisation'],
        'utilization': ['utilisation'],
        'visualization': ['visualisation'],
        'categorization': ['categorisation'],
        'characterization': ['characterisation'],
        'parametrization': ['parametrisation'],
        'quantization': ['quantisation'],
        
        # -er/-re endings
        'center': ['centre'],
        'fiber': ['fibre'],
        'meter': ['metre'],
        'liter': ['litre'],
        
        # -l-/-ll- (American single, British double)
        'modeling': ['modelling'],
        'labeled': ['labelled'],
        'labeling': ['labelling'],
        'traveled': ['travelled'],
        'traveling': ['travelling'],
        'canceled': ['cancelled'],
        'canceling': ['cancelling'],
        'signaled': ['signalled'],
        'signaling': ['signalling'],
        
        # -og/-ogue endings
        'analog': ['analogue'],
        'catalog': ['catalogue'],
        'dialog': ['dialogue'],
        
        # -ense/-ence endings
        'defense': ['defence'],
        'license': ['licence'],
        'offense': ['offence'],
        
        # Other common differences
        'gray': ['grey'],
        'artifact': ['artefact'],
        'program': ['programme'],  # Note: 'program' is standard in computing
        'skeptical': ['sceptical'],
        'aluminum': ['aluminium'],
        
        # Verb forms
        'learned': ['learnt'],
        'burned': ['burnt'],
        'spelled': ['spelt'],
        
        # Common term variants
        'dataset': ['data set', 'data-set'],
        'benchmark': ['bench mark', 'bench-mark'],
        'baseline': ['base line', 'base-line'],
        'downstream': ['down stream', 'down-stream'],
        'upstream': ['up stream', 'up-stream'],
        'encoder': ['en-coder'],
        'decoder': ['de-coder'],
    }
    
    # Capitalization variants to track
    CAPITALIZATION_TERMS = [
        'transformer', 'attention', 'bert', 'gpt', 'lstm', 'cnn', 'rnn',
        'encoder', 'decoder', 'embedding', 'softmax', 'sigmoid', 'relu',
    ]
    
    def check(self, tex_content: str, config: dict = None) -> List[CheckResult]:
        results = []
        
        # Remove comments
        content = re.sub(r'(?<!\\)%.*$', '', tex_content, flags=re.MULTILINE)
        content_lower = content.lower()
        
        # Check for known variant inconsistencies
        for canonical, variants in self.KNOWN_VARIANTS.items():
            found_forms = []
            
            # Check canonical form
            if re.search(rf'\b{re.escape(canonical)}\b', content, re.IGNORECASE):
                found_forms.append(canonical)
            
            # Check variants
            for variant in variants:
                if re.search(rf'\b{re.escape(variant)}\b', content, re.IGNORECASE):
                    found_forms.append(variant)
            
            if len(found_forms) > 1:
                results.append(self._create_result(
                    passed=False,
                    severity=CheckSeverity.WARNING,
                    message=f"Inconsistent spelling: {', '.join(found_forms)}",
                    suggestion=f"Use '{canonical}' consistently throughout"
                ))
        
        # Check hyphenated word consistency
        hyphen_results = self._check_hyphenation_consistency(content)
        results.extend(hyphen_results)
        
        # Check capitalization consistency
        cap_results = self._check_capitalization_consistency(content)
        results.extend(cap_results)
        
        return results
    
    def _check_hyphenation_consistency(self, content: str) -> List[CheckResult]:
        """Find words that appear both hyphenated and non-hyphenated."""
        results = []
        
        # Common terms that should always be hyphenated (exceptions)
        ALWAYS_HYPHENATED = {
            'state-of-the-art', 'end-to-end', 'real-time', 'real-world',
            'fine-tuning', 'fine-grained', 'large-scale', 'small-scale',
            'multi-task', 'multi-modal', 'cross-domain', 'cross-lingual',
            'self-supervised', 'self-attention', 'co-training', 'pre-training',
            'post-processing', 'pre-processing', 'well-known', 'well-defined',
            'high-quality', 'low-quality', 'long-term', 'short-term'
        }
        
        # Find all hyphenated words
        hyphenated = set(re.findall(r'\b([a-z]+-[a-z]+(?:-[a-z]+)*)\b', content, re.IGNORECASE))
        
        for hyph_word in hyphenated:
            # Skip if it's a known compound that should always be hyphenated
            if hyph_word.lower() in ALWAYS_HYPHENATED:
                continue
            
            # Create non-hyphenated version
            non_hyph = hyph_word.replace('-', ' ')
            combined = hyph_word.replace('-', '')
            
            # Check if non-hyphenated version exists
            if re.search(rf'\b{re.escape(non_hyph)}\b', content, re.IGNORECASE):
                results.append(self._create_result(
                    passed=False,
                    severity=CheckSeverity.INFO,
                    message=f"Inconsistent hyphenation: '{hyph_word}' vs '{non_hyph}'",
                    suggestion="Choose one form and use it consistently"
                ))
            elif re.search(rf'\b{re.escape(combined)}\b', content, re.IGNORECASE):
                results.append(self._create_result(
                    passed=False,
                    severity=CheckSeverity.INFO,
                    message=f"Inconsistent hyphenation: '{hyph_word}' vs '{combined}'",
                    suggestion="Choose one form and use it consistently"
                ))
        
        return results
    
    def _check_capitalization_consistency(self, content: str) -> List[CheckResult]:
        """Check if technical terms have consistent capitalization."""
        results = []
        
        for term in self.CAPITALIZATION_TERMS:
            # Find all case variations
            pattern = re.compile(rf'\b{term}\b', re.IGNORECASE)
            matches = pattern.findall(content)
            
            if len(matches) > 1:
                # Check if there are mixed capitalizations
                unique_forms = set(matches)
                if len(unique_forms) > 1:
                    forms_str = ', '.join(f"'{f}'" for f in unique_forms)
                    results.append(self._create_result(
                        passed=False,
                        severity=CheckSeverity.INFO,
                        message=f"Inconsistent capitalization: {forms_str}",
                        suggestion="Use consistent capitalization for technical terms"
                    ))
        
        return results