-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathquiz4.py
119 lines (99 loc) · 3.86 KB
/
quiz4.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
# ========================================================================
# Copyright 2020 Emory University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========================================================================
import glob
import os
from types import SimpleNamespace
from typing import Iterable, Tuple, Any, List, Set
import ahocorasick
def create_ac(data: Iterable[Tuple[str, Any]]) -> ahocorasick.Automaton:
"""
Creates the Aho-Corasick automation and adds all (span, value) pairs in the data and finalizes this matcher.
:param data: a collection of (span, value) pairs.
"""
AC = ahocorasick.Automaton(ahocorasick.STORE_ANY)
for span, value in data:
if span in AC:
t = AC.get(span)
else:
t = SimpleNamespace(span=span, values=set())
AC.add_word(span, t)
t.values.add(value)
AC.make_automaton()
return AC
def read_gazetteers(dirname: str) -> ahocorasick.Automaton:
data = []
for filename in glob.glob(os.path.join(dirname, '*.txt')):
label = os.path.basename(filename)[:-4]
for line in open(filename):
data.append((line.strip(), label))
return create_ac(data)
def match(AC: ahocorasick.Automaton, tokens: List[str]) -> List[Tuple[str, int, int, Set[str]]]:
"""
:param AC: the finalized Aho-Corasick automation.
:param tokens: the list of input tokens.
:return: a list of tuples where each tuple consists of
- span: str,
- start token index (inclusive): int
- end token index (exclusive): int
- a set of values for the span: Set[str]
"""
smap, emap, idx = dict(), dict(), 0
for i, token in enumerate(tokens):
smap[idx] = i
idx += len(token)
emap[idx] = i
idx += 1
# find matches
text = ' '.join(tokens)
spans = []
for eidx, t in AC.iter(text):
eidx += 1
sidx = eidx - len(t.span)
sidx = smap.get(sidx, None)
eidx = emap.get(eidx, None)
if sidx is None or eidx is None: continue
spans.append((t.span, sidx, eidx + 1, t.values))
return spans
def remove_overlaps(entities: List[Tuple[str, int, int, Set[str]]]) -> List[Tuple[str, int, int, Set[str]]]:
"""
:param entities: a list of tuples where each tuple consists of
- span: str,
- start token index (inclusive): int
- end token index (exclusive): int
- a set of values for the span: Set[str]
:return: a list of entities where each entity is represented by a tuple of (span, start index, end index, value set)
"""
# TODO: to be updated
return []
def to_bilou(tokens: List[str], entities: List[Tuple[str, int, int, str]]) -> List[str]:
"""
:param tokens: a list of tokens.
:param entities: a list of tuples where each tuple consists of
- span: str,
- start token index (inclusive): int
- end token index (exclusive): int
- a named entity tag
:return: a list of named entity tags in the BILOU notation with respect to the tokens
"""
# TODO: to be updated
return []
if __name__ == '__main__':
gaz_dir = 'res/ner'
AC = read_gazetteers('res/ner')
tokens = 'Atlantic City of Georgia'.split()
entities = match(AC, tokens)
entities = remove_overlaps(entities)
print(entities)