* Check file existance before opening in lemmatizer

This commit is contained in:
Matthew Honnibal 2015-09-13 10:45:21 +10:00
parent 73d7ece34d
commit 24ed3fc25c
1 changed files with 8 additions and 1 deletions

View File

@ -18,7 +18,10 @@ class Lemmatizer(object):
for pos in ['adj', 'noun', 'verb']: for pos in ['adj', 'noun', 'verb']:
index[pos] = read_index(path.join(data_dir, 'wordnet', 'index.%s' % pos)) index[pos] = read_index(path.join(data_dir, 'wordnet', 'index.%s' % pos))
exc[pos] = read_exc(path.join(data_dir, 'wordnet', '%s.exc' % pos)) exc[pos] = read_exc(path.join(data_dir, 'wordnet', '%s.exc' % pos))
if path.exists(path.join(data_dir, 'vocab', 'lemma_rules.json')):
rules = json.load(open(path.join(data_dir, 'vocab', 'lemma_rules.json'))) rules = json.load(open(path.join(data_dir, 'vocab', 'lemma_rules.json')))
else:
rules = {}
return cls(index, exc, rules) return cls(index, exc, rules)
def __init__(self, index, exceptions, rules): def __init__(self, index, exceptions, rules):
@ -64,6 +67,8 @@ def lemmatize(string, index, exceptions, rules):
def read_index(loc): def read_index(loc):
index = set() index = set()
if not path.exists(loc):
return index
for line in codecs.open(loc, 'r', 'utf8'): for line in codecs.open(loc, 'r', 'utf8'):
if line.startswith(' '): if line.startswith(' '):
continue continue
@ -76,6 +81,8 @@ def read_index(loc):
def read_exc(loc): def read_exc(loc):
exceptions = {} exceptions = {}
if not path.exists(loc):
return exceptions
for line in codecs.open(loc, 'r', 'utf8'): for line in codecs.open(loc, 'r', 'utf8'):
if line.startswith(' '): if line.startswith(' '):
continue continue