Skip to main content

_diffctx/config/
weights.rs

1use once_cell::sync::Lazy;
2use rustc_hash::FxHashMap;
3
4#[derive(Debug, Clone, Copy)]
5pub struct EdgeWeightConfig {
6    pub forward: f64,
7    pub reverse_factor: f64,
8}
9
10impl EdgeWeightConfig {
11    pub const fn new(forward: f64, reverse_factor: f64) -> Self {
12        Self {
13            forward,
14            reverse_factor,
15        }
16    }
17
18    pub fn reverse(&self) -> f64 {
19        self.forward * self.reverse_factor
20    }
21}
22
23pub static EDGE_WEIGHTS: Lazy<FxHashMap<&'static str, EdgeWeightConfig>> = Lazy::new(|| {
24    let entries: &[(&str, EdgeWeightConfig)] = &[
25        ("containment", EdgeWeightConfig::new(0.50, 0.70)),
26        ("import", EdgeWeightConfig::new(0.50, 0.70)),
27        ("test_direct", EdgeWeightConfig::new(0.60, 0.50)),
28        ("test_naming", EdgeWeightConfig::new(0.50, 0.50)),
29        ("test_reverse", EdgeWeightConfig::new(0.30, 1.0)),
30        ("config_code", EdgeWeightConfig::new(0.35, 0.50)),
31        ("sibling", EdgeWeightConfig::new(0.05, 1.0)),
32        ("cochange", EdgeWeightConfig::new(0.40, 1.0)),
33        ("doc_structure", EdgeWeightConfig::new(0.30, 0.83)),
34        ("anchor_link", EdgeWeightConfig::new(0.55, 0.64)),
35        ("citation", EdgeWeightConfig::new(0.25, 1.0)),
36        ("python_call", EdgeWeightConfig::new(0.65, 0.70)),
37        ("python_symbol", EdgeWeightConfig::new(0.70, 0.70)),
38        ("python_type", EdgeWeightConfig::new(0.50, 0.70)),
39        ("javascript_call", EdgeWeightConfig::new(0.70, 0.50)),
40        ("javascript_symbol", EdgeWeightConfig::new(0.75, 0.50)),
41        ("javascript_type", EdgeWeightConfig::new(0.65, 0.50)),
42        ("go_import", EdgeWeightConfig::new(0.70, 0.40)),
43        ("go_type", EdgeWeightConfig::new(0.65, 0.40)),
44        ("go_func", EdgeWeightConfig::new(0.60, 0.40)),
45        ("go_same_package", EdgeWeightConfig::new(0.05, 0.40)),
46        ("rust_mod", EdgeWeightConfig::new(0.70, 0.40)),
47        ("rust_use", EdgeWeightConfig::new(0.65, 0.40)),
48        ("rust_type", EdgeWeightConfig::new(0.65, 0.40)),
49        ("rust_fn", EdgeWeightConfig::new(0.60, 0.40)),
50        ("rust_same_crate", EdgeWeightConfig::new(0.05, 0.40)),
51        ("jvm_import", EdgeWeightConfig::new(0.75, 0.40)),
52        ("jvm_inheritance", EdgeWeightConfig::new(0.80, 0.40)),
53        ("jvm_type", EdgeWeightConfig::new(0.60, 0.40)),
54        ("jvm_member", EdgeWeightConfig::new(0.65, 0.40)),
55        ("jvm_same_package", EdgeWeightConfig::new(0.05, 0.40)),
56        ("jvm_annotation", EdgeWeightConfig::new(0.50, 0.40)),
57        ("c_include", EdgeWeightConfig::new(0.65, 0.40)),
58        ("c_call", EdgeWeightConfig::new(0.55, 0.40)),
59        ("c_type", EdgeWeightConfig::new(0.50, 0.40)),
60        ("c_inheritance", EdgeWeightConfig::new(0.70, 0.40)),
61        ("dotnet_using", EdgeWeightConfig::new(0.65, 0.40)),
62        ("dotnet_inheritance", EdgeWeightConfig::new(0.75, 0.40)),
63        ("dotnet_type", EdgeWeightConfig::new(0.60, 0.40)),
64        ("dotnet_member", EdgeWeightConfig::new(0.65, 0.40)),
65        ("dotnet_same_namespace", EdgeWeightConfig::new(0.05, 0.40)),
66        ("dotnet_attribute", EdgeWeightConfig::new(0.50, 0.40)),
67        ("dotnet_partial", EdgeWeightConfig::new(0.80, 0.40)),
68        ("ruby_require", EdgeWeightConfig::new(0.65, 0.40)),
69        ("ruby_include", EdgeWeightConfig::new(0.60, 0.40)),
70        ("ruby_const", EdgeWeightConfig::new(0.55, 0.40)),
71        ("ruby_same_dir", EdgeWeightConfig::new(0.05, 0.40)),
72        ("php_use", EdgeWeightConfig::new(0.65, 0.40)),
73        ("php_require", EdgeWeightConfig::new(0.60, 0.40)),
74        ("php_inheritance", EdgeWeightConfig::new(0.75, 0.40)),
75        ("php_type", EdgeWeightConfig::new(0.55, 0.40)),
76        ("php_same_namespace", EdgeWeightConfig::new(0.05, 0.40)),
77        ("shell_source", EdgeWeightConfig::new(0.60, 0.35)),
78        ("shell_script", EdgeWeightConfig::new(0.50, 0.35)),
79        ("swift_import", EdgeWeightConfig::new(0.65, 0.40)),
80        ("swift_conformance", EdgeWeightConfig::new(0.70, 0.40)),
81        ("swift_extension", EdgeWeightConfig::new(0.65, 0.40)),
82        ("swift_type", EdgeWeightConfig::new(0.60, 0.40)),
83        ("swift_func", EdgeWeightConfig::new(0.55, 0.40)),
84        ("swift_same_module", EdgeWeightConfig::new(0.05, 0.40)),
85        ("zig_import", EdgeWeightConfig::new(0.65, 0.40)),
86        ("zig_type", EdgeWeightConfig::new(0.60, 0.40)),
87        ("zig_fn", EdgeWeightConfig::new(0.55, 0.40)),
88        ("haskell_import", EdgeWeightConfig::new(0.70, 0.40)),
89        ("haskell_type", EdgeWeightConfig::new(0.65, 0.40)),
90        ("haskell_fn", EdgeWeightConfig::new(0.60, 0.40)),
91        ("haskell_instance", EdgeWeightConfig::new(0.55, 0.40)),
92        ("clojure_require", EdgeWeightConfig::new(0.65, 0.40)),
93        ("clojure_fn", EdgeWeightConfig::new(0.60, 0.40)),
94        ("clojure_protocol", EdgeWeightConfig::new(0.55, 0.40)),
95        ("proto_import", EdgeWeightConfig::new(0.65, 0.40)),
96        ("proto_message_ref", EdgeWeightConfig::new(0.60, 0.40)),
97        ("proto_service_rpc", EdgeWeightConfig::new(0.55, 0.40)),
98        ("graphql_import", EdgeWeightConfig::new(0.60, 0.40)),
99        ("graphql_type_ref", EdgeWeightConfig::new(0.65, 0.40)),
100        ("graphql_extend", EdgeWeightConfig::new(0.55, 0.40)),
101        ("sql_fk", EdgeWeightConfig::new(0.70, 0.40)),
102        ("sql_table_ref", EdgeWeightConfig::new(0.60, 0.40)),
103        ("sql_view_source", EdgeWeightConfig::new(0.65, 0.40)),
104        ("sql_migration", EdgeWeightConfig::new(0.55, 0.40)),
105        ("css_import", EdgeWeightConfig::new(0.55, 0.40)),
106        ("lua_require", EdgeWeightConfig::new(0.65, 0.40)),
107        ("lua_fn", EdgeWeightConfig::new(0.55, 0.40)),
108        ("lua_method", EdgeWeightConfig::new(0.50, 0.40)),
109        ("openapi_internal_ref", EdgeWeightConfig::new(0.65, 0.40)),
110        ("openapi_external_ref", EdgeWeightConfig::new(0.60, 0.40)),
111        ("openapi_schema_ref", EdgeWeightConfig::new(0.55, 0.40)),
112        ("erlang_include", EdgeWeightConfig::new(0.65, 0.40)),
113        ("erlang_behaviour", EdgeWeightConfig::new(0.60, 0.40)),
114        ("erlang_call", EdgeWeightConfig::new(0.55, 0.40)),
115        ("elixir_use", EdgeWeightConfig::new(0.65, 0.40)),
116        ("elixir_alias", EdgeWeightConfig::new(0.60, 0.40)),
117        ("elixir_behaviour", EdgeWeightConfig::new(0.55, 0.40)),
118        ("elixir_fn", EdgeWeightConfig::new(0.50, 0.40)),
119        ("dart_import", EdgeWeightConfig::new(0.65, 0.40)),
120        ("dart_export", EdgeWeightConfig::new(0.60, 0.40)),
121        ("dart_type", EdgeWeightConfig::new(0.60, 0.40)),
122        ("dart_fn", EdgeWeightConfig::new(0.55, 0.40)),
123        ("dart_inheritance", EdgeWeightConfig::new(0.70, 0.40)),
124        ("cargo_workspace", EdgeWeightConfig::new(0.60, 0.40)),
125        ("cargo_path_dep", EdgeWeightConfig::new(0.65, 0.40)),
126        ("cargo_entry_point", EdgeWeightConfig::new(0.55, 0.40)),
127        ("ocaml_open", EdgeWeightConfig::new(0.65, 0.40)),
128        ("ocaml_type", EdgeWeightConfig::new(0.60, 0.40)),
129        ("ocaml_fn", EdgeWeightConfig::new(0.55, 0.40)),
130        ("ocaml_module_ref", EdgeWeightConfig::new(0.60, 0.40)),
131        ("r_source", EdgeWeightConfig::new(0.60, 0.40)),
132        ("r_library", EdgeWeightConfig::new(0.55, 0.40)),
133        ("r_fn", EdgeWeightConfig::new(0.50, 0.40)),
134        ("r_s4", EdgeWeightConfig::new(0.55, 0.40)),
135        ("perl_use", EdgeWeightConfig::new(0.65, 0.40)),
136        ("perl_require", EdgeWeightConfig::new(0.60, 0.40)),
137        ("perl_fn", EdgeWeightConfig::new(0.55, 0.40)),
138        ("perl_method", EdgeWeightConfig::new(0.50, 0.40)),
139        ("perl_inheritance", EdgeWeightConfig::new(0.65, 0.40)),
140        ("nim_import", EdgeWeightConfig::new(0.65, 0.40)),
141        ("nim_type", EdgeWeightConfig::new(0.60, 0.40)),
142        ("nim_fn", EdgeWeightConfig::new(0.55, 0.40)),
143        ("julia_using", EdgeWeightConfig::new(0.65, 0.40)),
144        ("julia_include", EdgeWeightConfig::new(0.60, 0.40)),
145        ("julia_type", EdgeWeightConfig::new(0.60, 0.40)),
146        ("julia_fn", EdgeWeightConfig::new(0.55, 0.40)),
147        ("ansible_include", EdgeWeightConfig::new(0.60, 0.40)),
148        ("ansible_role", EdgeWeightConfig::new(0.55, 0.40)),
149        ("bazel_deps", EdgeWeightConfig::new(0.65, 0.40)),
150        ("bazel_load", EdgeWeightConfig::new(0.60, 0.40)),
151        ("bazel_srcs", EdgeWeightConfig::new(0.55, 0.40)),
152        ("nix_import", EdgeWeightConfig::new(0.60, 0.40)),
153        ("dbt_ref", EdgeWeightConfig::new(0.65, 0.40)),
154        ("dbt_source", EdgeWeightConfig::new(0.60, 0.40)),
155        ("dbt_macro", EdgeWeightConfig::new(0.55, 0.40)),
156        ("latex_input", EdgeWeightConfig::new(0.65, 0.40)),
157        ("latex_package", EdgeWeightConfig::new(0.55, 0.40)),
158        ("latex_bib", EdgeWeightConfig::new(0.55, 0.40)),
159        ("prisma_schema", EdgeWeightConfig::new(0.65, 0.40)),
160        ("prisma_client", EdgeWeightConfig::new(0.55, 0.40)),
161    ];
162    entries.iter().copied().collect()
163});
164
165#[derive(Debug, Clone, Copy)]
166pub struct LangWeights {
167    pub call: f64,
168    pub symbol_ref: f64,
169    pub type_ref: f64,
170    pub lexical_min: f64,
171    pub lexical_max: f64,
172}
173
174pub static DEFAULT_LANG_WEIGHTS: LangWeights = LangWeights {
175    call: 0.55,
176    symbol_ref: 0.60,
177    type_ref: 0.50,
178    lexical_min: 0.08,
179    lexical_max: 0.15,
180};
181
182pub static LANG_WEIGHTS: Lazy<FxHashMap<&'static str, LangWeights>> = Lazy::new(|| {
183    let entries: &[(&str, LangWeights)] = &[
184        (
185            "python",
186            LangWeights {
187                call: 0.65,
188                symbol_ref: 0.70,
189                type_ref: 0.50,
190                lexical_min: 0.10,
191                lexical_max: 0.20,
192            },
193        ),
194        (
195            "javascript",
196            LangWeights {
197                call: 0.50,
198                symbol_ref: 0.55,
199                type_ref: 0.45,
200                lexical_min: 0.10,
201                lexical_max: 0.20,
202            },
203        ),
204        (
205            "jsx",
206            LangWeights {
207                call: 0.50,
208                symbol_ref: 0.55,
209                type_ref: 0.45,
210                lexical_min: 0.10,
211                lexical_max: 0.20,
212            },
213        ),
214        (
215            "typescript",
216            LangWeights {
217                call: 0.70,
218                symbol_ref: 0.75,
219                type_ref: 0.65,
220                lexical_min: 0.10,
221                lexical_max: 0.18,
222            },
223        ),
224        (
225            "tsx",
226            LangWeights {
227                call: 0.70,
228                symbol_ref: 0.75,
229                type_ref: 0.65,
230                lexical_min: 0.10,
231                lexical_max: 0.18,
232            },
233        ),
234        (
235            "rust",
236            LangWeights {
237                call: 0.90,
238                symbol_ref: 0.95,
239                type_ref: 0.85,
240                lexical_min: 0.05,
241                lexical_max: 0.10,
242            },
243        ),
244        (
245            "java",
246            LangWeights {
247                call: 0.85,
248                symbol_ref: 0.90,
249                type_ref: 0.80,
250                lexical_min: 0.05,
251                lexical_max: 0.10,
252            },
253        ),
254        (
255            "kotlin",
256            LangWeights {
257                call: 0.80,
258                symbol_ref: 0.85,
259                type_ref: 0.75,
260                lexical_min: 0.05,
261                lexical_max: 0.12,
262            },
263        ),
264        (
265            "scala",
266            LangWeights {
267                call: 0.80,
268                symbol_ref: 0.85,
269                type_ref: 0.75,
270                lexical_min: 0.05,
271                lexical_max: 0.12,
272            },
273        ),
274        (
275            "go",
276            LangWeights {
277                call: 0.80,
278                symbol_ref: 0.85,
279                type_ref: 0.75,
280                lexical_min: 0.05,
281                lexical_max: 0.12,
282            },
283        ),
284        (
285            "c",
286            LangWeights {
287                call: 0.60,
288                symbol_ref: 0.65,
289                type_ref: 0.55,
290                lexical_min: 0.08,
291                lexical_max: 0.15,
292            },
293        ),
294        (
295            "cpp",
296            LangWeights {
297                call: 0.65,
298                symbol_ref: 0.70,
299                type_ref: 0.60,
300                lexical_min: 0.08,
301                lexical_max: 0.15,
302            },
303        ),
304        (
305            "csharp",
306            LangWeights {
307                call: 0.75,
308                symbol_ref: 0.80,
309                type_ref: 0.70,
310                lexical_min: 0.05,
311                lexical_max: 0.12,
312            },
313        ),
314        (
315            "fsharp",
316            LangWeights {
317                call: 0.70,
318                symbol_ref: 0.75,
319                type_ref: 0.65,
320                lexical_min: 0.05,
321                lexical_max: 0.12,
322            },
323        ),
324        (
325            "ruby",
326            LangWeights {
327                call: 0.60,
328                symbol_ref: 0.65,
329                type_ref: 0.55,
330                lexical_min: 0.08,
331                lexical_max: 0.15,
332            },
333        ),
334        (
335            "php",
336            LangWeights {
337                call: 0.60,
338                symbol_ref: 0.65,
339                type_ref: 0.55,
340                lexical_min: 0.08,
341                lexical_max: 0.15,
342            },
343        ),
344        (
345            "shell",
346            LangWeights {
347                call: 0.40,
348                symbol_ref: 0.45,
349                type_ref: 0.35,
350                lexical_min: 0.10,
351                lexical_max: 0.18,
352            },
353        ),
354        (
355            "swift",
356            LangWeights {
357                call: 0.75,
358                symbol_ref: 0.80,
359                type_ref: 0.70,
360                lexical_min: 0.05,
361                lexical_max: 0.12,
362            },
363        ),
364    ];
365    entries.iter().copied().collect()
366});