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