error-align 0.1.0b1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- error_align/__init__.py +7 -0
- error_align/backtrace_graph.py +258 -0
- error_align/baselines/__init__.py +10 -0
- error_align/baselines/optimal_word_alignment.py +111 -0
- error_align/baselines/power/normalize/__init__.py +3 -0
- error_align/baselines/power/normalize/contractions.py +152 -0
- error_align/baselines/power/normalize/normalize.py +295 -0
- error_align/baselines/power/normalize/numbers.py +289 -0
- error_align/baselines/power/power/__init__.py +3 -0
- error_align/baselines/power/power/aligner.py +467 -0
- error_align/baselines/power/power/levenshtein.py +576 -0
- error_align/baselines/power/power/phonemes.py +16 -0
- error_align/baselines/power/power/pronounce.py +85 -0
- error_align/baselines/power/power/punct.py +166 -0
- error_align/baselines/power/power/readers.py +48 -0
- error_align/baselines/power/power/writers.py +259 -0
- error_align/baselines/power_alignment.py +61 -0
- error_align/baselines/rapidfuzz_word_alignment.py +100 -0
- error_align/baselines/utils.py +93 -0
- error_align/edit_distance.py +144 -0
- error_align/error_align.py +495 -0
- error_align/func.py +38 -0
- error_align/utils.py +166 -0
- error_align-0.1.0b1.dist-info/METADATA +161 -0
- error_align-0.1.0b1.dist-info/RECORD +26 -0
- error_align-0.1.0b1.dist-info/WHEEL +4 -0
error_align/__init__.py
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import random
|
|
2
|
+
from collections import Counter
|
|
3
|
+
|
|
4
|
+
from error_align.utils import OP_TYPE_COMBO_MAP, OpType
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Node:
|
|
8
|
+
"""Node in the backtrace graph corresponding to the index (i, j) in the backtrace matrix."""
|
|
9
|
+
|
|
10
|
+
def __init__(self, hyp_idx, ref_idx) -> None:
|
|
11
|
+
"""Initialize the node at index (i, j)."""
|
|
12
|
+
self.hyp_idx = hyp_idx
|
|
13
|
+
self.ref_idx = ref_idx
|
|
14
|
+
self.children = {}
|
|
15
|
+
self.parents = {}
|
|
16
|
+
|
|
17
|
+
# Used to count the number of paths going through this node.
|
|
18
|
+
self._bwd_node_count = 0
|
|
19
|
+
self._fwd_node_count = 0
|
|
20
|
+
|
|
21
|
+
# Used to store the operations that lead to this node.
|
|
22
|
+
self._ingoing_edge_counts = {}
|
|
23
|
+
self._outgoing_edge_counts = {}
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def index(self):
|
|
27
|
+
return (self.hyp_idx, self.ref_idx)
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def offset_index(self):
|
|
31
|
+
"""Get the offset index of the node so indices match the hypothesis and reference strings.
|
|
32
|
+
|
|
33
|
+
Root will be at (-1, -1).
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
tuple[int, int]: The offset index of the node in the backtrace matrix.
|
|
37
|
+
|
|
38
|
+
"""
|
|
39
|
+
return (self.hyp_idx - 1, self.ref_idx - 1)
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def number_of_paths(self):
|
|
43
|
+
return self._bwd_node_count * self._fwd_node_count
|
|
44
|
+
|
|
45
|
+
def number_of_ingoing_paths_via(self, op_type: OpType):
|
|
46
|
+
"""Get the number of paths going through this node via the given operation type.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
op_type (OpType): The operation type to check.
|
|
50
|
+
|
|
51
|
+
"""
|
|
52
|
+
if op_type not in self.parents:
|
|
53
|
+
return 0
|
|
54
|
+
return self._ingoing_edge_counts[op_type] * self.parents[op_type]._outgoing_edge_counts[op_type]
|
|
55
|
+
|
|
56
|
+
def number_of_outgoing_paths_via(self, op_type: OpType):
|
|
57
|
+
"""Get the number of paths going through this node via the given operation type.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
op_type (OpType): The operation type to check.
|
|
61
|
+
|
|
62
|
+
"""
|
|
63
|
+
if op_type not in self.children:
|
|
64
|
+
return 0
|
|
65
|
+
return self._outgoing_edge_counts[op_type] * self.children[op_type]._ingoing_edge_counts[op_type]
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def is_terminal(self):
|
|
69
|
+
"""Check if the node is a terminal node (i.e., it has no children)."""
|
|
70
|
+
return len(self.children) == 0
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def is_root(self):
|
|
74
|
+
"""Check if the node is a root node (i.e., it has no parents)."""
|
|
75
|
+
return len(self.parents) == 0
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class BacktraceGraph:
|
|
79
|
+
"""Backtrace alignment graph."""
|
|
80
|
+
|
|
81
|
+
def __init__(self, backtrace_matrix: list[list[int]]) -> None:
|
|
82
|
+
"""Create a graph from the backtrace matrix."""
|
|
83
|
+
self.hyp_dim = len(backtrace_matrix)
|
|
84
|
+
self.ref_dim = len(backtrace_matrix[0])
|
|
85
|
+
self.hyp_max_idx = self.hyp_dim - 1
|
|
86
|
+
self.ref_max_idx = self.ref_dim - 1
|
|
87
|
+
self.backtrace_matrix = backtrace_matrix
|
|
88
|
+
|
|
89
|
+
self._nodes = None
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def nodes(self) -> dict[tuple[int, int], Node]:
|
|
93
|
+
"""Get the nodes in the graph.
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
dict: A dictionary of nodes indexed by their (hyp_idx, ref_idx).
|
|
97
|
+
|
|
98
|
+
"""
|
|
99
|
+
if self._nodes is None:
|
|
100
|
+
terminal_node = Node(self.hyp_max_idx, self.ref_max_idx)
|
|
101
|
+
self._nodes = {terminal_node.index: terminal_node}
|
|
102
|
+
|
|
103
|
+
# Traverse nodes in reverse topological order to add parents.
|
|
104
|
+
for index in self._iter_topological_order(reverse=True):
|
|
105
|
+
if index in self._nodes and index != (0, 0):
|
|
106
|
+
self._add_parents_from_backtrace(index)
|
|
107
|
+
|
|
108
|
+
# Sort nodes by their indices to ensure topological order.
|
|
109
|
+
self._nodes = dict(sorted(self._nodes.items(), key=lambda item: (item[0][0], item[0][1])))
|
|
110
|
+
self._set_path_and_node_counts() # TODO: Consider to do this lazily. Shouldn't be too expensive.
|
|
111
|
+
|
|
112
|
+
return self._nodes
|
|
113
|
+
|
|
114
|
+
@property
|
|
115
|
+
def number_of_paths(self):
|
|
116
|
+
"""Count the number of paths in the graph.
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
int: The number of paths.
|
|
120
|
+
|
|
121
|
+
"""
|
|
122
|
+
return self.get_node(0, 0)._bwd_node_count
|
|
123
|
+
|
|
124
|
+
def get_node(self, hyp_idx, ref_idx):
|
|
125
|
+
"""Get the node at the given index.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
hyp_idx (int): Hyp/row index.
|
|
129
|
+
ref_idx (int): Ref/column index.
|
|
130
|
+
|
|
131
|
+
"""
|
|
132
|
+
return self.nodes[(hyp_idx, ref_idx)]
|
|
133
|
+
|
|
134
|
+
def get_node_set(self):
|
|
135
|
+
"""Get the set of all node indices in the graph.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
set: A set of all node indices.
|
|
139
|
+
|
|
140
|
+
"""
|
|
141
|
+
transitions = set()
|
|
142
|
+
for node in self.nodes.values():
|
|
143
|
+
transitions.add(node.offset_index)
|
|
144
|
+
return transitions
|
|
145
|
+
|
|
146
|
+
def get_path(self, sample=False):
|
|
147
|
+
"""Get a path through the graph.
|
|
148
|
+
|
|
149
|
+
Args:
|
|
150
|
+
sample (bool): If True, sample a path randomly based on the transition probabilities. Otherwise, return
|
|
151
|
+
the first path deterministically.
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
list[Node]: A list of nodes representing the path.
|
|
155
|
+
|
|
156
|
+
"""
|
|
157
|
+
node = self.get_node(0, 0)
|
|
158
|
+
assert node.is_root, "The node at (-1, -1) was expected to be a root node."
|
|
159
|
+
|
|
160
|
+
path = []
|
|
161
|
+
while not node.is_terminal:
|
|
162
|
+
if sample:
|
|
163
|
+
op_type = random.choice(list(node.children.keys()))
|
|
164
|
+
else:
|
|
165
|
+
op_type = next(iter(node.children.keys()))
|
|
166
|
+
node = node.children[op_type]
|
|
167
|
+
path.append((op_type, node))
|
|
168
|
+
|
|
169
|
+
return path
|
|
170
|
+
|
|
171
|
+
def get_unambiguous_matches(self, ref):
|
|
172
|
+
"""Get word spans that are unambiguously matched (i.e., only one path in backtrace graph).
|
|
173
|
+
|
|
174
|
+
Returns:
|
|
175
|
+
list[Node]: A list of nodes representing the unambiguous path.
|
|
176
|
+
|
|
177
|
+
"""
|
|
178
|
+
ref = "*" + ref # Index offset
|
|
179
|
+
mono_match_end_nodes = set()
|
|
180
|
+
ref_idxs = Counter()
|
|
181
|
+
hyp_idxs = Counter()
|
|
182
|
+
for (hyp_idx, ref_idx), node in self.nodes.items():
|
|
183
|
+
if OpType.MATCH in node.parents and ref[ref_idx] == "<":
|
|
184
|
+
_ref_idx, _hyp_idx = ref_idx + 1, hyp_idx + 1
|
|
185
|
+
while True:
|
|
186
|
+
if (_hyp_idx, _ref_idx) not in self.nodes:
|
|
187
|
+
break
|
|
188
|
+
if OpType.MATCH not in self.nodes[(_hyp_idx, _ref_idx)].parents:
|
|
189
|
+
break
|
|
190
|
+
if ref[_ref_idx] == ">":
|
|
191
|
+
end_index = (_hyp_idx, _ref_idx)
|
|
192
|
+
mono_match_end_nodes.add(end_index)
|
|
193
|
+
ref_idxs[_ref_idx] += 1
|
|
194
|
+
hyp_idxs[_hyp_idx] += 1
|
|
195
|
+
break
|
|
196
|
+
_ref_idx, _hyp_idx = _ref_idx + 1, _hyp_idx + 1
|
|
197
|
+
|
|
198
|
+
return {(h - 1, r - 1) for h, r in mono_match_end_nodes if hyp_idxs[h] == 1 and ref_idxs[r] == 1}
|
|
199
|
+
|
|
200
|
+
def _parent_index_from_op_type(self, hyp_idx, ref_idx, op_type):
|
|
201
|
+
"""Create a parent node based on the index of the current node and the operation type."""
|
|
202
|
+
hyp_idx = hyp_idx - 1 if op_type != OpType.DELETE else hyp_idx
|
|
203
|
+
ref_idx = ref_idx - 1 if op_type != OpType.INSERT else ref_idx
|
|
204
|
+
if (hyp_idx, ref_idx) not in self._nodes:
|
|
205
|
+
self._nodes[(hyp_idx, ref_idx)] = Node(hyp_idx, ref_idx)
|
|
206
|
+
return self._nodes[(hyp_idx, ref_idx)]
|
|
207
|
+
|
|
208
|
+
def _iter_topological_order(self, reverse=False):
|
|
209
|
+
"""Iterate through the nodes in topological order."""
|
|
210
|
+
if reversed:
|
|
211
|
+
for i in reversed(range(self.hyp_dim)):
|
|
212
|
+
for j in reversed(range(self.ref_dim)):
|
|
213
|
+
yield (i, j)
|
|
214
|
+
else:
|
|
215
|
+
for i in range(self.hyp_dim):
|
|
216
|
+
for j in range(self.ref_dim):
|
|
217
|
+
yield (i, j)
|
|
218
|
+
|
|
219
|
+
def _add_parents_from_backtrace(self, index):
|
|
220
|
+
"""Add parents to the node at the given index based on the backtrace matrix."""
|
|
221
|
+
node = self._nodes.get(index, None)
|
|
222
|
+
|
|
223
|
+
assert node is not None, f"Node at index {index} does not exist in the graph."
|
|
224
|
+
|
|
225
|
+
op_type_combo_code = self.backtrace_matrix[node.hyp_idx][node.ref_idx]
|
|
226
|
+
op_type_combo = OP_TYPE_COMBO_MAP[op_type_combo_code]
|
|
227
|
+
|
|
228
|
+
for op_type in op_type_combo:
|
|
229
|
+
parent_node = self._parent_index_from_op_type(*node.index, op_type)
|
|
230
|
+
node.parents[op_type] = parent_node
|
|
231
|
+
parent_node.children[op_type] = node
|
|
232
|
+
|
|
233
|
+
def _set_path_and_node_counts(self):
|
|
234
|
+
"""Count the number of paths going through any node in the graph using the forward-backward algorithm."""
|
|
235
|
+
ordered_nodes = list(self.nodes.values())
|
|
236
|
+
root_node = ordered_nodes[0]
|
|
237
|
+
terminal_node = ordered_nodes[-1]
|
|
238
|
+
assert root_node.is_root, "The first node must be a root node."
|
|
239
|
+
assert terminal_node.is_terminal, "The last node must be a terminal node."
|
|
240
|
+
|
|
241
|
+
# Forward pass
|
|
242
|
+
ordered_nodes[0]._fwd_node_count = 1
|
|
243
|
+
for node in ordered_nodes[1:]:
|
|
244
|
+
for op_type, parent in node.parents.items():
|
|
245
|
+
node._fwd_node_count += parent._fwd_node_count
|
|
246
|
+
node._ingoing_edge_counts[op_type] = parent._fwd_node_count
|
|
247
|
+
|
|
248
|
+
# Backward pass
|
|
249
|
+
ordered_nodes[-1]._bwd_node_count = 1
|
|
250
|
+
for node in reversed(ordered_nodes[:-1]):
|
|
251
|
+
for op_type, child in node.children.items():
|
|
252
|
+
node._bwd_node_count += child._bwd_node_count
|
|
253
|
+
node._outgoing_edge_counts[op_type] = child._bwd_node_count
|
|
254
|
+
|
|
255
|
+
# Validate that the number of forward and backward paths are equal
|
|
256
|
+
assert root_node._bwd_node_count == terminal_node._fwd_node_count, (
|
|
257
|
+
"The number of forward and backward paths must be equal."
|
|
258
|
+
)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
try:
|
|
3
|
+
from error_align.baselines.optimal_word_alignment import OptimalWordAlign as OptimalWordAlign
|
|
4
|
+
from error_align.baselines.power_alignment import PowerAlign as PowerAlign
|
|
5
|
+
from error_align.baselines.rapidfuzz_word_alignment import RapidFuzzWordAlign as RapidFuzzWordAlign
|
|
6
|
+
except ImportError:
|
|
7
|
+
raise ImportError(
|
|
8
|
+
"Baselines module could not be imported. Please ensure all dependencies are installed. "
|
|
9
|
+
"You can install the evaluation dependencies using: pip install error-align[evaluation]"
|
|
10
|
+
)
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
from rapidfuzz.distance import Levenshtein
|
|
2
|
+
|
|
3
|
+
from error_align.backtrace_graph import BacktraceGraph
|
|
4
|
+
from error_align.edit_distance import compute_distance_matrix
|
|
5
|
+
from error_align.utils import (
|
|
6
|
+
Alignment,
|
|
7
|
+
OpType,
|
|
8
|
+
basic_normalizer,
|
|
9
|
+
basic_tokenizer,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _get_optimal_word_alignment_values(ref_token: str, hyp_token: str):
|
|
14
|
+
"""Compute the optimal word alignment values for deletion, insertion, and diagonal (substitution or match).
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
ref_token (str): The reference token.
|
|
18
|
+
hyp_token (str): The hypothesis token.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
tuple: A tuple containing the deletion cost, insertion cost, and diagonal cost.
|
|
22
|
+
"""
|
|
23
|
+
if hyp_token == ref_token:
|
|
24
|
+
diag_cost = 0
|
|
25
|
+
else:
|
|
26
|
+
diag_cost = Levenshtein.distance(ref_token, hyp_token, weights=(1, 1, 2))
|
|
27
|
+
diag_cost += abs(len(ref_token) - len(hyp_token))
|
|
28
|
+
|
|
29
|
+
return len(hyp_token), len(ref_token), diag_cost
|
|
30
|
+
|
|
31
|
+
class OptimalWordAlign:
|
|
32
|
+
"""Optimal word-level alignment based on global-to-local edits (GLE) metric."""
|
|
33
|
+
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
ref: str,
|
|
37
|
+
hyp: str,
|
|
38
|
+
tokenizer: callable = basic_tokenizer,
|
|
39
|
+
normalizer: callable = basic_normalizer,
|
|
40
|
+
):
|
|
41
|
+
"""Initialize the optimal word-level alignment with reference and hypothesis texts.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
ref (str): The reference sequence/transcript.
|
|
45
|
+
hyp (str): The hypothesis sequence/transcript.
|
|
46
|
+
tokenizer (callable): A function to tokenize the sequences. Must be regex-based and return Match objects.
|
|
47
|
+
normalizer (callable): A function to normalize the tokens. Defaults to basic_normalizer.
|
|
48
|
+
"""
|
|
49
|
+
if not isinstance(ref, str):
|
|
50
|
+
raise TypeError("Reference sequence must be a string.")
|
|
51
|
+
if not isinstance(hyp, str):
|
|
52
|
+
raise TypeError("Hypothesis sequence must be a string.")
|
|
53
|
+
|
|
54
|
+
self.ref = ref
|
|
55
|
+
self.hyp = hyp
|
|
56
|
+
self._ref_token_matches = tokenizer(ref)
|
|
57
|
+
self._hyp_token_matches = tokenizer(hyp)
|
|
58
|
+
self._ref = [normalizer(r.group()) for r in self._ref_token_matches]
|
|
59
|
+
self._hyp = [normalizer(h.group()) for h in self._hyp_token_matches]
|
|
60
|
+
self._ref_max_idx = len(self._ref) - 1
|
|
61
|
+
self._hyp_max_idx = len(self._hyp) - 1
|
|
62
|
+
self.end_index = (self._hyp_max_idx, self._ref_max_idx)
|
|
63
|
+
|
|
64
|
+
# Extract backtrace graph.
|
|
65
|
+
_, B = compute_distance_matrix(
|
|
66
|
+
self._ref,
|
|
67
|
+
self._hyp,
|
|
68
|
+
_get_optimal_word_alignment_values,
|
|
69
|
+
backtrace=True,
|
|
70
|
+
dtype=float,
|
|
71
|
+
)
|
|
72
|
+
self._backtrace_graph = BacktraceGraph(B)
|
|
73
|
+
|
|
74
|
+
def align(self) -> list[Alignment]:
|
|
75
|
+
"""Extract an arbitrary path from the backtrace graph -- all paths are equal wrt. to the GLE metric.
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
list[Alignment]: A list of Alignment objects.
|
|
79
|
+
"""
|
|
80
|
+
path = self._backtrace_graph.get_path()
|
|
81
|
+
alignments = []
|
|
82
|
+
for op_type, node in path:
|
|
83
|
+
if op_type == OpType.MATCH or op_type == OpType.SUBSTITUTE:
|
|
84
|
+
ref_match = self._ref_token_matches[node.ref_idx - 1]
|
|
85
|
+
hyp_match = self._hyp_token_matches[node.hyp_idx - 1]
|
|
86
|
+
alignment = Alignment(
|
|
87
|
+
op_type=op_type,
|
|
88
|
+
ref_slice=slice(*ref_match.span()),
|
|
89
|
+
hyp_slice=slice(*hyp_match.span()),
|
|
90
|
+
ref=ref_match.group(),
|
|
91
|
+
hyp=hyp_match.group(),
|
|
92
|
+
)
|
|
93
|
+
elif op_type == OpType.DELETE:
|
|
94
|
+
ref_match = self._ref_token_matches[node.ref_idx - 1]
|
|
95
|
+
alignment = Alignment(
|
|
96
|
+
op_type=op_type,
|
|
97
|
+
ref_slice=slice(*ref_match.span()),
|
|
98
|
+
ref=ref_match.group(),
|
|
99
|
+
)
|
|
100
|
+
elif op_type == OpType.INSERT:
|
|
101
|
+
hyp_match = self._hyp_token_matches[node.hyp_idx - 1]
|
|
102
|
+
alignment = Alignment(
|
|
103
|
+
op_type=op_type,
|
|
104
|
+
hyp_slice=slice(*hyp_match.span()),
|
|
105
|
+
hyp=hyp_match.group(),
|
|
106
|
+
)
|
|
107
|
+
else:
|
|
108
|
+
raise ValueError(f"Unknown operation type: {op_type}")
|
|
109
|
+
alignments.append(alignment)
|
|
110
|
+
|
|
111
|
+
return alignments
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
from collections import defaultdict
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ContractionsEng(object):
|
|
5
|
+
@staticmethod
|
|
6
|
+
def isContraction(word):
|
|
7
|
+
return word in ContractionsEng.contractions
|
|
8
|
+
|
|
9
|
+
@staticmethod
|
|
10
|
+
def expandOptions(word):
|
|
11
|
+
if ContractionsEng.isContraction(word):
|
|
12
|
+
return ContractionsEng.contractions[word]
|
|
13
|
+
return set()
|
|
14
|
+
|
|
15
|
+
@staticmethod
|
|
16
|
+
def contractOptions(word):
|
|
17
|
+
if word in ContractionsEng.expansions:
|
|
18
|
+
return ContractionsEng.expansions[word]
|
|
19
|
+
return set()
|
|
20
|
+
|
|
21
|
+
@staticmethod
|
|
22
|
+
def isDashEquivalent(cont, real):
|
|
23
|
+
options = ContractionsEng.expandOptions(cont)
|
|
24
|
+
return options is not None and real in options
|
|
25
|
+
|
|
26
|
+
# From wikipedia
|
|
27
|
+
# http://en.wikipedia.org/wiki/Wikipedia%3aList_of_English_contractions
|
|
28
|
+
contractions = {
|
|
29
|
+
"ain't": set(["am not", "are not", "is not", "has not", "have not"]),
|
|
30
|
+
"aren't": set(["are not", "am not"]),
|
|
31
|
+
"can't": set(["cannot", "can not"]), # Manually added "can not"
|
|
32
|
+
"can't've": set(["cannot have"]),
|
|
33
|
+
"'cause": set(["because"]),
|
|
34
|
+
"could've": set(["could have"]),
|
|
35
|
+
"couldn't": set(["could not"]),
|
|
36
|
+
"couldn't've": set(["could not have"]),
|
|
37
|
+
"didn't": set(["did not"]),
|
|
38
|
+
"doesn't": set(["does not"]),
|
|
39
|
+
"don't": set(["do not"]),
|
|
40
|
+
"hadn't": set(["had not"]),
|
|
41
|
+
"hadn't've": set(["had not have"]),
|
|
42
|
+
"hasn't": set(["has not"]),
|
|
43
|
+
"haven't": set(["have not"]),
|
|
44
|
+
"he'd": set(["he had", "he would"]),
|
|
45
|
+
"he'd've": set(["he would have"]),
|
|
46
|
+
"he'll": set(["he shall", "he will"]),
|
|
47
|
+
"he'll've": set(["he shall have", "he will have"]),
|
|
48
|
+
"he's": set(["he has", "he is"]),
|
|
49
|
+
"how'd": set(["how did"]),
|
|
50
|
+
"how'd'y": set(["how do you"]),
|
|
51
|
+
"how'll": set(["how will"]),
|
|
52
|
+
"how's": set(["how has", "how is", "how does"]),
|
|
53
|
+
"i'd": set(["i had", "i would"]),
|
|
54
|
+
"i'd've": set(["i would have"]),
|
|
55
|
+
"i'll": set(["i shall", "i will"]),
|
|
56
|
+
"i'll've": set(["i shall have", "i will have"]),
|
|
57
|
+
"i'm": set(["i am"]),
|
|
58
|
+
"i've": set(["i have"]),
|
|
59
|
+
"isn't": set(["is not"]),
|
|
60
|
+
"it'd": set(["it had", "it would"]),
|
|
61
|
+
"it'd've": set(["it would have"]),
|
|
62
|
+
"it'll": set(["it shall", "it will"]),
|
|
63
|
+
"it'll've": set(["it shall have", "it will have"]),
|
|
64
|
+
"it's": set(["it has", "it is"]),
|
|
65
|
+
"let's": set(["let us"]),
|
|
66
|
+
"ma'am": set(["madam"]),
|
|
67
|
+
"mayn't": set(["may not"]),
|
|
68
|
+
"might've": set(["might have"]),
|
|
69
|
+
"mightn't": set(["might not"]),
|
|
70
|
+
"mightn't've": set(["might not have"]),
|
|
71
|
+
"must've": set(["must have"]),
|
|
72
|
+
"mustn't": set(["must not"]),
|
|
73
|
+
"mustn't've": set(["must not have"]),
|
|
74
|
+
"needn't": set(["need not"]),
|
|
75
|
+
"needn't've": set(["need not have"]),
|
|
76
|
+
"o'clock": set(["of the clock"]),
|
|
77
|
+
"oughtn't": set(["ought not"]),
|
|
78
|
+
"oughtn't've": set(["ought not have"]),
|
|
79
|
+
"shan't": set(["shall not"]),
|
|
80
|
+
"sha'n't": set(["shall not"]),
|
|
81
|
+
"shan't've": set(["shall not have"]),
|
|
82
|
+
"she'd": set(["she had", "she would"]),
|
|
83
|
+
"she'd've": set(["she would have"]),
|
|
84
|
+
"she'll": set(["she shall", "she will"]),
|
|
85
|
+
"she'll've": set(["she shall have", "she will have"]),
|
|
86
|
+
"she's": set(["she has", "she is"]),
|
|
87
|
+
"should've": set(["should have"]),
|
|
88
|
+
"shouldn't": set(["should not"]),
|
|
89
|
+
"shouldn't've": set(["should not have"]),
|
|
90
|
+
"so've": set(["so have"]),
|
|
91
|
+
"so's": set(["so as", "so is"]),
|
|
92
|
+
"that'd": set(["that would", "that had"]),
|
|
93
|
+
"that'd've": set(["that would have"]),
|
|
94
|
+
"that's": set(["that has", "that is"]),
|
|
95
|
+
"there'd": set(["there had", "there would"]),
|
|
96
|
+
"there'd've": set(["there would have"]),
|
|
97
|
+
"there's": set(["there has", "there is"]),
|
|
98
|
+
"they'd": set(["they had", "they would"]),
|
|
99
|
+
"they'd've": set(["they would have"]),
|
|
100
|
+
"they'll": set(["they shall", "they will"]),
|
|
101
|
+
"they'll've": set(["they shall have", "they will have"]),
|
|
102
|
+
"they're": set(["they are"]),
|
|
103
|
+
"they've": set(["they have"]),
|
|
104
|
+
"to've": set(["to have"]),
|
|
105
|
+
"wasn't": set(["was not"]),
|
|
106
|
+
"we'd": set(["we had", "we would"]),
|
|
107
|
+
"we'd've": set(["we would have"]),
|
|
108
|
+
"we'll": set(["we will"]),
|
|
109
|
+
"we'll've": set(["we will have"]),
|
|
110
|
+
"we're": set(["we are"]),
|
|
111
|
+
"we've": set(["we have"]),
|
|
112
|
+
"weren't": set(["were not"]),
|
|
113
|
+
"what'll": set(["what shall", "what will"]),
|
|
114
|
+
"what'll've": set(["what shall have", "what will have"]),
|
|
115
|
+
"what're": set(["what are"]),
|
|
116
|
+
"what's": set(["what has", "what is"]),
|
|
117
|
+
"what've": set(["what have"]),
|
|
118
|
+
"when's": set(["when has", "when is"]),
|
|
119
|
+
"when've": set(["when have"]),
|
|
120
|
+
"where'd": set(["where did"]),
|
|
121
|
+
"where's": set(["where has", "where is"]),
|
|
122
|
+
"where've": set(["where have"]),
|
|
123
|
+
"who'll": set(["who shall", "who will"]),
|
|
124
|
+
"who'll've": set(["who shall have", "who will have"]),
|
|
125
|
+
"who's": set(["who has", "who is"]),
|
|
126
|
+
"who've": set(["who have"]),
|
|
127
|
+
"why's": set(["why has", "why is"]),
|
|
128
|
+
"why've": set(["why have"]),
|
|
129
|
+
"will've": set(["will have"]),
|
|
130
|
+
"won't": set(["will not"]),
|
|
131
|
+
"won't've": set(["will not have"]),
|
|
132
|
+
"would've": set(["would have"]),
|
|
133
|
+
"wouldn't": set(["would not"]),
|
|
134
|
+
"wouldn't've": set(["would not have"]),
|
|
135
|
+
"y'all": set(["you all"]),
|
|
136
|
+
"y'all'd": set(["you all would"]),
|
|
137
|
+
"y'all'd've": set(["you all would have"]),
|
|
138
|
+
"y'all're": set(["you all are"]),
|
|
139
|
+
"y'all've": set(["you all have"]),
|
|
140
|
+
"you'd": set(["you had", "you would"]),
|
|
141
|
+
"you'd've": set(["you would have"]),
|
|
142
|
+
"you'll": set(["you shall", "you will"]),
|
|
143
|
+
"you'll've": set(["you shall have", "you will have"]),
|
|
144
|
+
"you're": set(["you are"]),
|
|
145
|
+
"you've": set(["you have"]),
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
# Reverse contractions data structure
|
|
149
|
+
expansions = defaultdict(set)
|
|
150
|
+
for key,value_set in contractions.items():
|
|
151
|
+
for value in value_set:
|
|
152
|
+
expansions[value].add(key)
|