-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtiebreak.py
68 lines (44 loc) · 1.62 KB
/
tiebreak.py
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
import ast
from PythonVoiceCodingPlugin.library.LCA import LCA
from PythonVoiceCodingPlugin.library.level_info import LevelVisitor
from PythonVoiceCodingPlugin.library.traverse import match_node
def tiebreak_on_lca(root,origin,candidates,lca = None):
"""ranks nodes based on the depth of their lowest_common_ancestor
With origin (the deeper the better). In case of ties the note
Closer to the LCA is preferred.
Args:
root (TYPE): Description
origin (TYPE): Description
candidates (TYPE): Description
Returns:
TYPE: Description
"""
lca = LCA(root) if lca is None else lca
def tiebreaker(x):
depth,node = lca(x, origin,node_and_depth = True)
v = 3
if match_node(node,ast.Dict):
if node is not x and node is not origin:
field,field_index = lca.get_field_with_respect_to(x,node)
ofield,ofield_index = lca.get_field_with_respect_to(origin,node)
v = abs(field_index - ofield_index)
v = v if v<3 else 3
return (-1 * depth,v,lca.get_depth(x),abs(x.first_token.start[0] - origin.first_token.start[0]))
return sorted(candidates, key = tiebreaker)
def tiebreak_on_visual(original_line,result,alternatives):
if result:
if original_line:
k = lambda x: (
abs(x.first_token.start[0] - result.first_token.start[0]) +
(
10 if (
x.first_token.start[0]<=original_line<=result.first_token.start[0] or
x.first_token.start[0]>=original_line>=result.first_token.start[0]
) else 0
)
)
else:
k = lambda x: abs(x.first_token.start[0] - result.first_token.start[0])
if alternatives:
return sorted(alternatives, key = k)
return alternatives