File size: 5,155 Bytes
71d49e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re

FEATURE_NAMES = [
    "n_words",
    "n_sents",
    "n_punct",
    "avg_sent_len",
    "n_code_blocks",
    "uppercase_ratio",
    "n_questions",
    "has_code",
    "has_arch",
    "has_math",
    "has_debug",
    "has_research",
    "n_complex_verbs",
    "n_scale_adjs",
    "n_hard_nouns",
    "has_simple",
    "has_realtime",
    "has_long_output",
    "avg_word_len",
]

_WORD_RE = re.compile(r"[A-Za-z0-9_]+")
_SENT_RE = re.compile(r"[^.!?\n]+[.!?]?")
_PUNCT_RE = re.compile(r"[^\w\s]")

CODE_PAT = re.compile(
    r"(```|~~~|`[^`]+`|\b(def |class |import |from .* import|function |const |let |"
    r"var |SELECT |INSERT |UPDATE |DELETE |public static|#include|fn main|=>"
    r"|println!|System\.out))"
)
ARCH_WORDS = {
    "architecture",
    "architect",
    "microservice",
    "microservices",
    "distributed",
    "pipeline",
    "infrastructure",
    "kubernetes",
    "cluster",
    "scalab",
    "deploy",
    "migration",
    "monolith",
    "shard",
    "replica",
    "consensus",
    "raft",
    "paxos",
}
MATH_PAT = re.compile(
    r"[=<>+\-*/^√∫∑∏∂∇≤≥≠≈∞]|\b(theorem|proof|prove|lemma|equation|"
    r"induction|derivative|integral|matrix|eigen|probability|qed)\b"
)
DEBUG_WORDS = {
    "debug",
    "debugging",
    "error",
    "bug",
    "traceback",
    "stacktrace",
    "exception",
    "fix",
    "broken",
    "crash",
    "fail",
    "failing",
    "patch",
    "hotfix",
}
RESEARCH_WORDS = {
    "research",
    "paper",
    "study",
    "survey",
    "literature",
    "benchmark",
    "dataset",
    "baseline",
    "ablation",
    "sota",
    "state-of-the-art",
}
COMPLEX_VERBS = {
    "analyze",
    "analyse",
    "design",
    "implement",
    "optimize",
    "optimise",
    "architect",
    "prove",
    "derive",
    "synthesize",
    "evaluate",
    "compare",
    "migrate",
    "deploy",
    "scale",
    "secure",
    "refactor",
    "integrate",
    "orchestrate",
    "formalize",
    "generalize",
    "debug",
}
SCALE_ADJS = {
    "distributed",
    "large-scale",
    "large scale",
    "massive",
    "enterprise",
    "global",
    "high-throughput",
    "high throughput",
    "fault-tolerant",
    "fault tolerant",
    "real-time",
    "realtime",
    "petabyte",
    "million",
    "billion",
    "end-to-end",
    "end to end",
    "production-grade",
    "production grade",
}
HARD_NOUNS = {
    "kubernetes",
    "microservices",
    "redis",
    "postgres",
    "theorem",
    "proof",
    "induction",
    "quantum",
    "compiler",
    "kernel",
    "cryptography",
    "blockchain",
    "consensus",
    "neural",
    "transformer",
    "gradient",
    "eigen",
    "mlops",
    "terraform",
    "kafka",
    "spark",
    "hadoop",
    "docker",
    "inference",
}
SIMPLE_PAT = re.compile(
    r"\b(simple|simply|easy|basic|basics|hello|hi\b|what is|who is|where is"
    r"|how do i say)\b"
)
REALTIME_PAT = re.compile(r"\b(real-?time|live|streaming|latency|deadline|instant)\b")
LONGOUT_PAT = re.compile(
    r"\b(essay|report|thesis|detailed|comprehensive|in-?depth|"
    r"step-?by-?step|thorough|elaborate|paragraphs|pages?)\b"
)


def _count_phrases(text_low: str, phrases: tuple[str, ...]) -> int:
    return sum(text_low.count(phrase) for phrase in phrases)


def featurize(text: str) -> list[float]:
    source = text if isinstance(text, str) else ""
    low = source.lower()
    words = _WORD_RE.findall(source)
    n_words = len(words)
    sentences = [part for part in _SENT_RE.findall(source) if part.strip()]
    n_sents = max(len(sentences), 1)
    n_punct = len(_PUNCT_RE.findall(source))
    avg_sent_len = n_words / n_sents
    n_code_blocks = len(re.findall(r"```|~~~", source)) // 2 + len(
        re.findall(r"(?m)^(?:    |\t)\S", source)
    )
    alpha = [char for char in source if char.isalpha()]
    uppercase_ratio = (
        (sum(char.isupper() for char in alpha) / len(alpha)) if alpha else 0.0
    )
    n_questions = source.count("?")
    has_code = 1.0 if CODE_PAT.search(source) else 0.0
    has_arch = 1.0 if any(word in low for word in ARCH_WORDS) else 0.0
    has_math = 1.0 if MATH_PAT.search(low) else 0.0
    has_debug = 1.0 if any(word in low for word in DEBUG_WORDS) else 0.0
    has_research = 1.0 if any(word in low for word in RESEARCH_WORDS) else 0.0
    n_complex_verbs = float(_count_phrases(low, COMPLEX_VERBS))
    n_scale_adjs = float(_count_phrases(low, SCALE_ADJS))
    n_hard_nouns = float(_count_phrases(low, HARD_NOUNS))
    has_simple = 1.0 if SIMPLE_PAT.search(low) else 0.0
    has_realtime = 1.0 if REALTIME_PAT.search(low) else 0.0
    has_long_output = 1.0 if LONGOUT_PAT.search(low) else 0.0
    avg_word_len = (sum(len(word) for word in words) / n_words) if n_words else 0.0

    return [
        n_words,
        n_sents,
        n_punct,
        avg_sent_len,
        n_code_blocks,
        uppercase_ratio,
        n_questions,
        has_code,
        has_arch,
        has_math,
        has_debug,
        has_research,
        n_complex_verbs,
        n_scale_adjs,
        n_hard_nouns,
        has_simple,
        has_realtime,
        has_long_output,
        avg_word_len,
    ]