forked from rlsummerscales/acres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionaryfinder.py
executable file
·31 lines (25 loc) · 1.09 KB
/
dictionaryfinder.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
#!/usr/bin/python
# author: Rodney Summerscales
from rulebasedfinder import RuleBasedFinder
######################################################################
# Dictionary based finder
######################################################################
class DictionaryFinder(RuleBasedFinder):
""" Label words based on their presence in a list of words.
"""
wordSet = None
def __init__(self, entityType, dictionaryFilename):
""" Create a finder that labels tokens with a given type if they appear
in a list of words.
entityType = the mention types to find (e.g. group, outcome)
dictionaryFilename = the path of the file containing the list of words
"""
RuleBasedFinder.__init__(self, [entityType])
self.wordSet = set([])
lines = open(dictionaryFilename, 'r').readlines()
for line in lines:
self.wordSet.add(line.strip())
def applyRules(self, token):
""" Label given token if it appears in a list of words """
if token.text in self.wordSet or token.lemma in self.wordSet:
token.addLabel(self.entityTypes[0])