forked from v-mipeng/LexiconAugmentedNER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gazetteer.py
49 lines (39 loc) · 1.51 KB
/
gazetteer.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
from utils.trie import Trie
class Gazetteer:
def __init__(self, lower):
self.trie = Trie()
self.ent2type = {} ## word list to type
self.ent2id = {"<UNK>":0} ## word list to id
self.lower = lower
self.space = ""
def enumerateMatchList(self, word_list):
if self.lower:
word_list = [word.lower() for word in word_list]
match_list = self.trie.enumerateMatch(word_list, self.space)
return match_list
def insert(self, word_list, source):
if self.lower:
word_list = [word.lower() for word in word_list]
self.trie.insert(word_list)
string = self.space.join(word_list)
if string not in self.ent2type:
self.ent2type[string] = source
if string not in self.ent2id:
self.ent2id[string] = len(self.ent2id)
def searchId(self, word_list):
if self.lower:
word_list = [word.lower() for word in word_list]
string = self.space.join(word_list)
if string in self.ent2id:
return self.ent2id[string]
return self.ent2id["<UNK>"]
def searchType(self, word_list):
if self.lower:
word_list = [word.lower() for word in word_list]
string = self.space.join(word_list)
if string in self.ent2type:
return self.ent2type[string]
print ( "Error in finding entity type at gazetteer.py, exit program! String:", string)
exit(0)
def size(self):
return len(self.ent2type)