forked from Answeror/lit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
137 lines (115 loc) · 3.81 KB
/
utils.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from levenshtein import levenshtein
def _common_prefix_length(s1, s2):
for i, (c1, c2) in enumerate(zip(s1, s2)):
if c1 != c2:
return i
return min(len(s1), len(s2))
class Query(object):
def __init__(
self,
text,
deletion_cost=1,
insertion_cost=1,
first_insertion_cost=1,
prepend_first_insertion_cost=1,
append_first_insertion_cost=1,
substitution_cost=1,
transposition_cost=1
):
self.text = text
self.deletion_cost = deletion_cost
self.insertion_cost = insertion_cost
self.first_insertion_cost = first_insertion_cost
self.prepend_first_insertion_cost = prepend_first_insertion_cost
self.append_first_insertion_cost = append_first_insertion_cost
self.substitution_cost = substitution_cost
self.transposition_cost = transposition_cost
self.memo = []
self.precol = []
def update(self, text):
# remove last common position to make insertion penalty work
self.memo = self.memo[:_common_prefix_length(self.text, text)]
self.precol = []
self.text = text
def distance_to(self, text):
return levenshtein(
self.text,
text,
deletion_cost=self.deletion_cost,
insertion_cost=self.insertion_cost,
first_insertion_cost=self.first_insertion_cost,
prepend_first_insertion_cost=self.prepend_first_insertion_cost,
append_first_insertion_cost=self.append_first_insertion_cost,
substitution_cost=self.substitution_cost,
transposition_cost=self.transposition_cost,
memo=self.memo,
precol=self.precol
)
class TreeQuery(object):
def __init__(
self,
tree,
query,
deletion_cost=1,
insertion_cost=1,
first_insertion_cost=1,
substitution_cost=1,
transposition_cost=1
):
self.query = query
self.deletion_cost = deletion_cost
self.insertion_cost = insertion_cost
self.first_insertion_cost = first_insertion_cost
self.substitution_cost = substitution_cost
self.transposition_cost = transposition_cost
self.nodes = []
def add(parent, tree):
for name, child in tree.items():
node = TreeQueryNode(text=name, tree=self, parent=parent)
self.nodes.append(node)
add(node, child)
add(None, tree)
self.update('')
def update(self, query):
for node in self.nodes:
node.update(self.query, query)
self.query = query
def best(self, count):
return sorted(self.nodes, key=lambda node: node())[:count]
class TreeQueryNode(object):
def __init__(
self,
text,
tree,
parent=None
):
self.text = text
self.tree = tree
self.parent = parent
self.memo = []
self.precol = []
@property
def path(self):
names = []
node = self
while node:
names.append(node.text)
node = node.parent
return reversed(names)
def update(self, old, new):
self.memo = self.memo[:_common_prefix_length(old, new) + 1]
self.precol = [] if self.parent is None else [x[-1] for x in self.parent.memo]
levenshtein(
new,
self.text,
deletion_cost=self.tree.deletion_cost,
insertion_cost=self.tree.insertion_cost,
substitution_cost=self.tree.substitution_cost,
transposition_cost=self.tree.transposition_cost,
memo=self.memo,
precol=self.precol
)
def __call__(self):
return self.memo[len(self.tree.query)][-1]